如果启用了错误,如何检查 TextInputLayout
How to check TextInputLayout if error is enabled
如果 editText1 是焦点并且 editTextWrapper 已经显示错误并且我将 focus 移开(例如到 editText2)errorMessage 会闪烁一点(因为 focusListener 再次启用它)并且会有小的动画故障。
有没有办法检查是否启用了 TextInputLayout 错误(或其他知道显示错误的方法,除了向我的 class 引入外部验证?
public class NoteCreateFragment extends Fragment{
@Bind(R.id.edit_text1) EditText editText1;
@Bind(R.id.edit_text2) EditText editText2;
@Bind(R.id.edit_text_wrapper) TextInputLayout editTextWrapper;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
){
View view = inflater.inflate(R.layout.fragment_note_create, container, false);
ButterKnife.bind(this, view);
editText1.addTextChangedListener(textWatcher);
editText1.setOnFocusChangeListener(focusListener);
return view;
}
@Override
public void onDestroyView(){
super.onDestroyView();
ButterKnife.unbind(this);
}
private void createTask(){
String text1 = editText1.getText().toString();
if(text1.isEmpty()){
editTextWrapper.setError("Please enter task name");
}
}
private TextWatcher textWatcher = new 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){
if(s.length() > 3){ editTextWrapper.setErrorEnabled(false); }
}
@Override
public void afterTextChanged(Editable s){
}
};
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if(!hasFocus && editText1.getText().toString().length() < 1){
editTextWrapper.setErrorEnabled(true);
editTextWrapper.setError("Please enter task name");
Log.e("TextInputLayout", editTextWrapper.getEditText().getError().toString());
}
}
};
}
不要在运行时将 setErrorEnabled()
更改为 show/hide 错误。使用 setError(message)
显示错误,使用 setError(null)
隐藏错误。因此
if(!TextUtils.isEmpty(textInputLayout.getError())
return 如果错误可见则为 true,否则为 flase
如果 editText1 是焦点并且 editTextWrapper 已经显示错误并且我将 focus 移开(例如到 editText2)errorMessage 会闪烁一点(因为 focusListener 再次启用它)并且会有小的动画故障。
有没有办法检查是否启用了 TextInputLayout 错误(或其他知道显示错误的方法,除了向我的 class 引入外部验证?
public class NoteCreateFragment extends Fragment{
@Bind(R.id.edit_text1) EditText editText1;
@Bind(R.id.edit_text2) EditText editText2;
@Bind(R.id.edit_text_wrapper) TextInputLayout editTextWrapper;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
){
View view = inflater.inflate(R.layout.fragment_note_create, container, false);
ButterKnife.bind(this, view);
editText1.addTextChangedListener(textWatcher);
editText1.setOnFocusChangeListener(focusListener);
return view;
}
@Override
public void onDestroyView(){
super.onDestroyView();
ButterKnife.unbind(this);
}
private void createTask(){
String text1 = editText1.getText().toString();
if(text1.isEmpty()){
editTextWrapper.setError("Please enter task name");
}
}
private TextWatcher textWatcher = new 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){
if(s.length() > 3){ editTextWrapper.setErrorEnabled(false); }
}
@Override
public void afterTextChanged(Editable s){
}
};
private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if(!hasFocus && editText1.getText().toString().length() < 1){
editTextWrapper.setErrorEnabled(true);
editTextWrapper.setError("Please enter task name");
Log.e("TextInputLayout", editTextWrapper.getEditText().getError().toString());
}
}
};
}
不要在运行时将 setErrorEnabled()
更改为 show/hide 错误。使用 setError(message)
显示错误,使用 setError(null)
隐藏错误。因此
if(!TextUtils.isEmpty(textInputLayout.getError())
return 如果错误可见则为 true,否则为 flase