android 手机号码显示框,从android的edittext框中获取XXX-XXX-XXXX格式的电话号码

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 ;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值