0

这里的目标是在用户输入联系信息时验证每个字段,并最终对每个字段应用自动格式(大写、拼写等)。

我几乎可以使用以下代码进行此操作,但它基于按键回调,而我真正想要触发的是每个文本字段的“失去焦点”。有任何想法吗?

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;
}
4

2 回答 2

0

好的,它不会让我昨天发布这个,但我想通了。为了让其他人免于这个掌声时刻,我将在此处发布固定代码。

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

}

于 2012-04-20T13:44:10.117 回答
0

这可能是您正在寻找的东西吗? TextView.onWindowFocusChanged()

于 2012-04-19T19:17:18.390 回答