Java android edittext textwatcher格式的电话号码,如xxx xxx xx xx xx
如何使用textwacher设置电话号码格式,如xxx xxx xx 尝试了以下代码,但在我删除元素时它不起作用Java android edittext textwatcher格式的电话号码,如xxx xxx xx xx xx,java,android,Java,Android,如何使用textwacher设置电话号码格式,如xxx xxx xx 尝试了以下代码,但在我删除元素时它不起作用 et_phone_num.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); et_phone_num.addTextChangedListener(new TextWatcher() { @Override public void bef
et_phone_num.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
et_phone_num.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
et_phone_num.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
MyLog.e("onkeychange","key "+keyCode);
if(keyCode == KeyEvent.KEYCODE_DEL) {
keyDel = 1;
MyLog.e("onkeychange","key 1");
}
return false;
}
});
if (keyDel == 0) {
MyLog.e("onkeychange", "if key 0");
int len = et_phone_num.getText().toString().length();
if (len == 3) {
et_phone_num.setText(et_phone_num.getText().toString() + "-");
et_phone_num.setSelection(et_phone_num.getText().toString().length());
} else if (len == 7) {
et_phone_num.setText(et_phone_num.getText().toString() + "-");
et_phone_num.setSelection(et_phone_num.getText().toString().length());
} else if (len == 10) {
et_phone_num.setText(et_phone_num.getText().toString() + "-");
et_phone_num.setSelection(et_phone_num.getText().toString().length());
}
} else {
MyLog.e("onkeychange", "else key 0");
keyDel = 0;
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
使用图书馆
只需像下面那样传递你的面具
<com.github.pinball83.maskededittext.MaskedEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
app:mask="xxx-xxx-xx-xx"
app:notMaskedSymbol="x"
app:maskIconColor="@color/colorPrimary" />
注意:如果您想删除不正确的数字,请将光标放在该数字之前,然后输入您的右数字,此蒙版编辑文本库将对其进行更改。使用库
只需像下面那样传递你的面具
<com.github.pinball83.maskededittext.MaskedEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
app:mask="xxx-xxx-xx-xx"
app:notMaskedSymbol="x"
app:maskIconColor="@color/colorPrimary" />
注意:如果您想删除不正确的数字,请将光标放在该数字之前并输入您的右数字,此蒙面编辑文本库将对其进行更改。最后,我这样做:
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);
}
}
}
活动\u main.xml
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:visibility="visible"
android:id="@+id/et_phone_num"
android:inputType="phone"
android:maxLength="12"
android:digits="0123456789"
android:background="@drawable/phone_edittext_drawable"
android:gravity="center"
android:hint="5XX-XXX-XXXX"
android:imeOptions="actionDone"
android:textColor="@color/cc" />
最后我是这样做的:
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);
}
}
}
活动\u main.xml
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:visibility="visible"
android:id="@+id/et_phone_num"
android:inputType="phone"
android:maxLength="12"
android:digits="0123456789"
android:background="@drawable/phone_edittext_drawable"
android:gravity="center"
android:hint="5XX-XXX-XXXX"
android:imeOptions="actionDone"
android:textColor="@color/cc" />
我想使用textwacher@DhanrajNaik选中编辑它将根据您的要求工作。如果用户输入错误的字符,如何删除库中的中间字符我收到以下错误,并且应用程序挂起“SpellCheckerSession:无法获得建议android.os.DeadObjectException”@DhanrajNaik检查编辑我在回答中更改了库。我想使用textwacher@DhanrajNaik如果用户输入错误的字符,如何删除库中的中间字符?我遇到以下错误,应用程序挂起“SpellCheckerSession:未能获取建议android.os.DeadObjectException”@DhanrajNaik check edit我已在回答中更改了库。