2017-03-17 62 views
0

我想一個TextView,當用戶在按鍵出現,這是我的代碼試圖讓文字顯示,同時用戶輸入密碼的Android工作室

TextView mustContain, minChar, alphaNumeric; 

這3種不同的TextViews有出現基於特定條件。只要

minChar應顯示爲用戶已鍵入小於8個字符, mustContain應該出現如果用戶鍵入口令不大寫或小寫字符 alphaNumeric應該只要顯示爲用戶沒有輸入至少一個數字

public class AccountDetails extends AppCompatActivity implements TextWatcher 

    @Override 
     public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) { 

      } 

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

      final String validatePassword = password.getText().toString(); 

      if (validatePassword.length()<8 || validatePassword.length()>24){ 
       minChar.setText("minimum 8 characters, up to 24 characters"); 
       minChar.setTextColor(Integer.parseInt("#ff0000")); 
      } else if (validatePassword.contains("^(?=.*?[A-Z])(?=.*?[a-z]).{8,}$")){ 

       mustContain.setText("at least one upper-case and lower-case"); 
       mustContain.setTextColor(Integer.parseInt("#ff0000")); 

      } else if (validatePassword.contains("(?=.*?[0-9])")){ 

       alphaNumeric.setText("contain alphanumeric"); 
       alphaNumeric.setTextColor(Integer.parseInt("#ff0000")); 
      } else { 
       minChar.setText(""); 
       mustContain.setText(""); 
       alphaNumeric.setText(""); 
      } 

     } 

    @Override 
    public void afterTextChanged(Editable editable) { 
     final String validatePassword = password.getText().toString(); 

     if (validatePassword.length()<8 || validatePassword.length()>24){ 
      minChar.setText("minimum 8 characters, up to 24 characters"); 
      minChar.setTextColor(Integer.parseInt("#ff0000")); 
     } else if (validatePassword.contains("^(?=.*?[A-Z])(?=.*?[a-z]).{8,}$")){ 


      mustContain.setText("at least one upper-case and lower-case"); 
      mustContain.setTextColor(Integer.parseInt("#ff0000")); 

     } else if (validatePassword.contains("(?=.*?[0-9])")){ 

      alphaNumeric.setText("contain alphanumeric"); 
      alphaNumeric.setTextColor(Integer.parseInt("#ff0000")); 
     } else { 
      minChar.setText(""); 
      mustContain.setText(""); 
      alphaNumeric.setText(""); 
     } 
    } 


    } 
+0

根據你的設計,你可能想看看'TextInputLayout' https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html –

回答

1

只需使用setVisibility()函數。初始化textViews對false的可見性,並在完成編碼時將可見性設置爲true。例如:

minChar.setVisibility(View.INVISIBLE); 
if (validatePassword.length()<8 || validatePassword.length()>24){ 
      minChar.setText("minimum 8 characters, up to 24 characters"); 
      minChar.setTextColor(Integer.parseInt("#ff0000")); 
      minChar.setVisibility(View.VISIBLE); 
} 
+0

感謝想法集視圖無形。但這不起作用,我不知道爲什麼。我想知道我是否在這裏的某個地方犯了一個錯誤。 – leyreyyan

相關問題