我有一个EditText,我正在设置以下属性,以便当用户单击EditText时,我可以在键盘上显示done按钮。

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

当用户点击屏幕键盘上的完成按钮(完成输入),我想改变一个RadioButton状态。

我怎么能跟踪完成按钮时,它是从屏幕键盘击中?


当前回答

最后我得到了Roberts和chirags的答案:

((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(
        new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // Identifier of the action. This will be either the identifier you supplied,
        // or EditorInfo.IME_NULL if being called due to the enter key being pressed.
        if (actionId == EditorInfo.IME_ACTION_SEARCH
                || actionId == EditorInfo.IME_ACTION_DONE
                || event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            onSearchAction(v);
            return true;
        }
        // Return true if you have consumed the action, else false.
        return false;
    }
});

更新: 上面的代码有时会激活回调两次。相反,我选择了以下代码,这是从谷歌聊天客户端:

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    // If triggered by an enter key, this is the event; otherwise, this is null.
    if (event != null) {
        // if shift key is down, then we want to insert the '\n' char in the TextView;
        // otherwise, the default action is to send the message.
        if (!event.isShiftPressed()) {
            if (isPreparedForSending()) {
                confirmSendMessageIfNeeded();
            }
            return true;
        }
        return false;
    }

    if (isPreparedForSending()) {
        confirmSendMessageIfNeeded();
    }
    return true;
}

其他回答

<EditText android:imeOptions="actionDone"
          android:inputType="text"/>

Java代码如下:

edittext.setOnEditorActionListener(new OnEditorActionListener() { 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Log.i(TAG,"Here you can write the code");
            return true;
        }    
        return false;
    }
});

多亏了奇卡。anddev和Kotlin的Alex Cohn是:

text.setOnEditorActionListener { v, actionId, event ->
    if (actionId == EditorInfo.IME_ACTION_DONE ||
        event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) {
        doSomething()
        true
    } else {
        false
    }
}

这里我检查Enter键,因为它返回EditorInfo。IME_NULL代替IME_ACTION_DONE。

另见Android imeOptions="actionDone"不工作。在EditText中添加android:singleLine="true"。

更多关于如何设置OnKeyListener,并让它监听Done按钮的细节。

首先将OnKeyListener添加到类的implements部分。然后添加OnKeyListener接口中定义的函数:

/*
 * 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;
}

给定一个EditText对象:

EditText textField = (EditText)findViewById(R.id.MyEditText);
textField.setOnKeyListener(this);

如果你使用Android annotation https://github.com/androidannotations/androidannotations

您可以使用@EditorAction注释

@EditorAction(R.id.your_component_id)
void onDoneAction(EditText view, int actionId){
    if(actionId == EditorInfo.IME_ACTION_DONE){
        //Todo: Do your work or call a method
    }
}

虽然大多数人都直接回答了这个问题,但我想更详细地阐述它背后的概念。首先,当我创建一个默认的登录活动时,我引起了IME的注意。它为我生成了一些代码,包括以下内容:

<EditText
  android:id="@+id/password"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="@string/prompt_password"
  android:imeActionId="@+id/login"
  android:imeActionLabel="@string/action_sign_in_short"
  android:imeOptions="actionUnspecified"
  android:inputType="textPassword"
  android:maxLines="1"
  android:singleLine="true"/>

您应该已经熟悉了inputType属性。这只是通知Android需要的文本类型,如电子邮件地址、密码或电话号码。可能值的完整列表可以在这里找到。

It was, however, the attribute imeOptions="actionUnspecified" that I didn't understand its purpose. Android allows you to interact with the keyboard that pops up from bottom of screen when text is selected using the InputMethodManager. On the bottom corner of the keyboard, there is a button, typically it says "Next" or "Done", depending on the current text field. Android allows you to customize this using android:imeOptions. You can specify a "Send" button or "Next" button. The full list can be found here.

这样,您就可以通过定义TextView监听按下的动作按钮。EditText元素的OnEditorActionListener。在你的例子中:

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(EditText v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
       //do here your stuff f
       return true;
    }
    return false;
    } 
});

现在在我的例子中,我有android:imeOptions="actionUnspecified"属性。当您想在用户按enter键时尝试登录时,这很有用。在Activity中,你可以检测到这个标签,然后尝试登录:

    mPasswordView = (EditText) findViewById(R.id.password);
    mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
            if (id == R.id.login || id == EditorInfo.IME_NULL) {
                attemptLogin();
                return true;
            }
            return false;
        }
    });