有两个EditText,在加载页面时,在第一个EditText中设置了一个文本,所以现在光标将位于EditText的起始位置,我想在第二个EditText中设置光标的位置,其中不包含数据。如何做到这一点?


当前回答

如何解决在US Mobile中格式化后自动移动到最后一个位置的光标位置问题,如Android中的(xxx) xxx-xxxx。

private String oldText = "";
private int lastCursor;
private EditText mEtPhone;

@Override
    public void afterTextChanged(Editable s) {
String cleanString = AppUtil.cleanPhone(s.toString());

String format = cleanString.length() < 11 ? cleanString.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3") :
                cleanString.substring(0, 10).replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3");


boolean isDeleted = format.length() < oldText.length();

        try {

            int cur = AppUtil.getPointer(lastCursor, isDeleted ? s.toString() : format, oldText, isDeleted);
            mEtPhone.setSelection(cur > 0 ? cur : format.length());

        }catch (Exception e){
            e.printStackTrace();
            mEtPhone.setSelection(format.length());
        }

        mEtPhone.addTextChangedListener(this);

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        oldText = s.toString();
        lastCursor = start;
    }

在我的例子中,将下面的方法定义为任何Activityclass,活动名称为AppUtil并全局访问它

public static int getPointer(int index, String newString, String oldText, boolean isDeleted){

        int diff = Math.abs(newString.length() - oldText.length());

        return diff > 1 ? isDeleted ? index - diff : index  + diff : isDeleted ? index : index + 1;
    }

public static String cleanPhone(String phone){

        if(TextUtils.isEmpty(phone))
            return "";

        StringBuilder sb = new StringBuilder();
        for(char c : phone.toCharArray()){
            if(Character.isDigit(c))
                sb.append(c);
        }
        return sb.toString();
    }

如果你想设置一个特定的位置

edittext.setSelection(position);

其他回答

我相信最简单的方法就是使用填充。

Say in your xml's edittext section, add android: paddingLeft = " 100 dp” This will move your start position of cursor 100dp right from left end.

同理,你可以用 android: paddingRight = " 100 dp” 这将把你的光标的结束位置从右端向左移动100dp。

要了解更多细节,请查看我博客上的这篇文章:Android:在EditText Widget中设置光标的起始和结束位置

如何解决在US Mobile中格式化后自动移动到最后一个位置的光标位置问题,如Android中的(xxx) xxx-xxxx。

private String oldText = "";
private int lastCursor;
private EditText mEtPhone;

@Override
    public void afterTextChanged(Editable s) {
String cleanString = AppUtil.cleanPhone(s.toString());

String format = cleanString.length() < 11 ? cleanString.replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3") :
                cleanString.substring(0, 10).replaceFirst("(\\d{3})(\\d{3})(\\d+)", "($1) $2-$3");


boolean isDeleted = format.length() < oldText.length();

        try {

            int cur = AppUtil.getPointer(lastCursor, isDeleted ? s.toString() : format, oldText, isDeleted);
            mEtPhone.setSelection(cur > 0 ? cur : format.length());

        }catch (Exception e){
            e.printStackTrace();
            mEtPhone.setSelection(format.length());
        }

        mEtPhone.addTextChangedListener(this);

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        oldText = s.toString();
        lastCursor = start;
    }

在我的例子中,将下面的方法定义为任何Activityclass,活动名称为AppUtil并全局访问它

public static int getPointer(int index, String newString, String oldText, boolean isDeleted){

        int diff = Math.abs(newString.length() - oldText.length());

        return diff > 1 ? isDeleted ? index - diff : index  + diff : isDeleted ? index : index + 1;
    }

public static String cleanPhone(String phone){

        if(TextUtils.isEmpty(phone))
            return "";

        StringBuilder sb = new StringBuilder();
        for(char c : phone.toCharArray()){
            if(Character.isDigit(c))
                sb.append(c);
        }
        return sb.toString();
    }

如果你想设置一个特定的位置

edittext.setSelection(position);

使用下面的行

e2.setSelection(e2.length());

e2是编辑文本对象名称

在kotlin中,你可以像这样创建一个扩展函数:

fun EditText.placeCursorAtLast() {
    val string = this.text.toString()
    this.setSelection(string.length)
}

然后简单地调用myEditText.placeCursorAtLast()

让editText2是你的第二个EditText视图。然后将以下代码放在onResume()中

editText2.setFocusableInTouchMode(true);
editText2.requestFocus();

或者把

<requestFocus />

在第二个EditText视图的xml布局中。