电话号码:格式化一个EditText Android中 [英] Phone number formatting an EditText in Android

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

问题描述

我想提出一个简单的地址簿应用程序(目标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.

推荐答案

只需使用<一个href="https://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html">PhoneNumberFormattingTextWatcher,只要致电:

editText.addTextChangedListener(new PhoneNumberFormattingTextWatcher());


添加
需要明确的是,PhoneNumberFormattingTextWatcher的<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.2.2_r1/android/telephony/PhoneNumberFormattingTextWatcher.java#PhoneNumberFormattingTextWatcher.afterTextChanged%28android.text.Editable%29">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.

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

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