在Android开发过程中,有时候想让光标显示在EditText的指定位置或者选中某些文本。同样,为了方便用户输入以提升用户体验,可能需要使EditText获得或失去焦点,下面是设置方法。
1. 设置光标到指定位置
1 2 |
EditText et = (EditText ) findViewById (R. id. etTest ) ; et. setSelection ( 4 ) ; |
2. 隐藏光标
1 2 3 |
EditText et = (EditText ) findViewById (R. id. etTest ) ; //设置光标不显示,但不能设置光标颜色 et. setCursorVisible ( false ) ; |
3. 获得焦点时全选文本
1 2 |
EditText et = (EditText ) findViewById (R. id. etTest ) ; et. setSelectAllOnFocus ( true ) ; |
4. 获取和失去焦点
1 2 3 |
EditText et = (EditText ) findViewById (R. id. etTest ) ; et. requestFocus ( ) ; //请求获取焦点 et. clearFocus ( ) ; //清除焦点 |