我正在更改keyListener上的EditText的值。

但是当我更改文本时,光标移动到EditText的开头。 我需要光标在文本的末尾。

如何将光标移动到EditText文本的末尾。


当前回答

类似于@Anh Duy的回答,但对我没用。我还需要光标移动到最后,只有当用户点击编辑文本,仍然能够选择光标的位置之后,这是唯一的代码,已经为我工作

boolean textFocus = false; //define somewhere globally in the class

//in onFinishInflate() or somewhere
editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        editText.onTouchEvent(event);

        if(!textFocus) {
            editText.setSelection(editText.getText().length());
            textFocus = true;
        }

        return true;
    }
});

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        textFocus = false;
    }
});

其他回答

这是另一种可能的解决方案:

et.append("");

如果因为某种原因不管用,试试下面的方法:

et.setSelection(et.getText().length());

您应该能够在EditText的方法setSelection()的帮助下实现这一点,请参阅这里

类似于@Anh Duy的回答,但对我没用。我还需要光标移动到最后,只有当用户点击编辑文本,仍然能够选择光标的位置之后,这是唯一的代码,已经为我工作

boolean textFocus = false; //define somewhere globally in the class

//in onFinishInflate() or somewhere
editText.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        editText.onTouchEvent(event);

        if(!textFocus) {
            editText.setSelection(editText.getText().length());
            textFocus = true;
        }

        return true;
    }
});

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        textFocus = false;
    }
});

我测试过的所有其他代码都没有很好地工作,因为用户仍然可以将插入符号/光标放在字符串中间的任何地方(例如:12|3.00 -其中|是光标)。我的解决方案总是在EditText上发生触摸时将光标放在字符串的末尾。

最终的解决方案是:

// For a EditText like:
<EditText
    android:id="@+id/EditTextAmount"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:hint="@string/amount"
    android:layout_weight="1"
    android:text="@string/zero_value"
    android:inputType="text|numberDecimal"
    android:maxLength="13" />

字符串值:

@string/amount="0.00"
@string/zero_value="0.00"

在代码:

// Create a Static boolean flag
private static boolean returnNext; 


// Set caret/cursor to the end on focus change
EditTextAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View editText, boolean hasFocus) {
        if(hasFocus) {
            ((EditText) editText).setSelection(((EditText) editText).getText().length());
        }
    }
});

// Create a touch listener and put caret to the end (no matter where the user touched in the middle of the string)
EditTextAmount.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View editText, MotionEvent event) {
        ((EditText) editText).onTouchEvent(event);
        ((EditText) editText).setSelection(((EditText) editText).getText().length());
        return true;
    }
});


// Implement a Currency Mask with addTextChangedListener
EditTextAmount.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String input = s.toString();
        String output = new String();
        String buffer = new String();
        String decimals = new String();
        String numbers = Integer.toString(Integer.parseInt(input.replaceAll("[^0-9]", "")));

        if(returnNext) {
            returnNext = false;
            return;
        }

        returnNext = true;

        if (numbers.equals("0")) {
            output += "0.00";
        } else if (numbers.length() <= 2) {
            output += "0." + String.format("%02d", Integer.parseInt(numbers));
        } else if(numbers.length() >= 3) {
            decimals = numbers.substring(numbers.length() - 2);
            int commaCounter = 0;
            for(int i = numbers.length() - 3; i >= 0; i--) {
                if(commaCounter == 3) {
                    buffer += ",";
                    commaCounter = 0;
                }
                buffer += numbers.charAt(i);
                commaCounter++;
            }
            output = new StringBuilder(buffer).reverse().toString() + "." + decimals;
        }

        EditTextAmount.setText(output);
        EditTextAmount.setSelection(EditTextAmount.getText().length());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        /*String input = s.toString();
        if(input.equals("0.0")) {
            EditTextAmount.setText("0.00");
            EditTextAmount.setSelection(EditTextAmount.getText().length());
            return;
        }*/
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

希望能有所帮助!

etSSID.setSelection(etSSID.getText().length());