2

在我的应用程序中,我有一个 editText 它将接受用户的电话号码,我的目标是,一旦用户输入电话号码,它就应该被格式化(就像通过应用文本更改的监听器一样),格式就像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,它就无法正常工作。提前致谢。

4

3 回答 3

8

假设您想按照美国格式格式化电话号码。

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

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

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;
        }
    }
});

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

PhoneNumberUtils.formatNumber(
    s, PhoneNumberUtils.getFormatTypeForLocale(Locale.US));     
于 2013-03-19T11:04:39.940 回答
0

此代码适用于删除和编辑两种情况。

 public class PhoneNumberFormattingTextWatcher implements TextWatcher {
EditText Edt;
int keyDel=0;
public PhoneNumberFormattingTextWatcher(EditText edt){
 Edt =edt;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    Edt.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (keyCode == KeyEvent.KEYCODE_DEL)
                keyDel = 1;
            return false;
        }
    });

    if (keyDel == 0) {
        int len = Edt.getText().length();
        String input=Edt.getText().toString();
        if(len == 4) {
            Edt.setText( input.substring(0,3)+"-"+ input.substring(3) );
            Edt.setSelection(Edt.getText().length());
        }
        if(len == 8) {
            Edt.setText( input.substring(0,7)+"-"+ input.substring(7) );
            Edt.setSelection(Edt.getText().length());
        }
    } else if(keyDel==1){
        int len = Edt.getText().length();
        if(len == 4||len==8) {
            Edt.setText(Edt.getText().delete(Edt.getText().length()-1,Edt.getText().length()) );
            Edt.setSelection(Edt.getText().length());
        }
        keyDel = 0;
    }
}

@Override
public void afterTextChanged(Editable editable) {

}

}

于 2016-10-18T18:36:16.200 回答
-1

您可以使用愚蠢的代码,它会起作用。

String number="5129892680";
String formattedNumber = PhoneNumberUtils.formatNumber(number);
于 2017-03-16T08:32:16.823 回答