I've got two EditText fields and a button,

EditText topNumericInputField; // Top text field
EditText bottomNumericInputField; // Bottom text field
Button clearButton; // Regular Button

I'm trying to clear the text that's in them when I change focus, my onClick method looks like this:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.topInputField:
            clearTextBoxes();
            break;
        case R.id.bottomNumericInputField:
            clearTextBoxes();
            break;
        case R.id.clearButton:
            clearTextBoxes();
            break;
        }
}
private void clearTextBoxes() {
    topNumericInputField.getText().clear();
    bottomNumericInputField.getText().clear();
}

Now, it clears both fields fine with either:

a) The field I click on already has focus

or

b) I click the clear button

If I change focus from the top field to the bottom field, it gets focus, then I need to click it one more time.

I'm not sure exactly what's happening, but I'm sure it's related to something that keeps coming up in the LogCat log, every time I change focus two lines appear:

Tag: IInputConnectionWrapper Text: beginBatchEdit on inactive InputConnection
Tag: IInputConnectionWrapper Text: endBatchEdit on inactive InputConnection

I've tried searching SO and I've seen some submissions that are similar, but none seem to really be what I'm looking for.

Thanks!

有帮助吗?

解决方案

I think problem is not with setting text but problem lies in capturing wrong event.

As you said you want to clear edittext when focus is changed, then you should override onFocusChange(View v, boolean hasfocus) to capture that event. You need to implement FocusChangeListener.

   edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean focus) {
            if (focus) {
                ((EditText)v).setText("");
            }
        }
    });

A simple implementation should look like above. Refer this for detailed information

其他提示

You can set the text null.

topNumericInputField.setText("");
bottomNumericInputField.setText("");

You just need to clear your Text by:

 topNumericInputField.setText("");
 bottomNumericInputField.setText("");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top