How to set cursor position in EditText?

2019-01-07 04:54发布

There are two EditText,while loading the page a text is set in the first EditText, So now cursor will be in the starting place of EditText, I want to set cursor position in the second EditText which contains no data. How to do this?

20条回答
虎瘦雄心在
2楼-- · 2019-01-07 05:24

Remember call requestFocus() before setSelection for edittext.

查看更多
地球回转人心会变
3楼-- · 2019-01-07 05:25

I believe the most simple way to do this is just use padding.

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

Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.

For more detail, check this article on my blog: Android: Setting Cursor Starting and Ending Position in EditText Widget

查看更多
冷血范
4楼-- · 2019-01-07 05:27
EditText editText = findViewById(R.id.editText);
editText.setSelection(editText.getText().length());
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-07 05:29

Let editText2 is your second EditText view .then put following piece of code in onResume()

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

or put

<requestFocus />

in your xml layout of the second EditText view.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-07 05:29

If you want to set the cursor after n character from right to left then you have to do like this.

edittext.setSelection(edittext.length()-n);

If edittext's text like

version<sub></sub>

and you want to move cursor at 6th position from right

Then it will move the cursor at-

    version<sub> </sub>
                ^
查看更多
闹够了就滚
7楼-- · 2019-01-07 05:32

use the below line

e2.setSelection(e2.length());

e2 is edit text Object Name

查看更多
登录 后发表回答