android源码追踪学习 RecipientsEditor

来源:互联网 发布:飞天侠淘宝客9.1破解版 编辑:程序博客网 时间:2024/05/17 22:54

RecipientsEditor 新建短信时输入收接者的editor,

[java] view plaincopy
  1. public class RecipientsEditor extends MultiAutoCompleteTextView {  
  2.     private int mLongPressedPosition = -1;  
  3.     private final RecipientsEditorTokenizer mTokenizer;  
  4.     private char mLastSeparator = ',';  
  5.   
  6.     public RecipientsEditor(Context context, AttributeSet attrs) {  
  7.         super(context, attrs, android.R.attr.autoCompleteTextViewStyle);  
  8.         mTokenizer = new RecipientsEditorTokenizer(context, this);  
  9.         setTokenizer(mTokenizer);  
  10.         // For the focus to move to the message body when soft Next is pressed  
  11.         setImeOptions(EditorInfo.IME_ACTION_NEXT);  
  12.         // Set threshold as 1 CharSequence.  
  13.         setThreshold(1);  
  14.         addTextChangedListener(new TextWatcher() {  
  15.             private Annotation[] mAffected;  
  16.   
  17.             public void beforeTextChanged(CharSequence s, int start,  
  18.                     int count, int after) {  
  19.                 mAffected = ((Spanned) s).getSpans(start, start + count,  
  20.                         Annotation.class);  
  21.             }  
  22.   
  23.             public void onTextChanged(CharSequence s, int start,  
  24.                     int before, int after) {  
  25.                 if (before == 0 && after == 1) {    // inserting a character  
  26.                     char c = s.charAt(start);  
  27.                     if (c == ',' || c == ';') {  
  28.                         // Remember the delimiter the user typed to end this recipient. We'll  
  29.                         // need it shortly in terminateToken().  
  30.                         mLastSeparator = c;  
  31.                     }  
  32.                 }  
  33.             }  
  34.   
  35.             public void afterTextChanged(Editable s) {  
  36.                 if (mAffected != null) {  
  37.                     for (Annotation a : mAffected) {  
  38.                         s.removeSpan(a);  
  39.                     }  
  40.                 }  
  41.   
  42.                 mAffected = null;  
  43.             }  
  44.         });  
  45.     }  

RecipientsEditor 继承于 MultiAutoCompleteTextView

可支持输入多个手机号码,每个手机号码用用分隔符分开,有自动完成功能,预置匹配的数据为联系人;



其中RecipientsEditorTokenizer为了找出输入字符串中的分隔符","和“,”

[java] view plaincopy
  1. private class RecipientsEditorTokenizer  
  2.             implements MultiAutoCompleteTextView.Tokenizer {  
  3.         private final MultiAutoCompleteTextView mList;  
  4.         private final Context mContext;  
  5.   
  6.         RecipientsEditorTokenizer(Context context, MultiAutoCompleteTextView list) {  
  7.             mList = list;  
  8.             mContext = context;  
  9.         }  
  10.   
  11.         public int findTokenStart(CharSequence text, int cursor) {  
  12.             int i = cursor;  
  13.             char c;  
  14.   
  15.             while (i > 0 && (c = text.charAt(i - 1)) != ',' && c != ';') {  
  16.                 i--;  
  17.             }  
  18.             while (i < cursor && text.charAt(i) == ' ') {  
  19.                 i++;  
  20.             }  
  21.   
  22.             return i;  
  23.         }  
  24.   
  25.         public int findTokenEnd(CharSequence text, int cursor) {  
  26.             int i = cursor;  
  27.             int len = text.length();  
  28.             char c;  
  29.   
  30.             while (i < len) {  
  31.                 if ((c = text.charAt(i)) == ',' || c == ';') {  
  32.                     return i;  
  33.                 } else {  
  34.                     i++;  
  35.                 }  
  36.             }  
  37.   
  38.             return len;  
  39.         }  
  40.   
  41.         public CharSequence terminateToken(CharSequence text) {  
  42.             int i = text.length();  
  43.   
  44.             while (i > 0 && text.charAt(i - 1) == ' ') {  
  45.                 i--;  
  46.             }  
  47.   
  48.             char c;  
  49.             if (i > 0 && ((c = text.charAt(i - 1)) == ',' || c == ';')) {  
  50.                 return text;  
  51.             } else {  
  52.                 // Use the same delimiter the user just typed.  
  53.                 // This lets them have a mixture of commas and semicolons in their list.  
  54.                 String separator = mLastSeparator + " ";  
  55.                 if (text instanceof Spanned) {  
  56.                     SpannableString sp = new SpannableString(text + separator);  
  57.                     TextUtils.copySpansFrom((Spanned) text, 0, text.length(),  
  58.                                             Object.class, sp, 0);  
  59.                     return sp;  
  60.                 } else {  
  61.                     return text + separator;  
  62.                 }  
  63.             }  
  64.         }  

setImeOptions(EditorInfo.IME_ACTION_NEXT);//设置软键盘右下角的button的功能为下一个,即切换到下一个输入框,如果设置成EditorInfo.IME_ACTION_DONE,则表示输入完成,关掉软键盘,还有很多其他的选项可供设置的

setThreshold(1);// Threshold门槛的意思,此处设置只要输入一个字符就开始匹配,若设置为“2”则表示要输入两个字符才是匹配。

addTextChangedListener(TextWatcher);//添加一个TextView监听器

TextWatcher里有三个回调方法,当有输入框里的字符有变化时会自动依次调用以下三个方法:

beforeTextChanged(CharSequence s, int start,int count, int after) ;

//此处已输入为例解释上面各变量的意思,s 是输入以前的字符串,start光标所在的位置, count为要改变的字符个数,即选中的个数,after为要插入的个数

onTextChanged(CharSequence s, int start, int before, int after)

//s为改变后的字符串,start和上面的start一样, before和上面的count一样,after与上面的after一样

afterTextChanged(Editable s)// s为改变后的字符串

预制匹配数据为联系人的方法是通过设置适配器:

[java] view plaincopy
  1. mRecipientsEditor.setAdapter(new RecipientsAdapter(this));  
RecipientsAdapter 是extends ResourceCursorAdapter的

在适配器里面通过Phone.CONTENT_FILTER_URI,获取电话本里的信息。


0 0