In the previous article, click QQ emoticons and the QQ emoticons are displayed on edittext, instead of the emoticons.
However, there is no such function, such as the Code of the 88 expression; 88, and then it appears. The project leader has implemented this function and I will share it with you.
In this way, you must implement the textwatcher interface by yourself to enable edittext to register the listener and monitor whether the characters entered by the user are the emoticon code. If so, the listener displays the emoticon, if not, show the text to him.
In this case, you must implement an algorithm to determine the emoticon code. (This cannot be shared, because it is not designed by me)
I have implemented textwatcher and gridview.
Chatemotion has already been explained in the previous article. There is no waste of words here.
Mgridview. setonitemclicklistener (New adapterview. onitemclicklistener (){
@ Override
Public void onitemclick (adapterview <?> Parent, view,
Int position, long ID ){
// Todo auto-generated method stub
Int currentpos = meditview. getselectionstart ();
Meditview. gettext (). insert (currentpos, chatemotion. emo_symbols [position]);
// Spannablestringbuilder SSB = new spannablestringbuilder ();
// SSB. append (getimagespannablestring (position ));
// Meditview. gettext (). insert (currentpos, spannablestring. valueof (SSB ));
Meditview. invalidate ();
}
});
Textwatcher listener:
Class watcher implements textwatcher {
// Ischanged flag to prevent stackoverflowerror caused by settext
Private Boolean ischanged = false;
Int Len, St = 0;
@ Override
Public void beforetextchanged (charsequence S, int start, int count,
Int after ){
// Todo auto-generated method stub
If (ischanged ){
Return;
}
Len = S. Length ();
Int focusstart = meditview. getselectionstart ();
Int focusend = meditview. getselectionend ();
// Log. D (TAG, "Focus start:" + focusstart );
// Log. D (TAG, "Focus end:" + focusend );
// Log. D (TAG, "Len:" + Len );
St = focusstart> = focusend? Focusstart: focusend;
}
@ 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;
}
If (LEN> S. Length ()){
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 ();
Log. D (TAG, "Start:" + st );
Log. D (TAG, "Len:" + Len );
Log. D (TAG, "Length:" + S. Length ());
// Position the cursor. settext only places the cursor at 0.
Meditview. setselection (S. Length ()-len + st );
}
}
Chatemotion. string2symbol (smileactivity. this, STR); this function encapsulates the algorithm design and I will not share it. The prompt is: Convert the input string into a character array, and then use a loop to compare the emoticon characters one by one, it is similar to character matching. Oh, that's it.