Masked Input Using EditText Widget in Android(在 Android 中使用 EditText 小部件的屏蔽输入)
问题描述
有没有一种方法可以为 Android 中的 EditText 控件指定输入掩码?
Is there a way I can specify an input mask to the EditText control in Android?
我希望能够为社会安全号码指定类似 ### - ## - #### 的内容.这将导致自动拒绝任何无效输入(例如,我输入字母字符而不是数字).
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).
我意识到我可以添加一个 OnKeyListener 并手动检查有效性.但这很乏味,我将不得不处理各种边缘情况.
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.
嗡嗡声
推荐答案
尝试使用 InputFilter
而不是 OnKeyListener
.这意味着您不必担心跟踪单个按键,它还可以处理诸如粘贴到字段中之类的事情,而使用 OnKeyListener
来处理这些事情会很痛苦.
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
.
你可以看看 Android 附带的 InputFilter
实现的源代码,为您提供编写自己的起点.
You could have a look at the source of the InputFilter
implementations that come with Android to give you a starting point for writing your own.
这篇关于在 Android 中使用 EditText 小部件的屏蔽输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Android 中使用 EditText 小部件的屏蔽输入


- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01