Edittext: Set/hide the cursor position, select the text, and obtain/clear the focus ()

Source: Internet
Author: User

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

  1. Edittext ET = (edittext) findviewbyid (R. Id. ettest );
  2. 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

  1. Edittext ET = (edittext) findviewbyid (R. Id. ettest );
  2. // Set the cursor not to be displayed, but the cursor color cannot be set.
  3. Et. setcursorvisible (false );

3. Select all text when getting focus

  1. Edittext ET = (edittext) findviewbyid (R. Id. ettest );
  2. Et. setselectallonfocus (true );


PS: This method can be used to select the default content when you click edittext.
4. Get and lose focus

  1. Edittext ET = (edittext) findviewbyid (R. Id. ettest );
  2. Et. requestfocus (); // The request obtains the focus.
  3. Et. clearfocus (); // clear the focus

5. Comprehensive application of code

  1. Edittext ET = (edittext) findviewbyid (R. Id. ettest );
  2. Int Index = ET. getselectionstart (); // obtain the cursor position
  3. String text = "# enter the topic here #";
  4. Editable edit = ET. geteditabletext (); // get the edittext text
  5. If (index <0 | index> = edit. Length ()){
  6. Edit. append (text );
  7. } Else {
  8. Edit. insert (index, text); // insert text at the cursor position
  9. }
  10. 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.