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

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

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


当前回答

我测试过的所有其他代码都没有很好地工作,因为用户仍然可以将插入符号/光标放在字符串中间的任何地方(例如: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) {

    }
});

希望能有所帮助!

其他回答

我认为这可以达到你想要的效果。

 Editable etext = mSubjectTextEditor.getText();
 Selection.setSelection(etext, etext.length());
/**
 * Set cursor to end of text in edittext when user clicks Next on Keyboard.
 */
View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (b) {
            ((EditText) view).setSelection(((EditText) view).getText().length());
        }
    }
};

mEditFirstName.setOnFocusChangeListener(onFocusChangeListener); 
mEditLastName.setOnFocusChangeListener(onFocusChangeListener);

这对我很有用!

public class CustomEditText extends EditText {
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context) {
        super(context);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        this.setSelection(this.getText().length());
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {

    }
}

在XML文件中使用此CustomEditText,这将起作用。我已经测试过了,它对我很有效。

这个问题已经很老了,但我认为如果你想使用Android上最新发布的DataBinding工具,有这个答案可能会很有用,只需在XML中设置这个:

<data>
    <variable name="stringValue" type="String"/>
</data>
...
<EditText
        ...
        android:text="@={stringValue}"
        android:selection="@{stringValue.length()}"
        ...
/>

如果您想将光标放置在EditText视图中文本的末尾

 EditText rename;
 String title = "title_goes_here";
 int counts = (int) title.length();
 rename.setSelection(counts);
 rename.setText(title);