正在开发一个简单的应用程序。其中EditText从user.i输入数字想要以XXX-XX-XXXX格式而不是XXXXXXXXX格式显示数字。有任何方法可以实现它。谢谢提前.. ...
答案 0 :(得分:1)
经过很长一段时间后,我通过使用此代码获得了它。
修复edittext xml中的行
android:maxLength="11"
现在尝试此代码
final EditText edittext = (EditText) findViewById(R.id.editText4);
edittext.addTextChangedListener(new TextWatcher() {
int cursorPosition = 0;
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
edittext.removeTextChangedListener(this);
try {
cursorPosition = edittext.getSelectionStart();
if (editable.length() > 0) {
String tempIp = "";
String newIp = "";
String strIp = edittext.getText().toString();
String tempParamArr[] = strIp.split("-");
if (tempParamArr.length > 0) {
cursorPosition -= (tempParamArr.length - 1);
for (int i = 0; i < tempParamArr.length; i++) {
tempIp += tempParamArr[i];
}
} else {
tempIp = strIp;
}
for (int count = 0; count < tempIp.length(); count++) {
if (count == 3 || count == 5) {
newIp += "-";
newIp += tempIp.charAt(count);
cursorPosition++;
} else {
newIp += tempIp.charAt(count);
}
}
edittext.setText(newIp);
if (newIp.length() > cursorPosition)
edittext.setSelection(cursorPosition);
else
edittext.setSelection(newIp.length());
}
} catch (Exception e) {
e.printStackTrace();
}
edittext.addTextChangedListener(this);
}
});
答案 1 :(得分:0)
尝试这样可以帮到你,
EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.addTextChangedListener(new NumberTextWatcher());
NumberTextWatcher - 处理自动插入破折号并在用户删除时自动删除破折号:
public class NumberTextWatcher implements TextWatcher {
private boolean isFormatting;
private boolean deletingHyphen;
private int hyphenStart;
private boolean deletingBackward;
@Override
public void afterTextChanged(Editable text) {
if (isFormatting)
return;
isFormatting = true;
// If deleting hyphen, also delete character before or after it
if (deletingHyphen && hyphenStart > 0) {
if (deletingBackward) {
if (hyphenStart - 1 < text.length()) {
text.delete(hyphenStart - 1, hyphenStart);
}
} else if (hyphenStart < text.length()) {
text.delete(hyphenStart, hyphenStart + 1);
}
}
if (text.length() == 3 || text.length() == 7) {
text.append('-');
}
isFormatting = false;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if (isFormatting)
return;
// Make sure user is deleting one char, without a selection
final int selStart = Selection.getSelectionStart(s);
final int selEnd = Selection.getSelectionEnd(s);
if (s.length() > 1 // Can delete another character
&& count == 1 // Deleting only one character
&& after == 0 // Deleting
&& s.charAt(start) == '-' // a hyphen
&& selStart == selEnd) { // no selection
deletingHyphen = true;
hyphenStart = start;
// Check if the user is deleting forward or backward
if (selStart == start + 1) {
deletingBackward = true;
} else {
deletingBackward = false;
}
} else {
deletingHyphen = false;
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
答案 2 :(得分:0)
使用格式化您编号的PhoneNumberUtils:
String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);
请查看PhoneNumberUtils了解更多选项。