Use the addTextChangedListener (new TextWatcher () method of EditText, edittexttextwatcher

Source: Internet
Author: User

Use the addTextChangedListener (new TextWatcher () method of EditText, edittexttextwatcher

(To: http://www.apkbus.com/android-5257-1-14.html)

When using the addTextChangedListener (new TextWatcher () method of EditText (that is, adding a listener to EditText): Note: 1. When using functions in it, you cannot change the content of the EditText without any conditions, because this will easily lead to an endless loop, therefore, the following conditions must be added ////////////////////////////////// //////////////////
// Add a listener to EditText
ContentEditText. addTextChangedListener (new TextWatcher (){

Int l = 0; // The length of the string before the string is deleted
Int location = 0; // records the cursor position
@ Override
Public void onTextChanged (CharSequence s, int start, int before, int count ){
// TODO Auto-generated method stub

}

@ Override
Public void beforeTextChanged (CharSequence s, int start, int count,
Int after ){
// TODO Auto-generated method stub
L = s. length ();
Location = contentEditText. getSelectionStart ();
}

@ Override
Public void afterTextChanged (Editable s ){
// TODO Auto-generated method stub
If (l> s. toString (). length ()){
Gyf. function. face_analysis faceAnalysis = new gyf. function. face_analysis (releaseComment. this );
SpannableStringBuilder sBuilder = faceAnalysis. getSpannableStringBuilder (s. toString ());
// EText. setText (sBuilder );
// EText. setText ("");
ContentEditText. setText (sBuilder );
Editable etable = contentEditText. getText ();
Selection. setSelection (etable, location );
// Toast. makeText (releaseComment. this, "11111", Toast. LENGTH_SHORT). show ();

}
// Toast. makeText (releaseComment. this, "0000", Toast. LENGTH_SHORT). show ();
}
}); 2. Each time the EditText is refreshed, the cursor is reset, that is, the position begins with the code above. Introduction to the cursor: raised the Android EditText Cursor Selection Problem, you can use android. text. the Selection package provides methods for implementation. The Android SDK provides multiple methods for Cursor Selection, such as getSelectionEnd, getSelectionStart, removeSelection, selectAll, and setSelection. The detailed parameter declaration is as follows:

Final static int getSelectionEnd (CharSequence text) Return the offset of the selection edge or cursor, or-1 if there is no selection or cursor. final static int getSelectionStart (CharSequence text) Return the offset of the selection anchor or cursor, or-1 if there is no selection or cursor. final static void removeSelection (Spannable text) Remove the selection or cursor, if any, from the text. final static void selectAll (Spannable text) Select the entire text. final static void setSelection (Spannable text, int index) Move the cursor to offset index. static void setSelection (Spannable text, int start, int stop) Set the selection anchor to start and the selection edge to stop.



Android123 reminds you that the Spannable type can be found from the above parameters. In general, Editable in our EditText editing directly implements the Spannable interface, so we can set the selection through the following methods:
    • Editable ea = etEdit. getText (); // etEdit is EditText
    • Selection. setSelection (ea, ea. length ()-1); // Android Development Network prompts that the length of the ea must be greater than 1. Otherwise, an exception occurs.

Related Article

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.