使用电话号码格式 NaN 屏蔽 EditText,如 PhoneNumberUtils [英] Mask an EditText with Phone Number Format NaN like in PhoneNumberUtils

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

问题描述

我想让用户在 editText 中输入电话号码,以便在用户每次输入号码时动态更改格式.也就是说,当用户输入最多 4 位数字时,例如 7144,editText 显示714-4".我希望在用户输入数字时动态更新editText以格式化###-###-####.如何才能做到这一点?另外,我处理的编辑文本不止一个.

I want to make user inputted phone number in an editText to dynamically change format every time the user inputs a number. That is, when user inputs up to 4 digits, like 7144, the editText shows "714-4". I would like the editText to be dynamically updated to format ###-###-#### whenever the user inputs a digit. how can this be done? also, I am handling more than one editTexts.

推荐答案

最简单的方法是使用内置的Android PhoneNumberFormattingTextWatcher.

Easiest way to do this is to use the built in Android PhoneNumberFormattingTextWatcher.

所以基本上你在代码中得到你的 EditText 并像这样设置你的文本观察器......

So basically you get your EditText in code and set your text watcher like this...

EditText inputField = (EditText) findViewById(R.id.inputfield);
inputField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

使用 PhoneNumberFormattingTextWatcher 的好处是它会根据您的语言环境正确格式化您的号码输入.

Nice thing about using PhoneNumberFormattingTextWatcher is that it will format your number entry correctly based on your locale.

这篇关于使用电话号码格式 NaN 屏蔽 EditText,如 PhoneNumberUtils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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