Добавление галочки в поле edittexted
Я делаю форму в Android, и я хочу знать, как мне поставить галочку, когда пользователь вводит оба пароля правильно. Я собираюсь использовать встроенную пометку Drawable, как описано в этом посте ( Как получить доступ к пометке Drawable в ОС Android?)
//variables
private EditText inputName, inputEmail, inputPassword, inputPasswordConfirm;
private TextInputLayout inputLayoutName, inputLayoutEmail, inputLayoutPassword, inputLayoutConfirmPassword;
private Button btnNextSignUp; //button
private static final String PASSWORD_PATTERN = ".{6,20}"; //regex expression for password validatoin
private Pattern patternToMatch;
private Matcher matcherToCheck;
private boolean validatePassword(){
String passwordString = inputPassword.getText().toString().trim();
//Check for if field is empty and password is valid meaning
// it has to be greater than 6 characters
if (passwordString.isEmpty() || !isPasswordValid(passwordString)){
inputLayoutPassword.setError("Enter password greater than 6 characters");
requestFocus(inputEmail);
return false;
}
else{
//set error layout to false if password has been filled with characters
inputLayoutPassword.setErrorEnabled(false);
}
return true;
}
/**
* helper method make sure password is greater than 6
* @param passwordEntered for validation
* @return true valid password, false invalid password
*/
private boolean isPasswordValid(String passwordEntered){
patternToMatch = Pattern.compile(PASSWORD_PATTERN);
matcherToCheck = patternToMatch.matcher(passwordEntered);
return matcherToCheck.matches();
}
/**
* isTwoPasswordsMatching
* @param none
* @returns returns confirmed password match
* check the two passwords are equal to each other
* */
private boolean isTwoPasswordsMatching(){
//password field one
String passwordString = inputPassword.getText().toString().trim();
//password field two confirm password field --> convert to string
String passwordConfirmString = inputPasswordConfirm.getText().toString().trim();
if (passwordString.equals(passwordConfirmString)){
//field is valid add check mark or change underline field to green
}
}
//get error to appear
private void requestFocus(View view){
if (view.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
1 ответ
Попробуй setCompoundDrawables
Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
edit.setCompoundDrawables(drawable, null, null, null);
Вы также можете использовать
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.drawableRight, 0);
Для получения дополнительной информации см. Android Docs.