android 电话号码,Android中的电话号码自动格式化

我需要按照以下格式在Android的编辑文本中设置电话号码的格式(222)222-2222 ext222222

我创建了一个监视程序,可以为我执行自动格式化.观察者可以正常工作并正确格式化电话号码.我唯一的问题是,当有人手动转到输入电话中的其他位置并开始删除或添加号码时,自动格式化将不起作用.

任何想法如何解决这个问题.

这是我目前的观察者的样子:

editText.addTextChangedListener(object : TextWatcher {

override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

override fun afterTextChanged(s: Editable) {

val text = editText.text.toString()

val textLength = editText.text.length

if (text.endsWith("-") || text.endsWith(" ")) {

return

}

if (textLength == 1) {

if (!text.contains("(")) {

editText.setText(StringBuilder(text).insert(text.length - 1, "(").toString())

editText.setSelection(editText.text.length)

}

} else if (textLength == 5) {

if (!text.contains(")")) {

editText.setText(StringBuilder(text).insert(text.length - 1, ")").toString())

editText.setSelection(editText.text.length)

}

} else if (textLength == 6) {

editText.setText(StringBuilder(text).insert(text.length - 1, " ").toString())

editText.setSelection(editText.text.length)

} else if (textLength == 10) {

if (!text.contains("-")) {

editText.setText(StringBuilder(text).insert(text.length - 1, "-").toString())

editText.setSelection(editText.text.length)

}

} else if (textLength == 15) {

if (text.contains("-")) {

editText.setText(StringBuilder(text).insert(text.length - 1, " ext").toString())

editText.setSelection(editText.text.length)

}

}

}

})

解决方法:

如果所有人都可以依靠我,这就是我可以处理的方式(请参见各行之间的注释).这仅适用于电话号码格式,假设它是最多10位数字的区域号码:

phoneNumber.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) {

/* Let me prepare a StringBuilder to hold all digits of the edit text */

StringBuilder digits = new StringBuilder();

/* this is the phone StringBuilder that will hold the phone number */

StringBuilder phone = new StringBuilder();

/* let's take all characters from the edit text */

char[] chars = phoneNumber.getText().toString().toCharArray();

/* a loop to extract all digits */

for (int x = 0; x < chars.length; x++) {

if (Character.isDigit(chars[x])) {

/* if its a digit append to digits string builder */

digits.append(chars[x]);

}

}

if (digits.toString().length() >=3) {

/* our phone formatting starts at the third character and starts with the country code*/

String countryCode = new String();

/* we build the country code */

countryCode += "(" + digits.toString().substring(0, 3) + ") ";

/** and we append it to phone string builder **/

phone.append(countryCode);

/** if digits are more than or just 6, that means we already have our state code/region code **/

if (digits.toString().length()>=6)

{

String regionCode=new String();

/** we build the state/region code **/

regionCode+=digits.toString().substring(3,6)+"-";

/** we append the region code to phone **/

phone.append(regionCode);

/** the phone number will not go over 12 digits if ten, set the limit to ten digits**/

if (digits.toString().length()>=10)

{

phone.append(digits.toString().substring(6,10));

}else

{

phone.append(digits.toString().substring(6));

}

}else

{

phone.append(digits.toString().substring(3));

}

/** remove the watcher so you can not capture the affectation you are going to make, to avoid infinite loop on text change **/

phoneNumber.removeTextChangedListener(this);

/** set the new text to the EditText **/

phoneNumber.setText(phone.toString());

/** bring the cursor to the end of input **/

phoneNumber.setSelection(phoneNumber.getText().toString().length());

/* bring back the watcher and go on listening to change events */

phoneNumber.addTextChangedListener(this);

} else {

return;

}

}

});

每次用户更改EditText的值时,此代码都会重新格式化电话号码.我已经对其进行了测试,并且可以正常运行并且不会崩溃.您可以测试.

编辑:这是一个Java代码,但我认为您可以轻松地将其重写为Kotlin.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值