从Android中EditText框中获取XXX-XXX-XXXX格式的电话号码

4

你好,我正在编写一个Android应用程序,用户可以在编辑框中输入电话号码。我希望该号码的格式为xxx-xxx-xxxx,也就是说,在用户输入前三个数字后,应该自动添加'-',并在再次输入三个数字后再次添加'-'。

我使用了以下代码:

EditText anum= (EditText)findViewById(R.id.altnum); anum.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

但是,该代码只会在用户输入完所有数字后才会将其格式化。我希望当用户输入数据时,例如按下123时,应自动添加连字符。请告诉我如何实现这一点。

谢谢!

此致

敬礼

ChinniKrishna Kothapalli

3个回答

9

最近,我有同样的需求。我尝试使用TextWatcher实现。在这里分享一下,希望能帮到其他人。

public class PhoneNumberTextWatcher implements TextWatcher {

private static final String TAG = PhoneNumberTextWatcher.class
        .getSimpleName();
private EditText edTxt;
private boolean isDelete;

public PhoneNumberTextWatcher(EditText edTxtPhone) {
    this.edTxt = edTxtPhone;
    edTxt.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                isDelete = true;
            }
            return false;
        }
    });
}

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

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
}

public void afterTextChanged(Editable s) {

    if (isDelete) {
        isDelete = false;
        return;
    }
    String val = s.toString();
    String a = "";
    String b = "";
    String c = "";
    if (val != null && val.length() > 0) {
        val = val.replace("-", "");
        if (val.length() >= 3) {
            a = val.substring(0, 3);
        } else if (val.length() < 3) {
            a = val.substring(0, val.length());
        }
        if (val.length() >= 6) {
            b = val.substring(3, 6);
            c = val.substring(6, val.length());
        } else if (val.length() > 3 && val.length() < 6) {
            b = val.substring(3, val.length());
        }
        StringBuffer stringBuffer = new StringBuffer();
        if (a != null && a.length() > 0) {
            stringBuffer.append(a);
            if (a.length() == 3) {
                stringBuffer.append("-");
            }
        }
        if (b != null && b.length() > 0) {
            stringBuffer.append(b);
            if (b.length() == 3) {
                stringBuffer.append("-");
            }
        }
        if (c != null && c.length() > 0) {
            stringBuffer.append(c);
        }
        edTxt.removeTextChangedListener(this);
        edTxt.setText(stringBuffer.toString());
        edTxt.setSelection(edTxt.getText().toString().length());
        edTxt.addTextChangedListener(this);
    } else {
        edTxt.removeTextChangedListener(this);
        edTxt.setText("");
        edTxt.addTextChangedListener(this);
    }

}
 }

谢谢

JRH


1
解决方案不起作用!在删除时卡在“-”处。 - RobinHood
@RobinHood,你能分享一下你尝试过的代码吗?同时,请看一下我尝试的样例。https://www.dropbox.com/s/72w2kojffb39y37/TestPhoneNumWatcher.zip - jrh
  1. 是的,当在字符“-”处删除时会卡住。
  2. 当我将光标放在任何位置并删除任何字符时,该字符将被删除并且光标移动到最后。这也不应该发生。
- B Bhanu Chander
有人找到了卡住的解决方案吗? - Aditi Parikh
@AditiParikh,你能分享一下代码吗?之前粘贴的代码对我来说是有效的。但是没有在最新版本中进行测试。 - jrh
显示剩余4条评论

6
以下代码可以正常添加和删除,但是逻辑有些冗长:
```html

以下代码可以正常添加和删除,但是逻辑有些冗长:

```
public class PhoneNumberTextWatcher implements TextWatcher {

private static final String TAG = "PhoneNumberTextWatcher";
private EditText editText;

public PhoneNumberTextWatcher(EditText edTxtPhone) {
    this.editText = edTxtPhone;
}

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

    if(before == 0 && count == 1){  //Entering values

        String val = s.toString();
        String a = "";
        String b = "";
        String c = "";
        if (val != null && val.length() > 0) {
            val = val.replace("-", "");
            if (val.length() >= 3) {
                a = val.substring(0, 3);
            } else if (val.length() < 3) {
                a = val.substring(0, val.length());
            }
            if (val.length() >= 6) {
                b = val.substring(3, 6);
                c = val.substring(6, val.length());
            } else if (val.length() > 3 && val.length() < 6) {
                b = val.substring(3, val.length());
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (a != null && a.length() > 0) {
                stringBuffer.append(a);

            }
            if (b != null && b.length() > 0) {
                stringBuffer.append("-");
                stringBuffer.append(b);

            }
            if (c != null && c.length() > 0) {
                stringBuffer.append("-");
                stringBuffer.append(c);
            }
            editText.removeTextChangedListener(this);
            editText.setText(stringBuffer.toString());
            if(cursorPosition == 3 || cursorPosition == 7){
                cursorPosition = cursorPosition+2;
            }else{
                cursorPosition = cursorPosition+1;
            }
            if(cursorPosition <= editText.getText().toString().length()) {
                editText.setSelection(cursorPosition);
            }else{
                editText.setSelection(editText.getText().toString().length());
            }
            editText.addTextChangedListener(this);
        } else {
            editText.removeTextChangedListener(this);
            editText.setText("");
            editText.addTextChangedListener(this);
        }

    }

    if(before == 1 && count == 0){  //Deleting values

        String val = s.toString();
        String a = "";
        String b = "";
        String c = "";

        if (val != null && val.length() > 0) {
            val = val.replace("-", "");
            if(cursorPosition == 3){
                val = removeCharAt(val,cursorPosition-1,s.toString().length()-1);
            }else if(cursorPosition == 7){
                val = removeCharAt(val,cursorPosition-2,s.toString().length()-2);
            }
            if (val.length() >= 3) {
                a = val.substring(0, 3);
            } else if (val.length() < 3) {
                a = val.substring(0, val.length());
            }
            if (val.length() >= 6) {
                b = val.substring(3, 6);
                c = val.substring(6, val.length());
            } else if (val.length() > 3 && val.length() < 6) {
                b = val.substring(3, val.length());
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (a != null && a.length() > 0) {
                stringBuffer.append(a);

            }
            if (b != null && b.length() > 0) {
                stringBuffer.append("-");
                stringBuffer.append(b);

            }
            if (c != null && c.length() > 0) {
                stringBuffer.append("-");
                stringBuffer.append(c);
            }
            editText.removeTextChangedListener(this);
            editText.setText(stringBuffer.toString());
            if(cursorPosition == 3 || cursorPosition == 7){
                cursorPosition = cursorPosition-1;
            }
            if(cursorPosition <= editText.getText().toString().length()) {
                editText.setSelection(cursorPosition);
            }else{
                editText.setSelection(editText.getText().toString().length());
            }
            editText.addTextChangedListener(this);
        } else {
            editText.removeTextChangedListener(this);
            editText.setText("");
            editText.addTextChangedListener(this);
        }

    }



}

public void beforeTextChanged(CharSequence s, int start, int count,
                              int after) {
}

public void afterTextChanged(Editable s) {


}

public static String removeCharAt(String s, int pos,int length) {

    String value = "";
    if(length > pos){
        value = s.substring(pos + 1);
    }
    return s.substring(0, pos)+value ;
}
}

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接