在 Android 中使用 EditText 小部件屏蔽输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2912375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Masked Input Using EditText Widget in Android
提问by Buzzy
Is there a way I can specify an input mask to the EditText control in Android?
有没有办法可以为 Android 中的 EditText 控件指定输入掩码?
I want be able to specify something like ### - ## - #### for a Social Security Number. This will cause any invalid input to be rejected automatically (example, I type alphabetical characters instead of numeric digits).
我希望能够为社会安全号码指定类似 ### - ## - #### 的内容。这将导致自动拒绝任何无效输入(例如,我键入字母字符而不是数字)。
I realize that I can add an OnKeyListener and manually check for validity. But this is tedious and I will have to handle various edge cases.
我意识到我可以添加一个 OnKeyListener 并手动检查有效性。但这很乏味,我将不得不处理各种边缘情况。
Buzzy
嗡嗡声
回答by Dave Webb
Try using an InputFilter
rather than an OnKeyListener
. This means you don't have worry about tracking individual key presses and it will also handle things like pasting into a field which would be painful to handle with an OnKeyListener
.
尝试使用InputFilter
而不是OnKeyListener
。这意味着您不必担心跟踪单个按键,它还可以处理诸如粘贴到字段中的事情,这对于使用OnKeyListener
.
You could have a look at the source of the InputFilter
implementationsthat come with Android to give you a starting point for writing your own.
您可以查看Android 附带的InputFilter
实现的来源,为您编写自己的实现提供一个起点。
回答by Slava
The easiest way I know to use a mask on EditText in your Android programs in Android Studio is to use MaskedEditText
library (GitHub link).
It's a kind of custom EditText with Watcher that allows you to set a hint with different color (if you want it will be available even when user already started to type), mask and it's very easy to use :-)
我知道在 Android Studio 的 Android 程序中使用 EditText 掩码的最简单方法是使用MaskedEditText
库(GitHub 链接)。这是一种带有 Watcher 的自定义 EditText,它允许您设置具有不同颜色的提示(如果您愿意,即使用户已经开始输入,它也可用),遮罩,并且非常易于使用 :-)
compile 'ru.egslava:MaskedEditText:1.0.5'
<br.com.sapereaude.maskedEditText.MaskedEditText
android:id="@+id/phone_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:typeface="monospace"
mask:allowed_chars="1234567"
mask:mask="###-##-##"
app:keep_hint="true"
/>
And that is!
那就是!
回答by Fernando Pereira
You could take a look at the android.telephony.PhoneNumberFormattingTextWatcher class. It masks a phone number text input with the ###-###-#### pattern.
你可以看看 android.telephony.PhoneNumberFormattingTextWatcher 类。它使用###-###-#### 模式屏蔽电话号码文本输入。
回答by aaa
String phoneNumber = "1234567890";
String text = String.valueOf( android.telephony.PhoneNumberUtils.formatNumber(phoneNumber) ); //formatted: 123-456-7890
editTextMobile.setText(text); //set editText text equal to new formatted number
editTextMobile.setSelection(text.length()); //move cursor to end of editText view
Use this in an onKey
function for delicious on-the-fly bunny sex with formatted phone numbers.
将此onKey
功能用于带有格式化电话号码的美味即时兔子性爱。
回答by SatanEnglish
This sitegives a good 2 classes that help with masking that I found quite useful.
这个站点提供了一个很好的 2 个类,它们有助于我发现非常有用的屏蔽。
You need to use the items in the comments to improve it. But worth adding to your program for ease of masking your EditText views for many masks.
您需要使用评论中的项目来改进它。但值得添加到您的程序中,以便为许多掩码屏蔽 EditText 视图。