[英]Android - Formatting phone number in EditText
我需要一點幫助使其工作,因為服務器端API需要電話號碼字段的特殊格式。 也許我可以在發送API請求之前通過在特定位置替換字符來編輯該字段,但是這仍然使用戶可以自由地為電話號碼插入錯誤的格式。 在他更改文本並將其定向為正確格式之后,我需要立即使EditText協助他。
為此,我使用了TextWatcher方法afterTextChanged()
,接下來需要使用的格式是(063)22-22-333
這是我嘗試過的:
private static final char space = '-';
private static final char brackets = '(';
private static final char brackets1 = ')';
etPhone.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) {
}
@Override
public void afterTextChanged(Editable s) {
// (063)22-22-333
// -> Error
if (s.length() > 0 && !s.toString().startsWith("(")) {
s.replace(0, s.length(), "(0");
}
if (s.length() > 0 && s.length() == 8) {
final char c = s.charAt(s.length() - 1);
if (space == c) {
s.delete(s.length() - 1, s.length());
}
} else if (s.length() > 0 && s.length() == 5) {
final char c = s.charAt(s.length() - 1);
if (brackets1 == c) {
s.delete(s.length() - 1, s.length());
}
}
// Insert char where needed.
if (s.length() > 0 && s.length() == 8) {
char c = s.charAt(s.length() - 1);
// Only if its a digit where there should be a space we insert a space
if (Character.isDigit(c) && TextUtils.split(s.toString(), String.valueOf(space)).length <= 7) {
s.insert(s.length() - 1, String.valueOf(space));
}
} else if (s.length() > 0 && s.length() == 5) {
char c = s.charAt(s.length() - 1);
if (Character.isDigit(c) && TextUtils.split(s.toString(), String.valueOf(brackets1)).length <= 4) {
s.insert(s.length() - 1, String.valueOf(brackets1));
}
}
}
});
寫4個字符后出現錯誤。 開頭顯示錯誤。
您正在使用split()方法,該方法需要一個正則表達式作為其第二個參數,這是您的問題:您需要在正則表達式中轉義“(”和“)”。 因此,我進行了一些更改:
private static final char space = '-';
private static final char brackets = '(';
private static final char brackets1 = ')';
private static final String sspace = "\\x32";
private static final String sbrackets = "\\x28";
private static final String sbrackets1 = "\\x29";
etPhone.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) {
}
@Override
public void afterTextChanged(Editable s) {
// (063)22-22-333
// -> Error
if (s.length() > 0 && !s.toString().startsWith("(")) {
s.replace(0, s.length(), "(0");
}
if (s.length() > 0 && s.length() == 8) {
final char c = s.charAt(s.length() - 1);
if (space == c) {
s.delete(s.length() - 1, s.length());
}
} else if (s.length() > 0 && s.length() == 5) {
final char c = s.charAt(s.length() - 1);
if (brackets1 == c) {
s.delete(s.length() - 1, s.length());
}
}
// Insert char where needed.
if (s.length() > 0 && s.length() == 8) {
char c = s.charAt(s.length() - 1);
// Only if its a digit where there should be a space we insert a space
if (Character.isDigit(c) && TextUtils.split(s.toString(), sspace).length <= 7) {
s.insert(s.length() - 1, String.valueOf(space));
}
} else if (s.length() > 0 && s.length() == 5) {
char c = s.charAt(s.length() - 1);
if (Character.isDigit(c) && TextUtils.split(s.toString(), sbrackets1).length <= 4) {
s.insert(s.length() - 1, String.valueOf(brackets1));
}
}
}
});
現在,在3d字符之后您不會崩潰。
每次您要使用split()而不是String.valueOf(brackets1)
使用brackets1
或sbrackets
或sspace
從通用格式轉換為服務器端期望的格式可能會更容易。 為了達到標准格式,已經存在一些可以幫助您的類。 您看過PhoneNumberFormattingTextWatcher
嗎?
如果您絕對需要使用自己的觀察程序,則還有其他實用程序可以幫助您:
// this will result in the string "(212) 555-2232"
String number = PhoneNumberUtils.formatNumber("2125552232", "US")
在獲得可預測的結果之后,轉換為服務器所需的內容應該很簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.