問題描述
我是安卓新手我正在嘗試為一個項目編寫應用程序.
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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!