2016-01-19 29 views
1

TextInputLayout支持库中有一些错误的Android

我有一个注册表格中,我使用TextWatcher做验证。我用支持库的TextInputLayout突出错误

我的问题

当我EditText我输入不正确的数据,该TextInputLayout令我EditTextred颜色

然后,当我把正确的在我EditText输入,然后更改focus到其他领域,仍是第一EditText是红色不改变正常editext背景

我试过

我试图设置我的EditText编程到我的自定义绘制的背景,但它给我的其他的外观和感觉(其它颜色)

也试过只改变HintColorTextInputLayout红不使用setError()方法,仍然没有结果

我的代码

1的方法:给我一些其他颜色的背景

public boolean validateFirstName() { 
     mFirstName = edFirstName.getText().toString().trim(); 
     if (mFirstName.isEmpty()) { 
      tiFirstName.setError(mEmptyFields); 
      edFirstName.requestFocus();   
      return false; 
     } else { 
      tiFirstName.setError(null); 
      tiFirstName.setErrorEnabled(false); 
      edFirstName.setBackground(getResources().getDrawable(R.drawable.ed_line_bg)); 
     } 
     return true; 
    } 

第二个方法:看到任何变化:(

public boolean validateFirstName() { 
     mFirstName = edFirstName.getText().toString().trim(); 
     if (mFirstName.isEmpty()) { 
      edFirstName.requestFocus(); 
      changeTextInputLayoutColorToRed(tiFirstName); 
      return false; 
     } else { 
      changeTextInputLayoutColorToNormal(tiFirstName); 
      edFirstName.setBackground(null); 
     } 
     return true; 
    } 



public void changeTextInputLayoutColorToRed(TextInputLayout textInputLayout){ 
     int redColor=getResources().getColor(android.R.color.holo_red_dark); 
     // textInputLayout.getEditText().setHighlightColor(redColor); 
     textInputLayout.getEditText().setHintTextColor(redColor); 
     textInputLayout.setError(mErrorMessage); 
     textInputLayout.setErrorEnabled(true); 
    } 

    public void changeTextInputLayoutColorToNormal(TextInputLayout textInputLayout){ 
     int normalColor=getResources().getColor(R.color.text_color); 
     //textInputLayout.getEditText().setHighlightColor(normalColor); 
     textInputLayout.getEditText().setHintTextColor(normalColor); 
     textInputLayout.setError(null); 
     textInputLayout.setErrorEnabled(false); 
    } 

回答

0

我使用TextWatcher如下面我验证:

private class RegisterTextWatcher implements TextWatcher { 
@Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

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

    @Override 
    public void afterTextChanged(Editable s) { 

     switch (editText.getId()){ 

      case R.id.name_input: 
       validateName(); 
       break; 
      ... 
     } 
    } 
} 

我将它添加到我的editText onCreate活动方法:

inputName.addTextChangedListener(new RegisterTextWatcher(inputName)); 

而我的验证:

public boolean emptyField(String editText, TextInputLayout textInputLayout, String errorMessage){ 

     if(editText.trim().isEmpty()){ 

      setLayoutError(textInputLayout, errorMessage); 

      return false; 

     }else{ 

      removeError(textInputLayout); 
      return true; 
     } 
    } 


private void setLayoutError(TextInputLayout textInputLayout, String error){ 

    if(!textInputLayout.isErrorEnabled()){ 
     textInputLayout.setErrorEnabled(true); 
     textInputLayout.setError(error); 
     textInputLayout.getEditText().setTextColor(context.getResources().getColor(android.R.color.holo_red_dark)); 
    } 
} 

private void removeError(TextInputLayout textInputLayout){ 
    if(textInputLayout.isErrorEnabled()){ 
     textInputLayout.setErrorEnabled(false); 
     textInputLayout.getEditText().setTextColor(context.getResources().getColor(android.R.color.black)); 
    } 
} 
+0

看起来几乎与我相似的代码......有什么实际的区别? –

+0

猜猜它是与你的TextWatcher或更改焦点,当你做一些事情。这对我的作品 –

+0

让我试试这个兄弟:) –