I encountered this problem in the previous project and solved it later. I encountered this problem again in this project. As a result, I had a problem for a day and asked my colleagues to open my door.
So write it down and remember it.
Android has an interface for listening input: textwatcher, which can be implemented by itself and then registered with edittext.
Class watcher implements textwatcher {
@ Override
Public void beforetextchanged (charsequence S, int start, int count,
Int after ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
}
@ Override
Public void ontextchanged (charsequence S, int start, int before,
Int count ){
// Todo auto-generated method stub
}
@ Override
Public void aftertextchanged (editable s ){
// Todo auto-generated method stub
Meditview. settext (SS );
}
}
According to the above, a bug will occur: stackoverflowerror.
The reason is that settext () itself calls back the listener. The after function in the listener calls settext, and the result is a mutual call without exit. The result is that the call function stack overflows.
Solution: Do not write the function that triggers the listener in the listener, especially the function that does not have an exit to call each other.
Second, you can write a flag. If the edittext content changes, let him execute the settext function. If the content does not change, the settext function in the listener will not be executed.
: Class watcher implements textwatcher {
Private Boolean ischanged = false;
@ Override
Public void beforetextchanged (charsequence S, int start, int count,
Int after ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
}
@ Override
Public void ontextchanged (charsequence S, int start, int before,
Int count ){
// Todo auto-generated method stub
}
@ Override
Public void aftertextchanged (editable s ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
String STR = S. tostring ();
Spannablestring Ss = chatemotion. string2symbol (smileactivity. This, STR );
Log. D (TAG, ss. tostring ());
Ischanged = true;
Meditview. settext (SS );
Ischanged = false;
Meditview. invalidate ();
}
}
This is OK. Haha, the flag position is true before set, and then the flag position is false.