android - 显示验证消息

标签 android validation android-edittext

我有一个 XML 布局文件,其中包含一个 EditText 和一个 Button。我想显示以下验证消息以向用户提供反馈:

You must enter 4 numbers

完成此任务的最佳方法是什么?

最佳答案

根据我的经验,这是最好的方法:

EditText yourEditText;

// when you detect an error:
yourEditText.setError("Input must be 4 digits and numeric");

结果是:

enter image description here

此外,如果输入必须是数字,请在 EditText 定义中使用 android:inputType="numberSigned"。这样设备将不允许用户输入非数字值;更好的是,它会显示一个特殊的键盘来执行此操作:

enter image description here

关于android - 显示验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5082918/

相关文章:

android - 无法在Android Studio 2.1上启动模拟器

javascript - 向动态表单添加验证

java - 在 Android 应用程序中使用属性文件时出现 NullPointerException

android - 如何向 Facebook 好友发送应用邀请(非游戏应用)。?

用于 htc 渴望高清的 android usb adb 驱动程序

android - 编辑文本光标位置

java - NullPointerException - edittext.gettext()

javascript - 如何使用 jQuery 将正则表达式应用于输入?

java - 如何制作一个包含多个帐户的帐户验证程序?

java - 以编程方式向 EditText 添加引力