Android中验证的EditText [英] Validating edittext in Android
问题描述
我是新到Android和放大器;我想写一个项目的申请。
I'm new to android & I'm trying to write an application for a project.
我需要检查用户是否已经进入了7号随后在EditText上1字母。 示例:0000000x
I need to check whether the user has entered 7 numbers followed by one alphabet in edittext. Example: 0000000x
我应该怎么办呢? TIA! :)
How should I do that? TIA! :)
推荐答案
也许最好的办法是使用的 TextWatcher 传递到<一个href="http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29"相对的EditText上的=nofollow的> 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(串)的实施
方法作为一个练习的读者,但我想这应该很容易。我要么使用:
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:
- 在一个简单的正前pression。
- 或者因为这种情况下是很容易的,检查该串的长度为8,和审查每个字符。有一个简单的工具类检查字符的特性。 Character.isDigit(炭)和<一HREF =http://developer.android.com/reference/java/lang/Character.html#isLetter%28char%29相对=nofollow> Character.isLetter(字符)
这篇关于Android中验证的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!