Turn: http://blog.csdn.net/dajian790626/article/details/8464722
Sometimes you need to display the cursor at the specified position of edittext or select some text. Similarly, in order to facilitate user input to improve user experience, edittext may need to be acquired or focused.
1. Set the cursor to the specified position
- Edittext ET = (edittext) findviewbyid (R. Id. ettest );
- Et. setselection (2 );
PS: when there are too many contents, you can set the cursor position to display the content at this position on the screen.
2. Hide the cursor
- Edittext ET = (edittext) findviewbyid (R. Id. ettest );
- // Set the cursor not to be displayed, but the cursor color cannot be set.
- Et. setcursorvisible (false );
3. Select all text when getting focus
- Edittext ET = (edittext) findviewbyid (R. Id. ettest );
- Et. setselectallonfocus (true );
PS: This method can be used to select the default content when you click edittext.
4. Get and lose focus
- Edittext ET = (edittext) findviewbyid (R. Id. ettest );
- Et. requestfocus (); // The request obtains the focus.
- Et. clearfocus (); // clear the focus
5. Comprehensive application of code
- Edittext ET = (edittext) findviewbyid (R. Id. ettest );
- Int Index = ET. getselectionstart (); // obtain the cursor position
- String text = "# enter the topic here #";
- Editable edit = ET. geteditabletext (); // get the edittext text
- If (index <0 | index> = edit. Length ()){
- Edit. append (text );
- } Else {
- Edit. insert (index, text); // insert text at the cursor position
- }
- Et. setselection (index + 1, index + text. Length ()-1 );
PS: insert the text at the cursor and select the text in #
Organized by: http://orgcent.com/android-edittext-cursor-position-focus/
October 17, 2014 09:22:36
1. Other setting methods for getting and losing focus that you just saw:
Focus acquisition:
titleInput.setFocusable(true); titleInput.requestFocus();
Focus cancellation:
titleInput.setFocusable(false);
Edittext: Set/hide the cursor position, select the text, and obtain/clear the focus ()