Similar chat in the @ friends feature, encapsulated in a edittext, gist can't open, directly paste code here:
/**
* @ Friend's input component
*/
public class Atedittext extends EditText {
/**
* The maximum length of the text of @, updated according to the AddAt method
*/
private int mmaxattextlength = 4;
Private Onatchangedlistener monatinputedlistener = null;
Public Atedittext (Context context) {
This (context, NULL);
}
Public Atedittext (context context, AttributeSet Attrs) {
Super (context, attrs);
Init (context, attrs);
}
Public Atedittext (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
Init (context, attrs);
}
public void Setonatinputedlistener (Onatchangedlistener l) {
Monatinputedlistener = l;
}
/**
* Add a text at @
*
* @param attext
*/
public void AddAt (String attext) {
if (Textutils.isempty (Attext)) {
Return
}
Precede insert @, insert space after
String str = "@" + Attext + "";
Editable edit = Geteditabletext ();
Edit.insert (Getselectionend (), str);
Update the longest @ length
int len = Attext.length ();
if (len + 1 > Mmaxattextlength) {
Mmaxattextlength = len + 1;
}
}
/**
* @param attext
* @return Returns the text with @ that will be inserted into the edit and will not be inserted into the EditText
*/
public string Sudoaddat (string attext) {
if (Textutils.isempty (Attext)) {
return null;
}
No spaces after inserting @,sudo before
String str = "@" + attext;
return str;
}
private void init (context context, AttributeSet Attrs) {
This.addtextchangedlistener (New Attextwatcher ());
}
Private int[] Findat (Editable edit, int position) {
if (edit = = null) {
return null;
}
if (Edit.length () < 2) {
return null;
}
if (position < 2) {
return null;
}
if (Position > Edit.length ()) {
return null;
}
String str = edit.tostring ();
for (int i = position-1, j = 0; I >= 0 && J < mmaxattextlength; I--, j + +) {
if ("= = = Str.charat (i)) {
return null;
}
if (' @ ' = = Str.charat (i)) {
int Startofdel = i;//Delete Start index
int endofdel = position;
return new int[] {startofdel, endofdel};
}
}
return null;
}
Private class Attextwatcher implements Textwatcher {
private int countofbefore = 0;
Private char OnC = 1;
@Override
public void beforetextchanged (Charsequence s, int. start, int count, int after) {
Countofbefore = S.length ();
if (Getselectionend ()-1 >-1 && getselectionend ()-1 < S.length ()) {
OnC = S.charat (Getselectionend ()-1);
}
}
@Override
public void aftertextchanged (Editable edit) {
int countofafter = Edit.length ();
int offset = Countofafter-countofbefore;
if (offset = = 1) {//In input
if (Getselectionend ()-1 >-1 && getselectionend ()-1 < Edit.length ()) {
char C = Edit.charat (Getselectionend ()-1);
if (c = = ' @ ') {//AT input @
if (Monatinputedlistener! = null) {
if (monatinputedlistener.onatinputed () = = True) {
Edit.delete (Getselectionend ()-1, Getselectionend ());
}
}
}
}
} else if (offset = =-1) {//in Delete
if (OnC = = ") {//in removing spaces
int[] Delindexs = Findat (edit, Getselectionend ());
if (Delindexs! = null && delindexs.length = = 2) {
int startofdel = delindexs[0];
int endofdel = delindexs[1];
Charsequence cs = edit.subsequence (Startofdel + 1, Endofdel);
Edit.delete (Startofdel, Endofdel);
if (Monatinputedlistener! = NULL && CS! = null) {
Monatinputedlistener.onatdeleted (Cs.tostring ());
}
}
}
}
}
@Override
public void ontextchanged (charsequence s, int start, int before, int count) {
}
}
/**
* When input changes related to @
*
* @author Viyu
*
*/
Public interface Onatchangedlistener {
/**
* Callback When the @ is entered
*
* @return True if you want to delete the @ that you just entered, take over yourself; false, whatever.
*/
public boolean onatinputed ();
/**
* Callback when a "@abc" is deleted
*
* @param textwithoutat
* "@abc" is deleted, is "ABC"
*/
public void onatdeleted (String textwithoutat);
}
}
@ Friend's EditText