Recipientseditor: When you create a text message, enter the recipient's editor,
public class RecipientsEditor extends MultiAutoCompleteTextView { private int mLongPressedPosition = -1; private final RecipientsEditorTokenizer mTokenizer; private char mLastSeparator = ','; public RecipientsEditor(Context context, AttributeSet attrs) { super(context, attrs, android.R.attr.autoCompleteTextViewStyle); mTokenizer = new RecipientsEditorTokenizer(context, this); setTokenizer(mTokenizer); // For the focus to move to the message body when soft Next is pressed setImeOptions(EditorInfo.IME_ACTION_NEXT); // Set threshold as 1 CharSequence. setThreshold(1); addTextChangedListener(new TextWatcher() { private Annotation[] mAffected; public void beforeTextChanged(CharSequence s, int start, int count, int after) { mAffected = ((Spanned) s).getSpans(start, start + count, Annotation.class); } public void onTextChanged(CharSequence s, int start, int before, int after) { if (before == 0 && after == 1) { // inserting a character char c = s.charAt(start); if (c == ',' || c == ';') { // Remember the delimiter the user typed to end this recipient. We'll // need it shortly in terminateToken(). mLastSeparator = c; } } } public void afterTextChanged(Editable s) { if (mAffected != null) { for (Annotation a : mAffected) { s.removeSpan(a); } } mAffected = null; } }); }
Recipientseditor inherits from multiautocompletetextview
Multiple Mobile Phone numbers can be entered. Each mobile phone number is separated by a separator and can be automatically completed. The preset matching data is the contact;
Here, recipientseditortokenizer finds the separators "," and "," in the input string.
private class RecipientsEditorTokenizer implements MultiAutoCompleteTextView.Tokenizer { private final MultiAutoCompleteTextView mList; private final Context mContext; RecipientsEditorTokenizer(Context context, MultiAutoCompleteTextView list) { mList = list; mContext = context; } public int findTokenStart(CharSequence text, int cursor) { int i = cursor; char c; while (i > 0 && (c = text.charAt(i - 1)) != ',' && c != ';') { i--; } while (i < cursor && text.charAt(i) == ' ') { i++; } return i; } public int findTokenEnd(CharSequence text, int cursor) { int i = cursor; int len = text.length(); char c; while (i < len) { if ((c = text.charAt(i)) == ',' || c == ';') { return i; } else { i++; } } return len; } public CharSequence terminateToken(CharSequence text) { int i = text.length(); while (i > 0 && text.charAt(i - 1) == ' ') { i--; } char c; if (i > 0 && ((c = text.charAt(i - 1)) == ',' || c == ';')) { return text; } else { // Use the same delimiter the user just typed. // This lets them have a mixture of commas and semicolons in their list. String separator = mLastSeparator + " "; if (text instanceof Spanned) { SpannableString sp = new SpannableString(text + separator); TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0); return sp; } else { return text + separator; } } }
Setimeoptions (editorinfo. ime_action_next); // set the button function in the lower-right corner of the keyboard to the next one, that is, switch to the next input box, if it is set to editorinfo. ime_action_done indicates that the input is complete, the soft keyboard is disabled, and many other options are available.
Setthreshold (1); // threshold. This parameter is set to start matching as long as one character is entered. If it is set to "2", two characters are required for matching.
Addtextchangedlistener (textwatcher); // Add a textview listener
Three callback methods are available in textwatcher. When the characters in the input box change, the following three methods are automatically called in sequence:
Beforetextchanged (charsequence S, int start, int count, int after );
// Here, the input is used as an example to explain the meaning of the above variables. S is the input string, where the start cursor is located, and count is the number of characters to be changed, that is, the selected number, after indicates the number of inserts.
Ontextchanged (charsequence S, int start, int before, int after)
// S is the changed string. Start is the same as start above. Before is the same as count above. After is the same as after.
Aftertextchanged (editable s) // s is the changed string
Pre-fabricated matching data is used as the contact by setting the adapter:
mRecipientsEditor.setAdapter(new RecipientsAdapter(this));
The recipientsadapter is of the extends resourcecursoradapter
In the adapter, use phone. content_filter_uri to obtain information in the phone book.