2017-07-17 13 views
-7

ユーザーがアンドロイドアプリケーションでパスワードを入力しているときにそのメッセージを表示したい場合は、パスワードが短すぎます。入力中にメッセージを表示する

たとえば、ユーザーが6文字のパスワードを入力するまで、「パスワードが短すぎます」とパスワードを入力すると、ユーザーに表示されます。

答えて

1

これはあなたの活動は、このようなものである必要があり、XMLレイアウトファイルのテキスト入力レイアウト

Something like this

<android.support.design.widget.TextInputLayout 
       android:id="@+id/input_layout_password" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
       <EditText 
        android:id="@+id/textView_password" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="@string/password" 
        android:inputType="textPassword" /> 
</android.support.design.widget.TextInputLayout> 

です。

passwordTIL = (TextInputLayout) findViewById(R.id.input_layout_password); 
passwordET = (EditText) findViewById(R.id.textVIew_password); 


passwordET.addTextChangedListener(new SigninTextWatcher(passwordET) 

//you can use this for username too or to check if the email format is correct or not. 

private class SigninTextWatcher implements TextWatcher { 
     private View view; 

     private SigninTextWatcher(View view) { 
      this.view = view; 
     } 

     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
     } 

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

     public void afterTextChanged(Editable editable) { 
      switch (view.getId()) { 
       case R.id.textView_password: 
        validatePassword(); 
        break; 
      } 
     } 
    } 


    private boolean validatePassword() { 
     if (passwordET.getText().toString().trim().isEmpty()) { 
      passwordTIL.setError("Empty error message"); 
      requestFocus(passwordET); 
      return false; 
     } else if(passwordET.getText().toString().length() < 6){ 
       passwordTIL.setError("Short password error message"); 
       requestFocus(passwordET); 
       return false; 
     }else { 
       passwordTIL.setErrorEnabled(false); 
      } 
      return true; 
     } 

あなたはdownvoterは、理由を説明してもらえ/無効ログインボタンあまりに

0

編集テキストに添付されたtextChangedListenerを使用できます。 簡単な例を参照してください:

Field1.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void afterTextChanged(Editable s) { 
    if(s.length() < 6) 
     // show message too short 
    }} 

    @Override  
    public void beforeTextChanged(CharSequence s, int start, 
    int count, int after) { 
    } 

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

    }); 
0

使用を

et_password.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      if(charSequence.length()<6){ 
       et_password.setError("The password must contain 6 characters"); 
      } 

     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
    }); 
+1

を有効にするvalidatePassword()関数を利用することができますか? –

0

あなたのEditTextにaddTextChangedListenerを()を追加します。

EditText password = (EditText) findViewById(R.id.password_et); 

    password.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
     } 

     @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { 
      if((charSequence.toString()).length < 6){ 
      Toast.makeText(this, "Password length less than 6", Toast.LENGTH_SHORT).show(); 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable editable) { 

     } 
}); 
関連する問題