How do I handle ImeOptions' done button click?

2019-01-03 04:34发布

I am having an EditText where I am setting the following property so that I can display the done button on the keyboard when user click on the EditText.

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

When user clicks the done button on the screen keyboard (finished typing) I want to change a RadioButton state.

How can I track done button when it is hit from screen keyboard?

enter image description here

7条回答
乱世女痞
2楼-- · 2019-01-03 05:32

More details on how to set the OnKeyListener, and have it listen for the Done button.

First add OnKeyListener to the implements section of your class. Then add the function defined in the OnKeyListener interface:

/*
 * Respond to soft keyboard events, look for the DONE press on the password field.
 */
public boolean onKey(View v, int keyCode, KeyEvent event)
{
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER))
    {
        // Done pressed!  Do something here.
    }
    // Returning false allows other listeners to react to the press.
    return false;
}

Given an EditText object:

EditText textField = (EditText)findViewById(R.id.MyEditText);
textField.setOnKeyListener(this);
查看更多
登录 后发表回答