2016-06-23 47 views
0

我正在Android中創建表單,並且我想知道如何在用戶正確輸入兩個密碼時插入複選標記。我將使用內置的可繪製對勾在這個崗位(How to access checkmark drawable in Android OS?將複選標記添加到edittexted字段

//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); 
    } 
} 

what I want to do

回答

0

嘗試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

+0

嗨我打算把這個功能放到方法isTwoPasswordsMatching()中,我應該在那裏實現這個功能嗎? – user2353565

相關問題