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("");
}
}
}
根據你的設計,你可能想看看'TextInputLayout' https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html –