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.