Android中的电话号码格式 [英] Phone number format in android

查看:373
本文介绍了Android中的电话号码格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个editText,它将接受用户的电话号码,我的目标是,只要用户输入电话号码,就应该对其进行格式化(例如,通过在文本更​​改后的侦听器上应用),格式就像XXX-XXX-XXXX.

In my application, I have a editText which will accept phone number from the user, My target is, as soon as user enters the phone number it should be formatted (Like by applying on text changed listener) , format is like XXX-XXX-XXXX.

我将代码写为

ePhone.addTextChangedListener(new TextWatcher() {

            private Pattern pattern;
            private Matcher matcher;
            String a;


            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

                boolean flag = true;            
                if (flag) {

                    if (((ePhone.getText().length() + 1) % 4) == 0) 
                    {
                        if (ePhone.getText().toString().split("-").length <= 2) 
                        {
                            ePhone.setText(ePhone.getText() + "-");
                            ePhone.setSelection(ePhone.getText().length());
                        }
                    }
                    a = ePhone.getText().toString();
                } else {
                    ePhone.setText(a);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stus

            }
        });

但是,当用户想要从此editText中删除一个号码时,该号码将无法正常工作. 预先感谢.

But when the user wants to delete a number from this editText, its not working properly. Thanks in advance.

推荐答案

假设您要按照美国格式设置电话号码.

Supposing that you wanna format the phone number as per US format.

+1 (###) ###-####,1 (###) ###-####,###-####,###-###-####,011 $

以下内容将满足您的目的:

The following will serve your purpose:

phoneEditText.addTextChangedListener(new TextWatcher() {
    private boolean mFormatting; // a flag that prevents stack overflows.
    private int mAfter;

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

    //called before the text is changed...
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mAfter = after; // flag to detect backspace.
    }

    @Override
    public void afterTextChanged(Editable s) {
    // Make sure to ignore calls to afterTextChanged caused by the work done below
        if (!mFormatting) {
            mFormatting = true;
            // using US formatting.
            if(mAfter != 0) // in case back space ain't clicked.
                PhoneNumberUtils.formatNumber(
                    s,PhoneNumberUtils.getFormatTypeForLocale(Locale.US));
            mFormatting = false;
        }
    }
});

如果您需要特定于位置的服务,即对于每个位置,则需要该位置的特定格式(请参阅此链接). 如果只需要所需的格式,则在上面的代码片段中的行处编写一个自定义函数.

If you need location specific services, i.e. for each location, you need specific format of that place (refer to this link). If you need only the format you needed, then write a custom function in the place of line in the above code snippet.

PhoneNumberUtils.formatNumber(
    s, PhoneNumberUtils.getFormatTypeForLocale(Locale.US));     

这篇关于Android中的电话号码格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆