Validating edittext in Android(在Android中验证edittext)
问题描述
我是安卓新手我正在尝试为一个项目编写应用程序.
I'm new to android & I'm trying to write an application for a project.
我需要检查用户是否在编辑文本中输入了 7 个数字和一个字母.示例:0000000x
I need to check whether the user has entered 7 numbers followed by one alphabet in edittext. Example: 0000000x
我该怎么做?蒂亚!:)
How should I do that? TIA! :)
推荐答案
可能最好的方法是使用 TextWatcher 传入 addTextChangedListener() 方法.这是一个使用示例:
Probably the best approach would be to use a TextWatcher passed into the addTextChangedListener() method of the EditText. Here is an example use:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable e) {
String textFromEditView = e.toString();
validateText(textFromEditView);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing needed here...
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//nothing needed here...
}
});
我将把 validateText(String) 方法的实现留给读者作为练习,但我想它应该足够简单.我会使用:
I will leave the implementation of the validateText(String) method as an exercise for the reader, but I imagine it should be easy enough. I would either use:
- 一个简单的正则表达式.
- 或者由于这种情况很简单,检查字符串的长度是否为 8,并检查每个字符.有一个简单的实用程序类来检查字符的特征.Character.isDigit(char) 和 Character.isLetter(char)
这篇关于在Android中验证edittext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Android中验证edittext
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
