我正在嘗試在android中查找表單驗證庫。有這樣的事嗎?Android:表單驗證庫
我有一個註冊表單,我想驗證其字段。如果用戶輸入了無效數據,我想在該字段的右側輸入一個紅色警告標記,並彈出一個工具提示,輸入無效數據。
我瞭解android:inputType
但這不是我想要
我正在嘗試在android中查找表單驗證庫。有這樣的事嗎?Android:表單驗證庫
我有一個註冊表單,我想驗證其字段。如果用戶輸入了無效數據,我想在該字段的右側輸入一個紅色警告標記,並彈出一個工具提示,輸入無效數據。
我瞭解android:inputType
但這不是我想要
我不知道任何這樣的庫什麼。但是,如果你與EditTexts工作,那麼你最好的選擇是使用自定義TextWatcher:
class TextCheck implements TextWatcher
{
private EditText editor;
public TextCheck(EditText editor)
{
this.editor = editor;
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// check the text, and if the user entered
// something wrong, change your edittext
if(something wrong)
{
editor.setBackgroundColor(Color.RED); //for example
}
}
@Override
public void afterTextChanged(Editable arg0){}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}
}
然後你就可以使用它在你所有的EditTexts像
EditText editor = (EditText) findViewById(...your id...);
editor.addTextChangedListener(new TextCheck(editor));
非常感謝...它幫助了我很多我的驗證。 – 2012-08-08 22:19:29
我很高興它幫助:) – 2012-08-09 07:30:13
我做了類似的事情。您可以改進此代碼並根據您的需要進行調整。
EditTextWithValidation.java
public class EditTextWithValidation extends EditText implements OnTouchListener {
private EditTextValidator mValidator;
public EditTextWithValidation(Context context) {
super(context);
initialize();
}
public EditTextWithValidation(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public EditTextWithValidation(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
public EditTextValidator getCustomValidator() {
return mValidator;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
setError(null);
return false;
}
private void initialize() {
mValidator = new EditTextValidator(this);
setOnTouchListener(this);
}
}
EditTextValidator.java
用法:
mSignupEmail = (EditTextWithValidation) root.findViewById(R.id.signup_email);
mSignupEmail.getCustomValidator().setValidationRules(
"[a-zA-Z0-9_-]+(?:\\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9_-]+)*\\.(?:[a-zA-Z]{2,})",
R.string.email_answer_validation_msg,
false);
mSignupEmail.getCustomValidator().setValidLength(0, 50);
…
mSignupPassword = (EditTextWithValidation) root.findViewById(R.id.signup_password);
mSignupPassword.getCustomValidator().setValidationRules(
"[[email protected]#$%^&*()]{6,20}",
R.string.password_validation_msg,
false);
…
mSignupConfirmPassword = (EditTextWithValidation) root.findViewById(R.id.signup_confirm_password);
mSignupConfirmPassword.getCustomValidator().setAllowEmpty(true);
mSignupConfirmPassword.getCustomValidator().shouldMatch(mSignupPassword, R.string.password_mismatch);
mSignupConfirmPassword.getCustomValidator().setValidationErrorMsgId(R.string.password_validation_msg);
…
if (mSignupEmail.getCustomValidator().validate() && mSignupPassword.getCustomValidator().validate() && mSignupConfirmPassword.getCustomValidator().validate()) {
// DO SOMETHING
}
非常感謝你 – 2012-08-11 11:10:42
有這樣的事嗎?
啊..是的,有一個,你可以找到它here。
它確實爲您形成驗證,使用但不限於註釋。要了解圖書館的功能,請訪問以下answer on SO,其中描述了圖書館的用途。
如果你想寫新的規則,你總是可以擴展Rule類。 PS:我是圖書館的作者。
安裝此應用程序:https://play.google.com/store/apps/details?id=com.desarrollodroide.repos
轉到:Utils
- >Android-Validator
- >View Demo
有在此應用程序很多其他的酷庫爲好。好的是,如果你喜歡它,你可以查看每個庫的演示,並獲得該特定庫的鏈接到github repo。這非常有用。
我知道這是舊的,但你可以試試這個優秀Android Validation library和訪問這個Stackoverflow reference這Stackoverflow reference使用情況的示例,因爲我發現的主要圖書館的入門指南還挺很難理解。
'有沒有這樣的事情?'沒有...手動檢查字符串長度 – 2012-08-08 09:09:48