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

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

RecipientsEditor 继承于 MultiAutoCompleteTextView

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

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

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

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为改变后的字符串

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

  1. mRecipientsEditor.setAdapter(new RecipientsAdapter(this));

RecipientsAdapter 是extends ResourceCursorAdapter的

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

android源码追踪学习 RecipientsEditor的更多相关文章

  1. Android源码-学习随笔

    在线代码网站1:http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/ 书籍: ...

  2. Android学习系列(38)--Android源码下载和编译

    前面多篇文章介绍到如何下载和编译Android或者CM源码,不过一直都是放在<拓展系列>里.随着学习的深入,android源码是非常有参考和学习价值,强烈推荐大家都去下载,编译,学习,所以 ...

  3. (转)Android学习进阶路线导航线路(Android源码分享)

     转载请注明出处:http://blog.csdn.net/qinjuning 前言:公司最近来了很多应届实习生,看着他们充满信心但略带稚气的脸庞上,想到了去年的自己,那是的我是不是也和 现在的他们一 ...

  4. Android源码学习之装饰模式应用

    首先得了解最基础的装饰器模式 参考 设计模式之八 --- 装饰模式(Decorator) 参考链接:http://blog.csdn.net/cjjky/article/details/7478788 ...

  5. 《Android源码设计模式》学习笔记之ImageLoader

    微信公众号:CodingAndroid cnblog:http://www.cnblogs.com/angel88/ CSDN:http://blog.csdn.net/xinpengfei521 需 ...

  6. Android学习进阶路线导航线路(Android源码分享)

                                                                                                       转 ...

  7. Android源码学习之模板方法模式应用

    一.模板方法模式定义 模板方法模式定义: defines the skeleton of an algorithm in a method, deferring some steps to subcl ...

  8. 【Linux/Ubuntu学习6】unbuntu 下载android源码

    在Windows下安装Cygwin,通过Cygwin也可在Windows里通过本文的下载步骤下载Android源码. 以下为在Ubuntu下下载Google Android4.4源码的步骤: 1. 安 ...

  9. Android源码学习(一) 数据集观察者

    查看Android源码发现这个,决定记下下来. 1.在android.database这个包下面,存在这样一个抽象类DataSetObserver,里面包括onChanged()和onInvalida ...

随机推荐

  1. 环境安装问题:tensorflow 问题记录 python2.7 和 python3.6发生冲突

    似乎是pip在python2.7和python3.6中发生冲突 我想用pip但是python2里没有装pip 但是tensorflow是和python2相关联的 所以我在python2中装pip的过程 ...

  2. nginx configuration

    Now that you know how to manage the service itself, you should take a few minutes to familiarize you ...

  3. python 进程锁

    1. #_*_coding:utf-8_*_ from multiprocessing import Process,Lock import os,time def f(l,i): #加锁 l.acq ...

  4. vs2010_相关目录

    1. C:\Program Files\Microsoft SDKs\Windows\v7.0A 2.创建了 C:\Program Files\Microsoft Visual Studio 9.0 ...

  5. Unity中使用的一套敏感词过滤方式

    当项目中的敏感词数量不是很多的时候,直接用数组来遍历过滤其实也可以,但是具体的数量有多大,这个肯定不好说,因此,对.txt中的敏感词合理组织后再进行过滤就显得非常有必要了. 如上图,左边是txt中配置 ...

  6. Oracle 千万级别数据查询优化

    说明:平时很少接触到大数据分页,今天有兴趣在数据库插入1000万条数据进行测试,经过查询相关资料得到如下说明:笔者在工作中有一上百万条记录的表,在jsp页面中需对该表进行分页显示,便考虑用rownum ...

  7. php时区设置 warning: strtotime(): It is not safe to rely on the system's timezone settings

    warning: strtotime(): It is not safe to rely on the system's timezone settings warning: strtotime(): ...

  8. [.NET开发] 浅说C#异步和同步

    提到异步,那么与之对应的是什么呢?同步.那么C#的异步和同步是如何工作的呢? 首先,我们先来看看栗子: 新建一个控制台应用程序,在Program文件中添加如下代码: 1 static void Mai ...

  9. [Java学习] Java包装类、拆箱和装箱详解

    虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...

  10. TCP三次握手(待细研究)

    xu言: 看到一张不错清晰的Tcp三次握手图,收藏 Initiator  发起人 Receiver  接收者 LISTENING 状态xx服务启动后首先处于侦听(LISTENING)状态. ESTAB ...