You can specify the action by setting the
android:imeOptions
attribute. For example, here's how you can specify the Send action:<EditText android:id="@+id/search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/search_hint" android:inputType="text" android:imeOptions="actionSend" />
If you do not explicitly specify an input action then the system attempts to determine if there are any subsequent
android:focusable
fields. If any focusable fields are found following this one, the system applies the (@code actionNext} action to the current EditText
so the user can select Next to move to the next field. If there's no subsequent focusable field, the system applies the "actionDone"
action. You can override this by setting theandroid:imeOptions
attribute to any other value such as "actionSend"
or "actionSearch"
or suppress the default behavior by using the "actionNone"
action.Responding to action button events
If you have specified a keyboard action for the input method using
android:imeOptions
attribute (such as"actionSend"
), you can listen for the specific action event using an TextView.OnEditorActionListener
. The TextView.OnEditorActionListener
interface provides a callback method called onEditorAction()
that indicates the action type invoked with an action ID such as IME_ACTION_SEND
or IME_ACTION_SEARCH
.
For example, here's how you can listen for when the user clicks the Send button on the keyboard:
EditText editText = (EditText) findViewById(R.id.search); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { sendMessage(); handled = true; } return handled; } });
沒有留言:
張貼留言