问题描述
有没有一种方法,我可以指定一个输入掩码的EditText上控制Android的?
Is there a way I can specify an input mask to the EditText control in Android?
我希望能够指定类似### - ## - ####为一个社会安全号码。这将导致任何无效的输入被自动拒绝(例如,I型英文字母而不是数字组成)。
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.
嗡嗡
推荐答案
尝试使用的<$c$c>InputFilter$c$c>而不是 OnKeyListener
。这意味着你不必担心跟踪单个键presses,它也将处理之类的东西粘贴到这将是痛苦的处理与 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
.
您可以<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/text/InputFilter.java;h=2f55677ee24fae3bfcb487f357835f7df53bb130;hb=HEAD">have一起来看看输入过滤
实施附带机器人给你一个起点,编写自己的源代码。
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小工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!