Place cursor at the end of text in EditText

I am changing the value of an EditText on keyListener .

But when I change the text the cursor is moving to the beginning of the EditText . I need the cursor to be at the end of the text.

How to move the cursor to the end of the text in a EditText .


尝试这个:

EditText et = (EditText)findViewById(R.id.inbox);
et.setSelection(et.getText().length());

There is a function called append for ediitext which appends the string value to current edittext value and places the cursor at the end of the value. You can have the string value as the current ediitext value itself and call append();

myedittext.append("current_this_edittext_string"); 

If you called setText before and the new text didn't get layout phase call setSelection in a separate runnable fired by View.post(Runnable) (repost from this topic).

So, for me this code works:

editText.setText("text");
editText.post(new Runnable() {
         @Override
         public void run() {
             registerPhone.setSelection("text".length());
         }
});
链接地址: http://www.djcxy.com/p/24204.html

上一篇: findViewById为EditText返回null

下一篇: 将光标置于EditText中的文本末尾