此处的目标是在用户输入联系信息时验证每个字段,并最终将自动格式(大小写,拼写等)应用于每个字段。
我使用以下代码几乎可以使用它,但它基于一个按键回调,我真正想要触发的是每个文本字段的“失去焦点”。有什么想法吗?
public TextFormValidationCallback (CustomerFormActivity faja, String editTextName, Context context, EditText textField,
Button button, int rule) {
this.button = button;
this.textField = textField;
this.context = context;
this.ruleResourceId = rule;
this.editTextName = editTextName;
this.faja = faja;
this.res = context.getResources();
}
public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
if (event != null) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Log.i("TextFormValidation",
"TextFormValidation callback activated!");
if (validateTextFieldNotNullData(this.textField)
&& (validateTextFieldFormat(this.textField,
ruleResourceId))) {
faja.setFieldValidationArray(getHeadingIndex(editTextName),
true);
if (faja.sumFieldValidations()) {
button.setClickable(true);
button.setText(R.string.addCustomer);
}
} else {
button.setText(R.string.next);
button.setClickable(false);
}
return true;
}
} else {
}
return false;
}
答案 0 :(得分:0)
这可能是你想要的吗? TextView.onWindowFocusChanged()
答案 1 :(得分:0)
好的,它不会让我昨天发布这个,但我想通了。为了从这个facepalm时刻保存其他人,我将在这里发布固定代码。
public class TextFormValidationCallback implements OnFocusChangeListener {
private Button button;
private EditText textField;
private Integer ruleResourceId;
private Resources res;
private Context context;
private String editTextName;
private CustomerFormActivity faja;
public TextFormValidationCallback (CustomerFormActivity faja, String editTextName, Context context, EditText textField,
Button button) {
this.button = button;
this.textField = textField;
this.context = context;
this.editTextName = editTextName;
this.res = context.getResources();
this.ruleResourceId = R.string.validates_everything;
this.faja = faja;
}
public TextFormValidationCallback (CustomerFormActivity faja, String editTextName, Context context, EditText textField,
Button button, int rule) {
this.button = button;
this.textField = textField;
this.context = context;
this.ruleResourceId = rule;
this.editTextName = editTextName;
this.faja = faja;
this.res = context.getResources();
}
public void onFocusChange(View v, boolean hasFocus) {
Log.i("TextFormValidation", "TextFormValidation callback activated!");
if (! hasFocus) {
if (validateTextFieldNotNullData(this.textField)) {
if (validateTextFieldvsFormat(this.textField, ruleResourceId)) {
faja.setFieldValidationArray(getHeadingIndex(editTextName), true);
}
}
if (faja.sumFieldValidations()) {
button.setClickable(true);
button.setText(R.string.addCustomer);
}
else {
button.setText(R.string.next);
button.setClickable(false);
}
}
}
}