0

我想将这种 (888) 777-7777 格式用于电话号码。我的数字格式为“888-777-7777”。我把我的代码放在editTextChangedListener中。如何显示所需的格式?

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

             boolean flag = true;
             String eachBlock[] = edt_clientphone.getText().toString().split("-");
             for (int i = 0; i < eachBlock.length; i++) 
             {
                 if (eachBlock[i].length() > 3)
                 {
                     Log.v("data","cc"+flag + eachBlock[i].length());
                 }
             }
             if(flag){
                 edt_clientphone.setOnKeyListener(new OnKeyListener() {

                     @Override
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        // TODO Auto-generated method stub
                        if (keyCode == KeyEvent.KEYCODE_DEL)
                             keyDel = 1;
                        return false;
                    }
                 });

                 if (keyDel == 0) {

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

             } else {
                 edt_clientphone.setText(ab);
             }       
            }       
        @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 stub

        }
    });
4

4 回答 4

7

尝试这种方式就像魅力一样

EditText edt_clientphone = (EditText) findViewById(R.id.edt_clientphone);
        InputFilter filter = new InputFilter() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.length() > 0) {

                    if (!Character.isDigit(source.charAt(0)))
                        return "";
                    else {
                        if (dstart == 3) {
                            return source + ") ";
                        } else if (dstart == 0) {
                            return "(" + source;
                        } else if ((dstart == 5) || (dstart == 9))
                            return "-" + source;
                        else if (dstart >= 14)
                            return "";
                    }

                } else {

                }

                return null;

            }
        };

        edt_clientphone.setFilters(new InputFilter[] { filter });

不要设置 addTextChangedListener()。

输出

在此处输入图像描述

于 2013-09-26T06:17:52.723 回答
1

如果您想转换 (888)777-7777,下面的代码可以工作

 InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                if (source.length() > 0) {
                    if (!Character.isDigit(source.charAt(0)))
                        return "";
                    else {
                        if (source.toString().length() > 1) {
                            String number = source.toString();
                            char[] digits1 = number.toCharArray();
                            String[] digits2 = number.split("(?<=.)");
                            source = digits2[digits2.length - 1];
                        }
                        if (etPhone.getText().toString().length() < 1) {
                            return "(" + source;
                        } else if (etPhone.getText().toString().length() > 1 && etPhone.getText().toString().length() <= 3) {
                            return source;
                        } else if (etPhone.getText().toString().length() > 3 && etPhone.getText().toString().length() <= 5) {
                            boolean isContain = dest.toString().contains(")");

                            if (isContain) {
                                return source;
                            } else {
                                return ")" + source;
                            }

                        } else if (etPhone.getText().toString().length() > 5 && etPhone.getText().toString().length() <= 7) {
                            return source;
                        } else if (etPhone.getText().toString().length() > 7) {
                            boolean isContain = dest.toString().contains("-");

                            if (isContain) {
                                return source;
                            } else {
                                return "-" + source;
                            }
                        }
                    }
                } else {
                }
                return source;
            }
        };

 etPhone.setFilters(new InputFilter[]{filter});

还请更改您的键盘设置(预测文本)及其对我的工作

于 2015-11-25T00:34:27.540 回答
0

尝试使用这个库。它在 android 内部使用,但不公开,因此您需要将其导入到您的项目中。

对于电话号码格式,请使用:

AsYouTypeFormatter formatter = phoneUtil.getAsYouTypeFormatter("US");

于 2013-09-26T06:50:31.817 回答
0

最基本的:

String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);

这将根据号码所在国家/地区的规则自动格式化号码。

您还可以使用以下方法就地格式化可编辑文本:

PhoneNumberUtils.formatNumber(Editable text, int defaultFormattingType);

查看PhoneNumberUtils以获得更多选项。

于 2013-09-26T06:13:00.913 回答