为电话号码格式化EditText视图 [英] Format EditText view for phone numbers

查看:176
本文介绍了为电话号码格式化EditText视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText视图,我希望它将用户的输入格式化为电话号码格式。例如,当用户输入1234567890时,只要前3个数字输入,EditText视图应该动态显示为(123)456-7890。



我在我的OnCreate中尝试了以下内容,但它似乎没有为我做任何事情...

  EditText ET =( EditText)findViewById(R.id.add_number); 
ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

如何让用户的输入以电话号码格式显示?

解决方案

我做了一些事情的组合,我修改我的输入类型为手机,然后我用正则表达式来删除所有非数字字符:phonenumber = phonenumber.replaceAll (\D,);

I have an EditText view and I want it to format the user's input into the phone number format. For example, when the user types in 1234567890, the EditText view should dynamically show it as "(123) 456-7890" as soon as the first 3 numbers are inputted.

I tried the following in my OnCreate but it didn't seem to do anything for me...

EditText ET = (EditText) findViewById(R.id.add_number);
ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

How can I get the user's input to display in the phone number format?

解决方案

I did a combination of things, i modified my input type to phone, then I used regex to remove all non numeric characters: phonenumber = phonenumber.replaceAll("\D", "");

这篇关于为电话号码格式化EditText视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆