Java 从android的编辑文本框中获取XXX-XXX-XXXX格式的电话号码
嗨 我正在写一个android,用户在编辑文本框中输入电话号码。 我希望数字的形式是xxx xxx xxxx,这意味着在用户输入前3个字母和另一个字母后,“-”将自动出现 我使用了Java 从android的编辑文本框中获取XXX-XXX-XXXX格式的电话号码,java,android,android-layout,android-emulator,android-manifest,Java,Android,Android Layout,Android Emulator,Android Manifest,嗨 我正在写一个android,用户在编辑文本框中输入电话号码。 我希望数字的形式是xxx xxx xxxx,这意味着在用户输入前3个字母和另一个字母后,“-”将自动出现 我使用了EditText anum=(EditText)findviewbyd(R.id.altnum); anum.addTextChangedListener(新电话号码格式TextWatcher()) 但只有在输入了所有数字后,它才会形成格式。我希望在用户输入数据时进行更改,比如如果他按123,催眠就会自动出现 请告诉我
EditText anum=(EditText)findviewbyd(R.id.altnum);
anum.addTextChangedListener(新电话号码格式TextWatcher())代码>
但只有在输入了所有数字后,它才会形成格式。我希望在用户输入数据时进行更改,比如如果他按123,催眠就会自动出现
请告诉我怎么做
谢谢你
您诚挚的
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);
}
}
}
公共类PhoneNumberTextWatcher实现TextWatcher{
私有静态最终字符串标记=PhoneNumberTextWatcher.class
.getSimpleName();
私人编辑文本;
私有布尔isDelete;
公用电话号码ExtWatcher(EditText-edTxtPhone){
this.edTxt=edTxtPhone;
setOnKeyListener(新的OnKeyListener(){
@凌驾
公共布尔onKey(视图v、int keyCode、KeyEvent事件){
if(keyCode==KeyEvent.keyCode_DEL){
isDelete=真;
}
返回false;
}
});
}
public void onTextChanged(字符序列、int start、int before、int count){
}
更改前的公共无效(字符序列、整数开始、整数计数、,
整数后){
}
公共无效后文本已更改(可编辑){
if(isDelete){
isDelete=假;
返回;
}
字符串val=s.toString();
字符串a=“”;
字符串b=“”;
字符串c=“”;
如果(val!=null&&val.length()>0){
val=val.replace(“-”和“”);
如果(值长度()>=3){
a=val.substring(0,3);
}else if(val.length()<3){
a=val.substring(0,val.length());
}
如果(值长度()>=6){
b=val.substring(3,6);
c=val.substring(6,val.length());
}否则如果(val.length()>3&&val.length()<6){
b=val.substring(3,val.length());
}
StringBuffer StringBuffer=新的StringBuffer();
如果(a!=null&&a.length()>0){
stringBuffer.append(a);
如果(a.长度()==3){
stringBuffer.append(“-”);
}
}
如果(b!=null&&b.length()>0){
stringBuffer.append(b);
如果(b.长度()==3){
stringBuffer.append(“-”);
}
}
如果(c!=null&&c.length()>0){
stringBuffer.append(c);
}
edText.removeTextChangedListener(此);
setText(stringBuffer.toString());
setSelection(edTxt.getText().toString().length());
edText.addTextChangedListener(此);
}否则{
edText.removeTextChangedListener(此);
edText.setText(“”);
edText.addTextChangedListener(此);
}
}
}
谢谢
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 ;
}
}
公共类PhoneNumberTextWatcher实现TextWatcher{
私有静态最终字符串标记=“PhoneNumberTextWatcher”;
私人编辑文本;
公用电话号码ExtWatcher(EditText-edTxtPhone){
this.editText=edTxtPhone;
}
public void onTextChanged(字符序列s,int cursorPosition,int before,
整数计数){
如果(之前==0&&count==1){//输入值
字符串val=s.toString();
字符串a=“”;
字符串b=“”;
字符串c=“”;
如果(val!=null&&val.length()>0){
val=val.replace(“-”和“”);
如果(值长度()>=3){
a=val.substring(0,3);
}else if(val.length()<3){
a=val.substring(0,val.length());
}
如果(值长度()>=6){
b=val.substring(3,6);
c=val.substring(6,val.length());
}否则如果(val.length()>3&&val.length()<6){
b=val.substring(3,val.length());
}
StringBuffer StringBuffer=新的StringBuffer();
如果(a!=null&&a.length()>0){
stringBuffer.append(a);
}
如果(b!=null&&b.length()>0){
stringBuffer.append(“-”);
stringBuffer.append(b);
}
如果(c!=null&&c.length()>0){
stringBuffer.append(“-”);
stringBuffer.append(c);
}
removeTextChangedListener(此);
editText.setText(stringBuffer.toString());
如果(光标位置==3 | |光标位置==7){
光标位置=光标位置+2;
}否则{
光标位置=光标位置+1;
}
如果(光标位置0){
val=val.replace(“-”和“”);
如果(光标位置==3){
val=removeCharAt(val,光标位置-1,s.toString().length()-1);
}else if(光标位置==7){
val=removeCharAt(val,光标位置-2,s.toString().length()-2);
}
如果(值长度()>=3){
a=val.substring(0,3);
}else if(val.length()<3){
a=val.substring(0,val.length());
}
如果(值长度()>=6){
b=val.substring(3,6);
c=val.substring(6,val.leng