[英]getting phone number in XXX-XXX-XXXX format from edittext box in android
嗨,我正在寫一個 android,其中用戶在編輯文本框中輸入電話號碼。 我希望數字采用 xxx-xxx-xxxx 的形式,這意味着在用戶輸入前 3 個字母和另一個 '-' 后,'-' 應該自動出現
我用EditText anum= (EditText)findViewById(R.id.altnum); anum.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
EditText anum= (EditText)findViewById(R.id.altnum); anum.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
但只有在輸入所有數字后才會進入格式。 我希望在用戶輸入數據時進行更改,例如如果他按 123,hypen 應該會自動出現請告訴我該怎么做。
感謝您
此致
ChinniKrishna Kothapalli
最近,我也有同樣的要求。 我用 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
以下代碼可以很好地添加和刪除場景,但邏輯要長一些:
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 ;
}
}
一種選擇是實現您自己的InputFilter 。
你可以在這里使用我的答案: 按“。” 多次(在鍵入時在 EditText 中驗證 ip 地址)和此處: 如何設置 Edittext 視圖僅允許兩個數字值和兩個十進制值,例如 ##.## ,例如如何在鍵入時解析文本。
如果您希望破折號自動出現,您需要將它們添加到您的過濾器方法的返回
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.