我正在构建一个包含许多Edittexts的应用程序,我最终会一遍又一遍地重复相同的代码。
对于每个Edditext我隐藏提示并在其上方设置一个标签,我为所有这些设置了相同的错误,这对我来说似乎是浪费..
反正我是否可以编写代码并将其应用于所有Edittexts?
例如:edittext.setAllErrors(); 或者我可能不熟悉的任何其他方式?
我为几乎所有Edittext重复的代码示例:
firstNameET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus || isFull(firstNameET.getText().toString()) == false) {
firstNameET.setHint("");
firstNameLabel.setVisibility(TextView.VISIBLE);
//letter checker
if(charCheck(firstNameET.getText().toString().trim()) == false && !firstNameET.getText().toString().trim().matches("")){
firstNameET.setError("Only letters are allowed");
}
if(lengthCheck(firstNameET.getText().toString().trim()) == 0 && focusCount > 1){
firstNameET.setError("Please Insert Name");
}else if(lengthCheck(firstNameET.getText().toString().trim()) == 2){
firstNameET.setError("Name is not long enough.");
}
}
else{
firstNameET.setHint("First Name");
{
firstNameLabel.setVisibility(TextView.INVISIBLE);
}
}
}
});