我们可以认为这是一个很人性的格式化操作,在ComposeMessageActivity中系统在调用initRecipientsEditor()方法对联系人进行初始化的时候调用了 PhoneNumberFormatter.setPhoneNumberFormattingTextWatcher(this, mRecipientsEditor);我们通过对代码进行追踪发现,最终调用了Framework中PhoneNumberFormattingTextWatcher类对电话号码进行格式化处理,并在处理后在PhoneNumberFormatter中使用异步类控件的值进行处理,这里贴处该类的代码进行分析。

package android.telephony;



import com.android.i18n.phonenumbers.AsYouTypeFormatter;

import com.android.i18n.phonenumbers.PhoneNumberUtil;



import android.telephony.PhoneNumberUtils;

import android.text.Editable;

import android.text.Selection;

import android.text.TextWatcher;



import java.util.Locale;



/**

 * Watches a {@link android.widget.TextView} and if a phone number is entered

 * will format it.

 * <p>

 * Stop formatting when the user

 * <ul>

 * <li>Inputs non-dialable characters</li>

 * <li>Removes the separator in the middle of string.</li>

 * </ul>

 * <p>

 * The formatting will be restarted once the text is cleared.

 */

public class PhoneNumberFormattingTextWatcher implements TextWatcher {



    /**

     * Indicates the change was caused by ourselves.

     */

    private boolean mSelfChange = false;



    /**

     * Indicates the formatting has been stopped.

     */

    private boolean mStopFormatting;



    private AsYouTypeFormatter mFormatter;



    /**

     * The formatting is based on the current system locale and future locale changes

     * may not take effect on this instance.

     */

    public PhoneNumberFormattingTextWatcher() {

        this(Locale.getDefault().getCountry());

    }



    /**

     * The formatting is based on the given <code>countryCode</code>.

     *

     * @param countryCode the ISO 3166-1 two-letter country code that indicates the country/region

     * where the phone number is being entered.

     *

     * @hide

     */

    public PhoneNumberFormattingTextWatcher(String countryCode) {

        if (countryCode == null) throw new IllegalArgumentException();

        mFormatter = PhoneNumberUtil.getInstance().getAsYouTypeFormatter(countryCode);

    }



    @Override

    public void beforeTextChanged(CharSequence s, int start, int count,

            int after) {

        if (mSelfChange || mStopFormatting) {

            return;

        }

        // If the user manually deleted any non-dialable characters, stop formatting

        if (count > 0 && hasSeparator(s, start, count)) {

            stopFormatting();

        }

    }



    @Override

    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (mSelfChange || mStopFormatting) {

            return;

        }

        // If the user inserted any non-dialable characters, stop formatting

        if (count > 0 && hasSeparator(s, start, count)) {

            stopFormatting();

        }

    }



    @Override

    public synchronized void afterTextChanged(Editable s) {

        if (mStopFormatting) {

            // Restart the formatting when all texts were clear.

            mStopFormatting = !(s.length() == 0);

            return;

        }

        if (mSelfChange) {

            // Ignore the change caused by s.replace().

            return;

        }

        String formatted = reformat(s, Selection.getSelectionEnd(s));

        if (formatted != null) {

            int rememberedPos = mFormatter.getRememberedPosition();

            mSelfChange = true;

            s.replace(0, s.length(), formatted, 0, formatted.length());

            // The text could be changed by other TextWatcher after we changed it. If we found the

            // text is not the one we were expecting, just give up calling setSelection().

            if (formatted.equals(s.toString())) {

                Selection.setSelection(s, rememberedPos);

            }

            mSelfChange = false;

        }

    }



    /**

     * Generate the formatted number by ignoring all non-dialable chars and stick the cursor to the

     * nearest dialable char to the left. For instance, if the number is  (650) 123-45678 and '4' is

     * removed then the cursor should be behind '3' instead of '-'.

     */

    private String reformat(CharSequence s, int cursor) {

        // The index of char to the leftward of the cursor.

        int curIndex = cursor - 1;

        String formatted = null;

        mFormatter.clear();

        char lastNonSeparator = 0;

        boolean hasCursor = false;

        int len = s.length();

        for (int i = 0; i < len; i++) {

            char c = s.charAt(i);

            if (PhoneNumberUtils.isNonSeparator(c)) {

                if (lastNonSeparator != 0) {

                    formatted = getFormattedNumber(lastNonSeparator, hasCursor);

                    hasCursor = false;

                }

                lastNonSeparator = c;

            }

            if (i == curIndex) {

                hasCursor = true;

            }

        }

        if (lastNonSeparator != 0) {

            formatted = getFormattedNumber(lastNonSeparator, hasCursor);

        }

        return formatted;

    }



    private String getFormattedNumber(char lastNonSeparator, boolean hasCursor) {

        return hasCursor ? mFormatter.inputDigitAndRememberPosition(lastNonSeparator)

                : mFormatter.inputDigit(lastNonSeparator);

    }



    private void stopFormatting() {

        mStopFormatting = true;

        mFormatter.clear();

    }



    private boolean hasSeparator(final CharSequence s, final int start, final int count) {

        for (int i = start; i < start + count; i++) {

            char c = s.charAt(i);

            if (!PhoneNumberUtils.isNonSeparator(c)) {

                return true;

            }

        }

        return false;

    }

}

通过查看PhoneNumberFormattingTextWatcher代码我们发现,该类继承至TextWatcher,则实现了对RecipientEditor的值的变化进行了监听,最后调用PhoneNumberUtils进行处理。

分析:新建短信,当我们接受人RecipientsEditor中输入+86的时候,系统会自动在+86后加入空格的更多相关文章

  1. 将安卓手机短信导入到iPhone6 plus中

    不越狱的情况下短信不能直接同步到iphone手机,视频.图片.联系人可以直接使用itools的手机搬家功能超方便从android到iphone中.短信得变通的处理才能导入. 工具: 安卓手机iPhon ...

  2. Flask实战第43天:把图片验证码和短信验证码保存到memcached中

    前面我们已经获取到图片验证码和短信验证码,但是我们还没有把它们保存起来.同样的,我们和之前的邮箱验证码一样,保存到memcached中 编辑commom.vews.py .. from utils i ...

  3. android 在新建短信时,加入名称为","(英文逗号)的联系人时,应用崩溃的修改

    请修改文件 /alps/frameworks/ex/chips/src/com/android/ex/chips/RecipientAlternatesAdapter.java private sta ...

  4. Android系统自带APP分析——短信app

    Android操作系统本身就是一个巨大的开源软件仓库,熟悉它既可以了解到Android系统的设计框架,也可以获得高效的应用程序编写方式.本文所分析的源码来自于Google官方的AOSP源码4.0.1_ ...

  5. Android 短信模块分析(四) MMS之短信的发送与接收

     MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...

  6. PhpSms 稳定可靠的php短信发送库

    可能是目前最聪明.优雅的PHP短信发送库了.从此不再为各种原因造成的个别短信发送失败而烦忧! phpsms的任务均衡调度功能由toplan/task-balancer提供. GitHub地址:http ...

  7. C#利用Web Service实现短信发送(转)

    通过编程方式实现短信息的发送对很多人来说是一件比较烦杂的事情,目前一般的解决方法是通过计算机和手机的连线,通过可对手机编程的语言编写相关的手机短信息程序来实现,而这种方法对于一般人来说是很难达到的,因 ...

  8. android: 接收和发送短信

    8.2    接收和发送短信 收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这 项功能,而 Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个 A ...

  9. [android] 短信发送器

    /*****************2016年4月23日 更新********************************/ 知乎:什么是 7 位元的字符? 英文字符难道不是 8 bit 是一个字 ...

随机推荐

  1. UVa 11572 唯一的雪花(滑动窗口)

    https://vjudge.net/problem/UVA-11572 题意:输入一个长度为n的序列A,找到一个尽量长的连续子序列,使得该序列中没有相同的元素. 思路:很简单的题,也没啥好解释的了. ...

  2. UVa 1602 网格动物(回溯)

    https://vjudge.net/problem/UVA-1602 题意:计算n连通块不同形态的个数. 思路: 实在是不知道该怎么做好,感觉判重实在是太麻烦了. 判重就是判断所有格子位置是否都相同 ...

  3. 微信网页跳转页面常见bug处理

    微信网页跳转页面常见bug处理 1.不要直接用a链接直接跳转 2.url后加上时间戳 function gohome() { window.location.href = "../home/ ...

  4. Linux(CentOS 6.5) 下安装MySql 5.7.18 二进制版本粗浅攻略

    鉴于Linux和mysql因不同版本,安装方式也不同,所以在阅读本攻略前,请确保各位同学的版本和我的Linux.MySql 版本一致. 如果不一致,只能参考. 我的版本: Linux CentOS 6 ...

  5. Js页面自动跳转

    //声明 t = 1 var t = 10; function openwin() { t -= 1; if(t==0){ location.href='index2.html'; } setTime ...

  6. wc的使用

    统计文件个数 ls -l |grep "^-"|wc -l 统计目录的个数 ls -l|grep "^d"|wc -l 统计文件个数(包括子文件夹) ls -l ...

  7. iOS进阶_Socket(Socket简介&代码演练)

    网络通讯三要素 TCP & UDP 三次握手 断开连接的四次握手 Socket套接字 了解了上面的概念,我们开始演练一下Socket #import "ViewController. ...

  8. Codeforces 916C - Jamie and Interesting Graph

    916C - Jamie and Interesting Graph 思路:构造. 对于1到n最短路且素数,那么1到n之间连2 对于最小生成树,找一个稍微大点的素数(比1e5大)构造一个和为这个素数的 ...

  9. git/ssh备查文档

    配置多个ssh key: 待更新 git速查表: git remote set-url origin(远程仓库名称) https://xxxxx/ProjectName.git  从ssh切换至htt ...

  10. Linux文件与目录管理(二)

    一.处理目录的常用命令 ls:列出目录 cd:切换目录 pwd:显示当前的目录 mkdir:创建一个新的目录 rmdir:删除一个空的目录 cp:复制文件或者目录 rm:移除文件或者目录 可以使用ma ...