Android中EditText输入框焦点从文字前面变成文字后面

来源:互联网 发布:大连交通网络教学中心 编辑:程序博客网 时间:2024/06/11 13:07

Android中一个EditText,通过代码已经设置了默认的值为字符串0了:

?
1
2
etxtSingleAddress = (EditText)findViewById(R.id.pollingAddressValue);
etxtSingleAddress.setText("0");

然后显示也算正常:

可以显示出字符串0

但是在进入页面后,可编辑文本的焦点(光标),不是出于文字的最后,而是文字的前面:

即:

先是光标,然后才是字符串0

mouse cursor before text in edittext

现在希望变成:

先是字符串0,然后再试光标。

【折腾过程】

1.去搜:

android edittext focus position

google search android edittext focus position

参考:

android – Place cursor at the end of text in EditText – Stack Overflow

How to set focus to right of text in EditText for android? – Stack Overflow

android – How to set Cursor position in EditText? – Stack Overflow

但是我此处不希望是直接去修改某个获得了焦点的EditText。

而是希望,当前UI显示后,不管是选中了哪个EditText,都可以自动将光标位置放到最后。

2.所以再去找找关于输入法方面的事情。

但是算了,还是暂且试试这个:

?
1
2
3
etxtSingleAddress = (EditText)findViewById(R.id.pollingAddressValue);
etxtSingleAddress.setText("0");
etxtSingleAddress.setSelection(etxtSingleAddress.getText().length());

看看效果吧:

还是可以暂且实现对应的效果的:

edittext cursor has move the end of text

3.而此处,由于存在多个EditText,所以上述的办法,不是很好:

万一默认的焦点,是给了别的EditText,结果别的EditText会不会又出现同样的问题?

所以去试试:

参考:

android – EditText cursor position – Stack Overflow

去:

?
1
2
3
4
5
6
7
8
9
10
etxtSingleAddress = (EditText)findViewById(R.id.pollingAddressValue);
etxtSingleAddress.setText("0");
etxtSingleAddress.setSelection(etxtSingleAddress.getText().length());
 
etxtRangeAddressFrom = (EditText)findViewById(R.id.pollingRangeFromValue);
etxtRangeAddressFrom.setText("0");
etxtRangeAddressTo = (EditText)findViewById(R.id.pollingRangeToValue);
etxtRangeAddressTo.setText("15");
 
etxtRangeAddressFrom.requestFocus();

看看结果如何:

竟然是,另外一个EditText,通过获得了focus后,光标自动已经在显示内容的最后了:

if other edittext got focus has ok cursor is end

4.所以,再去重新试试,之前的那个EditText,也是去请求focus:

?
1
2
3
4
5
6
7
8
9
etxtSingleAddress = (EditText)findViewById(R.id.pollingAddressValue);
etxtSingleAddress.setText("0");
etxtSingleAddress.requestFocus();
//etxtSingleAddress.setSelection(etxtSingleAddress.getText().length());
 
etxtRangeAddressFrom = (EditText)findViewById(R.id.pollingRangeFromValue);
etxtRangeAddressFrom.setText("0");
etxtRangeAddressTo = (EditText)findViewById(R.id.pollingRangeToValue);
etxtRangeAddressTo.setText("15");

看看效果如何:

发现也是可以的:

光标是显示在EditText的内容:字符串0,之后的。

此时,基本猜到了:

肯定是:

对于EditText来说,其实光标始终都是显示在当然EditText内容的最后的:

  • 如果开始没有内容,那么光标自然是在最前面的
    • 即使后来设置了内容,那么光标也还是保持不动的,还是放在最前面
    • 此时想要放到后面就只有去用setSelection指定光标位置了
  • 如果之前已经有了内容,而后光标自然是放在内容的后面的了

所以可以去试试:

?
1
2
3
etxtSingleAddress = (EditText)findViewById(R.id.pollingAddressValue);
etxtSingleAddress.requestFocus();
etxtSingleAddress.setText("0");

果然是,光标是在字符串0之前;

5.所以,最终改为:

?
1
2
3
4
5
6
7
8
9
10
11
etxtSingleAddress = (EditText)findViewById(R.id.pollingAddressValue);
etxtSingleAddress.setText("0");
//etxtSingleAddress.setSelection(etxtSingleAddress.getText().length());
 
etxtRangeAddressFrom = (EditText)findViewById(R.id.pollingRangeFromValue);
etxtRangeAddressFrom.setText("0");
etxtRangeAddressTo = (EditText)findViewById(R.id.pollingRangeToValue);
etxtRangeAddressTo.setText("15");
 
//set default focus
etxtSingleAddress.requestFocus();

即可实现:

设置了内容之后,再去设置默认的焦点,此时即可确保,光标位于内容之后;

且如果是想要换其他的默认焦点,也可以保证,光标还是位于该EditText的内容之后的。

 

【总结】

总之:

对于Android中的EditText来说,想要保持光标位于内容最后,

则要保证,先去设置了内容,然后才得到的焦点,即可;

否则先获得了焦点,后续再设置内容,此时焦点肯定是放在内容之前的了,则就需要额外调用setSelection去调整位置了。

转载自:http://blog.csdn.net/ysh06201418/article/details/49074263

0 0
原创粉丝点击