android源码追踪学习 RecipientsEditor
RecipientsEditor 新建短信时输入收接者的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 继承于 MultiAutoCompleteTextView
可支持输入多个手机号码,每个手机号码用用分隔符分开,有自动完成功能,预置匹配的数据为联系人;
其中RecipientsEditorTokenizer为了找出输入字符串中的分隔符","和“,”
- 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);//设置软键盘右下角的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为改变后的字符串
预制匹配数据为联系人的方法是通过设置适配器:
- mRecipientsEditor.setAdapter(new RecipientsAdapter(this));
RecipientsAdapter 是extends ResourceCursorAdapter的
在适配器里面通过Phone.CONTENT_FILTER_URI,获取电话本里的信息。
android源码追踪学习 RecipientsEditor的更多相关文章
- Android源码-学习随笔
在线代码网站1:http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/ 书籍: ...
- Android学习系列(38)--Android源码下载和编译
前面多篇文章介绍到如何下载和编译Android或者CM源码,不过一直都是放在<拓展系列>里.随着学习的深入,android源码是非常有参考和学习价值,强烈推荐大家都去下载,编译,学习,所以 ...
- (转)Android学习进阶路线导航线路(Android源码分享)
转载请注明出处:http://blog.csdn.net/qinjuning 前言:公司最近来了很多应届实习生,看着他们充满信心但略带稚气的脸庞上,想到了去年的自己,那是的我是不是也和 现在的他们一 ...
- Android源码学习之装饰模式应用
首先得了解最基础的装饰器模式 参考 设计模式之八 --- 装饰模式(Decorator) 参考链接:http://blog.csdn.net/cjjky/article/details/7478788 ...
- 《Android源码设计模式》学习笔记之ImageLoader
微信公众号:CodingAndroid cnblog:http://www.cnblogs.com/angel88/ CSDN:http://blog.csdn.net/xinpengfei521 需 ...
- Android学习进阶路线导航线路(Android源码分享)
转 ...
- Android源码学习之模板方法模式应用
一.模板方法模式定义 模板方法模式定义: defines the skeleton of an algorithm in a method, deferring some steps to subcl ...
- 【Linux/Ubuntu学习6】unbuntu 下载android源码
在Windows下安装Cygwin,通过Cygwin也可在Windows里通过本文的下载步骤下载Android源码. 以下为在Ubuntu下下载Google Android4.4源码的步骤: 1. 安 ...
- Android源码学习(一) 数据集观察者
查看Android源码发现这个,决定记下下来. 1.在android.database这个包下面,存在这样一个抽象类DataSetObserver,里面包括onChanged()和onInvalida ...
随机推荐
- 环境安装问题:tensorflow 问题记录 python2.7 和 python3.6发生冲突
似乎是pip在python2.7和python3.6中发生冲突 我想用pip但是python2里没有装pip 但是tensorflow是和python2相关联的 所以我在python2中装pip的过程 ...
- nginx configuration
Now that you know how to manage the service itself, you should take a few minutes to familiarize you ...
- python 进程锁
1. #_*_coding:utf-8_*_ from multiprocessing import Process,Lock import os,time def f(l,i): #加锁 l.acq ...
- vs2010_相关目录
1. C:\Program Files\Microsoft SDKs\Windows\v7.0A 2.创建了 C:\Program Files\Microsoft Visual Studio 9.0 ...
- Unity中使用的一套敏感词过滤方式
当项目中的敏感词数量不是很多的时候,直接用数组来遍历过滤其实也可以,但是具体的数量有多大,这个肯定不好说,因此,对.txt中的敏感词合理组织后再进行过滤就显得非常有必要了. 如上图,左边是txt中配置 ...
- Oracle 千万级别数据查询优化
说明:平时很少接触到大数据分页,今天有兴趣在数据库插入1000万条数据进行测试,经过查询相关资料得到如下说明:笔者在工作中有一上百万条记录的表,在jsp页面中需对该表进行分页显示,便考虑用rownum ...
- 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(): ...
- [.NET开发] 浅说C#异步和同步
提到异步,那么与之对应的是什么呢?同步.那么C#的异步和同步是如何工作的呢? 首先,我们先来看看栗子: 新建一个控制台应用程序,在Program文件中添加如下代码: 1 static void Mai ...
- [Java学习] Java包装类、拆箱和装箱详解
虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎合人类根深蒂固的习惯, ...
- TCP三次握手(待细研究)
xu言: 看到一张不错清晰的Tcp三次握手图,收藏 Initiator 发起人 Receiver 接收者 LISTENING 状态xx服务启动后首先处于侦听(LISTENING)状态. ESTAB ...