2015-10-12 4 views
10

私はアンドロイドモバイル開発(Androidスタジオネイティブ開発 - 新しい知識のため)には初めてです。ここでは、入力検証のベストプラクティスに関する質問をしたいと思います。 私たちが知る限り、開発者は入力フォームを開発します。テキストフィールドへの間違った入力をキー入力しないようにする必要があります。だから私の質問ですベストプラクティス:入力検証(Android)

  1. 私たちは検証目的のために1つのJavaファイルを作成できますか?すべての入力フォームは、その1つの検証ファイルにのみ移動する必要があります(1つのアプリケーションで多くの入力ページ画面の場合)。 YESの場合、学習テクニックの例/リンク/チュートリアルはどのように取得できますか? NOの場合、なぜですか?

個人的には、この手法を実装する方法が必要です。そのため、各Javaファイルに対して、同じコードをもう一度繰り返し使用する必要はありません(クリーンコードという言葉で言えば)。残念ながら、私はそれのための例やチュートリアルは見つかりませんでした。たぶん私は間違ったキーワードや誤解を検索します。そのような手法が存在しない場合、入力検証のベストプラクティスは何ですか?

ありがとうございました。

p/s:このスレッドは、ベストプラクティスでより良い方法を見つけるためのものです。ありがとうございました。

+0

InputValidatorHelper inputValidatorHelper = new InputValidatorHelper(); StringBuilder errMsg = new StringBuilder("Unable to save. Please fix the following errors and try again.\n"); //Validate and Save boolean allowSave = true; if (user.getEmail() == null && !inputValidatorHelper.isValidEmail(user_email)) { errMsg.append("- Invalid email address.\n"); allowSave = false; } if (inputValidatorHelper.isNullOrEmpty(user_first_name)) { errMsg.append("- First name should not be empty.\n"); allowSave = false; } if(allowSave){ //Proceed with your save logic here } 

あなたはEditText#addTextChangedListener

例を介して結合しているTextWatcherを使用して検証を呼び出すことができます-edittext-validatorとhttps://github.com/thyrlian/AwesomeValidation – Neil

答えて

11

このJavaクラスは、テキストに行われた変更見て、あなたのエディットテキストを「見る」ためにTextWatcherを実装しています

public abstract class TextValidator implements TextWatcher { 
    private final TextView textView; 

    public TextValidator(TextView textView) { 
     this.textView = textView; 
    } 

    public abstract void validate(TextView textView, String text); 

    @Override 
    final public void afterTextChanged(Editable s) { 
     String text = textView.getText().toString(); 
     validate(textView, text); 
    } 

    @Override 
    final public void 
    beforeTextChanged(CharSequence s, int start, int count, int after) { 
     /* Needs to be implemented, but we are not using it. */ 
    } 

    @Override 
    final public void 
    onTextChanged(CharSequence s, int start, int before, int count) { 
     /* Needs to be implemented, but we are not using it. */  
    } 
} 

そして、あなたのEditTextでは、あなたはそのリスナー

にそのテキストウォッチャーを設定することができます(私が使用しています)
editText.addTextChangedListener(new TextValidator(editText) { 
    @Override public void validate(TextView textView, String text) { 
     /* Insert your validation rules here */ 
    } 
}); 
+0

ありがとう、非常に良い解決策。 – sandeepmaaram

+1

コピー:https://stackoverflow.com/a/11838715/1112963 – Zon

5

一つのアプローチは、次のような入力検証するためのヘルパーが必要です。

  1. NULLかどうか(あるいは空虚)
  2. 日付
  3. パスワード
  4. メール
  5. 数値
  6. など

ここに私のValidationHelperクラスからの抜粋です:

public class InputValidatorHelper { 
    public boolean isValidEmail(String string){ 
     final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 
     Pattern pattern = Pattern.compile(EMAIL_PATTERN); 
     Matcher matcher = pattern.matcher(string); 
     return matcher.matches(); 
    } 

    public boolean isValidPassword(String string, boolean allowSpecialChars){ 
     String PATTERN; 
     if(allowSpecialChars){ 
      //PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})"; 
      PATTERN = "^[[email protected]#$%]\\w{5,19}$"; 
     }else{ 
      //PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})"; 
      PATTERN = "^[a-zA-Z]\\w{5,19}$"; 
     } 



     Pattern pattern = Pattern.compile(PATTERN); 
     Matcher matcher = pattern.matcher(string); 
     return matcher.matches(); 
    } 

    public boolean isNullOrEmpty(String string){ 
     return TextUtils.isEmpty(string); 
    } 

    public boolean isNumeric(String string){ 
     return TextUtils.isDigitsOnly(string); 
    } 

    //Add more validators here if necessary 
} 

今私がこのクラスを使う方法はこれです:実際には、いくつかのlibsはすでにhttps://github.com/vekexasia/androidある

txtName.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     //Do nothing 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     validate(); 
    } 
}); 
+0

リストされているすべてのTextViewタイプのAndroid専用の入力タイプがあります。なぜ検証が必要ですか? – Zon