我想在电话号码之间添加连字符,在android edittext中以123-456-7890的格式添加连字符。我怎样才能实现这一点。请帮我一个片段。谢谢。
答案 0 :(得分:0)
String phoneNumber = "1234567890";
StringBuilder sb = new StringBuilder(phoneNumber)
.insert(4,"-")
.insert(8,"-");
String output = sb.toString();
Log.i("app", String.valueOf(output));
输出:123-456-7890
答案 1 :(得分:0)
您需要使用PhoneNumberFormattingTextWatcher课程来满足您的要求。该课程将根据电话号码自动格式化。
public class MyTextWatcher implements TextWatcher {
private boolean isInAfterTextChanged;
public synchronized void afterTextChanged(Editable text) {
if (!isInAfterTextChanged) {
isInAfterTextChanged = true;
// TODO format code goes here
isInAfterTextChanged = false;
}
}
}