电话号码:格式化一个EditText Android中电话号码、EditText、Android

2023-09-13 00:24:15 作者:全网通缉犯

我想提出一个简单的地址簿应用程序(目标4.2),它的名称,地址,城市,州,邮编和电话。

I am making a simple Address Book app (targeting 4.2) that takes name, address, city, state, zip and phone.

我要格式化的电话号码输入一个电话号码(XXX)XXX-XXXX,但我需要拉的价值作为一个字符串,所以我可以将其存储在我的数据库,当我保存。我怎样才能做到这一点? 我有EditText上设置的电话号码输入,但是这显然没有做太多。

I want to format the phone number input as a phone number (XXX) XXX-XXXX, but I need to pull the value out as a string so I can store it in my database when I save. How can i do this?? I have the EditText set for "phone number" input but that obviously doesn't do too much.

推荐答案

只需使用PhoneNumberFormattingTextWatcher,只要致电:

editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

添加 需要明确的是,PhoneNumberFormattingTextWatcher的backbone是PhoneNumberUtils类。所不同的是TextWatcher保持EditText上,而你必须调用 PhoneNumberUtils.formatNumber()每次更改其内容的时间。

Addition To be clear, PhoneNumberFormattingTextWatcher's backbone is the PhoneNumberUtils class. The difference is the TextWatcher maintains the EditText while you must call PhoneNumberUtils.formatNumber() every time you change its contents.