我是安卓新手我正在尝试为一个项目编写应用程序.
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:
这篇关于在Android中验证edittext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在android中剪切、复制、粘贴Cut, copy, paste in android(在android中剪切、复制、粘贴)
android EditText 融入背景android EditText blends into background(android EditText 融入背景)
更改 EditText 的线条颜色 - AndroidChange Line Color of EditText - Android(更改 EditText 的线条颜色 - Android)
EditText 始终显示带 2 位小数的数字EditText showing numbers with 2 decimals at all times(EditText 始终显示带 2 位小数的数字)
更改光标在展开的 EditText 中的开始位置Changing where cursor starts in an expanded EditText(更改光标在展开的 EditText 中的开始位置)
android中的EditText,adjustPan,ScrollView问题EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView问题)