219

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

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

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

当从屏幕键盘击中完成按钮时,如何跟踪它?

显示软件键盘右下角“完成”按钮的屏幕截图

4

9 回答 9

228

我最终得到了 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;
    }
});

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

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;
}
于 2011-03-20T17:03:28.653 回答
138

试试这个,它应该可以满足你的需要:


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;
    } 
});
于 2011-01-29T08:47:12.340 回答
49
<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;
    }
});
于 2016-10-24T08:13:26.957 回答
30

Kotlin 解决方案

在 Kotlin 中处理它的基本方法是:

edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        callback.invoke()
        return@setOnEditorActionListener true
    }
    false
}

Kotlin 扩展

使用它来调用edittext.onDone{/*action*/}你的主代码。使您的代码更具可读性和可维护性

fun EditText.onDone(callback: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            return@setOnEditorActionListener true
        }
        false
    }
}

不要忘记将这些选项添加到您的编辑文本中

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

如果您需要inputType="textMultiLine"支持,请阅读这篇文章

于 2020-01-15T19:03:44.630 回答
27

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

我尝试使用Android Developers 网站上的示例代码(如下所示),但没有成功。所以我检查了 EditorInfo 类,我意识到 IME_ACTION_SEND 整数值被指定为0x00000004.

来自 Android 开发者的示例代码:

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文件中。

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

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

<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"/>

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

于 2014-11-06T17:08:25.087 回答
17

有关如何设置 OnKeyListener 并让它监听 Done 按钮的更多详细信息。

首先将 OnKeyListener 添加到类的实现部分。然后添加 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);
于 2010-03-20T20:48:41.600 回答
17

虽然大多数人都直接回答了这个问题,但我想更详细地阐述它背后的概念。首先,当我创建默认登录活动时,我被 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 预期的文本类型,例如电子邮件地址、密码或电话号码。可以在此处找到可能值的完整列表。

然而,imeOptions="actionUnspecified"我不明白它的用途是这个属性。Android 允许您与使用 选择文本时从屏幕底部弹出的键盘进行交互InputMethodManager。在键盘的底角,有一个按钮,通常会显示“下一步”或“完成”,具体取决于当前文本字段。Android 允许您使用android:imeOptions. 您可以指定“发送”按钮或“下一步”按钮。完整列表可以在这里找到。

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

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"属性。当您想在用户按下回车键时尝试登录时,这很有用。在您的活动中,您可以检测到此标签,然后尝试登录:

    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;
        }
    });
于 2017-03-08T21:27:14.330 回答
11

感谢 Kotlin 中的chikka.anddevAlex 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
    }
}

在这里我检查Enterkey,因为它返回EditorInfo.IME_NULL而不是IME_ACTION_DONE.

另请参阅Android imeOptions="actionDone" not working。添加android:singleLine="true"EditText.

于 2018-10-02T16:17:10.470 回答
0

如果您使用 Android 注释 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
    }
}
于 2020-06-11T08:33:14.033 回答