简体   繁体   English

如果用户输入带字符串的int,则Onclick会验证edittext

[英]Onclick validate edittext if user enters an int with a string

I have in my application an EditText which validates that the user cannot enter a integer when the user clicks a button. 我的应用程序中有一个EditText,它可以验证用户单击按钮时用户是否不能输入整数。

This is done by an if statement on a condition where word is a String: 这是通过if语句在word为字符串的条件下完成的:

(word.matches("[0-9]*") (word.matches(“ [0-9] *”)

How can I change this if a user enters a word with a string as well. 如果用户也输入带有字符串的单词,该如何更改呢?

Thanks 谢谢

An EditText inherits from a TextView , which has a method called setInputType . EditText继承自TextView ,后者具有一个名为setInputType的方法。 This method accepts an int defined as InputType . 此方法接受定义为InputType的int。 So you can set the InputType dynamically depending on what you want/the user clicks 因此,您可以根据需要/用户单击InputType动态设置InputType

To do this kind of filter in EditText, the better way is to specify the android:inputType . 为了在EditText中进行这种过滤,更好的方法是指定android:inputType This allow you to specify what you expect and also to display the right keyboard. 这使您可以指定期望的内容并显示右键盘。 Another option is to use android:digit to specify the autorized characters. 另一种选择是使用android:digit来指定自动字符。 If you need numeric, enter 如果需要数字,请输入

android:digit="0123456789" android:digit =“ 0123456789”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM