关于android:如何处理ImeOptions的完成按钮单击?

How do I handle ImeOptions' done button click?

我在EditText处设置以下属性,以便当用户单击EditText时可以在键盘上显示完成按钮。

1
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

当用户单击屏幕键盘上的完成按钮(完成输入)时,我想更改RadioButton状态。

从屏幕键盘上击中完成按钮时,我该如何跟踪?

Screenshot showing the bottom right 'done' button on the software keyboard


我最终得到了罗伯茨和基拉格答案的结合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
((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;
    }
});

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 '
' 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;
}


试试这个,它应该可以满足您的需求:

1
2
3
4
5
6
7
8
9
10
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
       //do here your stuff f
       return true;
    }
    return false;
    }
});


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

然后,java代码是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    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;
    }
    });


我知道这个问题很老,但是我想指出对我有用的。

我尝试使用Android开发者网站上的示例代码(如下所示),但没有用。因此,我检查了EditorInfo类,并意识到将IME_ACTION_SEND整数值指定为0x00000004

来自Android开发人员的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextEmail
        .setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    /* handle action here */
                    handled = true;
                }
                return handled;
            }
        });

因此,我将整数值添加到了res/values/integers.xml文件中。

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="send">0x00000004</integer>
</resources>

然后,我如下编辑布局文件res/layouts/activity_home.xml

1
2
3
4
5
6
7
<EditText android:id="@+id/editTextEmail"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:imeActionId="@integer/send"
  android:imeActionLabel="@+string/send_label"
  android:imeOptions="actionSend"
  android:inputType="textEmailAddress"/>

然后,示例代码起作用了。


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

1
2
3
4
5
6
7
8
9
10
11
<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所需的文本类型,例如电子邮件地址,密码或电话号码。可在此处找到所有可能值的完整列表。

但是,我不理解它的用途是属性imeOptions="actionUnspecified"。使用InputMethodManager选择文本时,Android允许您与从屏幕底部弹出的键盘进行交互。在键盘的底角,有一个按钮,通常根据当前文本字段显示" Next"(下一步)或" Done"(完成)。 Android允许您使用android:imeOptions对此进行自定义。您可以指定"发送"按钮或"下一步"按钮。完整列表可以在这里找到。

这样,您可以通过为EditText元素定义TextView.OnEditorActionListener来监听操作按钮上的按下。如您的示例所示:

1
2
3
4
5
6
7
8
9
10
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键时尝试登录时,此功能很有用。在您的活动中,您可以检测到此标记,然后尝试登录:

1
2
3
4
5
6
7
8
9
10
11
    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;
        }
    });


有关如何设置OnKeyListener以及让它侦听"完成"按钮的更多详细信息。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 * 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对象:

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


感谢Kotlin中的chikka.anddev和Alex Cohn,它是:

1
2
3
4
5
6
7
8
9
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