EditText 有内置的过滤选项吗?
是否可以将过滤器(我不确定它的名称)附加到 EditTexts,以便如果用户以错误的方式键入某些内容,则 EditText 上会弹出某种警告?
例如:表单上有一个 EditText,必须填写它。因此,在它为空之前,其中会显示一些警告,表明它是表单上的必填字段。
我在 Android 上的 EditTexts 上看到过类似的行为,所以我猜有一个内置功能,但我找不到它。有人可以帮我吗?
目标 Android 版本为 3.0 及更高版本。
Is it possible to attach filters (I'm not sure about the name of this) to EditTexts, so that if the user types in something in the wrong manner, than a warning of some sort pops up on the EditText?
For example: there's an EditText on a form and it must be filled. So until it's empty, some warning is displayed in it, showing that it's a required field on the form.
I've seen similar behaviour on EditTexts all around Android, so I guess there's a built-in feature for this, but I can't find it. Could somebody help me out please?
Target Android version is 3.0 and up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要添加 TextWatcher 和在
public void afterTextChanged(Editable s)
的实现中做出相应的反应。只需检查s
是否为空,或者是否正确。您也可以实现过滤器,但是那些通常是为了防止用户输入错误的数据。
You may want to add a TextWatcher and react accordingly in your implementation of
public void afterTextChanged(Editable s)
. Just check ifs
is empty or not, or if it's correct or not.You can implement filters also, but those are usually to prevent the user to entering wrong data.
查看xml中
EditText
的android:inputType
属性。例如android:inputType="textEmailAddress|number|phone"
和其他内容。但是为了检查空性,除了
if (editText.getText().length() > 0)
之外,我无法建议任何其他内容。Look at
android:inputType
attribute ofEditText
in xml. For exampleandroid:inputType="textEmailAddress|number|phone"
and other stuff.But for checking emptiness I can't suggest anything but
if (editText.getText().length() > 0)
.您可以使用带有文本观察器事件的代码添加过滤器。当您在编辑文本上输入任何想法时,文本观察器事件将被执行,然后我们可以检查编辑文本中的类型文本,如果条件为假,那么您应该显示弹出窗口。我展示了一个在文本观察器事件的帮助下使用编辑文本在列表视图中进行过滤的示例,您应该在编辑文本中进行过滤。
ed.addTextChangedListener(new TextWatcher() {
You can add filter using code with Text watcher event. When you type any think on Edit Text the text watcher event is execute then we are able to check the type text in edit text if condition is false then you should show popup . I am showing a example for filtering in list view using edit text with the help of text watcher event you should you it for filtering in edittext.
ed.addTextChangedListener(new TextWatcher() {