如何处理ImeOptions的完成按钮点击?

d-m*_*man 172 android ime android-edittext

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

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
Run Code Online (Sandbox Code Playgroud)

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

如何从屏幕键盘上点击完成按钮?

在此输入图像描述

Tho*_*hle 201

我最终得到了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;
    }
});
Run Code Online (Sandbox Code Playgroud)

更新: 上面的代码有时会激活两次回调.相反,我选择了以下代码,我从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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我只是寻找IME_ACTION_DONE,我很惊讶它没有触发.在我也查找了ACTION_DOWN和KEYCODE_ENTER后,它最终触发了onEditorAction().因为我看到内置键盘没有区别(我希望Enter键被突出显示)我想知道使用android:imeOptions ="actionSend"用于EditText XML布局是什么意思. (4认同)
  • 第二种解决方案也会被触发两次. (2认同)
  • 什么是“isPreparedForSending()”?为什么第二种方法返回“true”? (2认同)

chi*_*dev 108

试试这个,它应该适合你需要的东西:


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;
    } 
});
Run Code Online (Sandbox Code Playgroud)

  • 这对HTC Evo不起作用.据我所知,HTC实现了自己的软键盘,忽略了imeOptions. (10认同)
  • 为了安全起见,请确保代码中的操作和视图匹配``<EditText android:imeOptions="actionDone" android:inputType="text"/>`` (2认同)

小智 32

<EditText android:imeOptions="actionDone"
          android:inputType="text"/>
Run Code Online (Sandbox Code Playgroud)

那么,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;
    }
});
Run Code Online (Sandbox Code Playgroud)


Gib*_*olt 27

科特林解决方案

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

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

Kotlin 扩展

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

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

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

<EditText ...
    android:imeOptions="actionDone"
    android:inputType="text"/>
Run Code Online (Sandbox Code Playgroud)

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

  • 您的 lambda 有错误。你应该写“return@setOnEditorActionListener true”而不是“true”,否则在所有情况下它都会变成“false”。 (3认同)
  • 很好的答案,感谢分享!不过,我似乎不断收到有关“setOnEditorActionListener”返回值的 lint 警告。也许这只是本地配置设置的问题,但我的 linter 确实希望我也添加一个“else”分支,以便接受“true”作为侦听器的返回语句(而不是“if”-堵塞)。 (2认同)

iRu*_*uth 26

我知道这个问题很老,但我想指出哪些对我有用.

我尝试使用Android开发者网站上的示例代码(如下所示),但它没有用.所以我检查了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;
            }
        });
Run Code Online (Sandbox Code Playgroud)

所以,我将整数值添加到我的res/values/integers.xml文件中.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="send">0x00000004</integer>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后,我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"/>
Run Code Online (Sandbox Code Playgroud)

然后,示例代码工作.


Rob*_*key 16

有关如何设置OnKeyListener并让它监听"完成"按钮的更多详细信息.

首先将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;
}
Run Code Online (Sandbox Code Playgroud)

给定EditText对象:

EditText textField = (EditText)findViewById(R.id.MyEditText);
textField.setOnKeyListener(this);
Run Code Online (Sandbox Code Playgroud)

  • 在针对API级别17+的应用程序上,这不再起作用,因为您的OnKeyListener不再因软键事件而触发:http://developer.android.com/reference/android/text/method/KeyListener.html (2认同)
  • 我已切换为使用setOnEditorActionListener来查找EditorInfo.IME_ACTION_DONE,这似乎很好用。 (2认同)

Don*_*ato 16

虽然大多数人直接回答了这个问题,但我想详细阐述它背后的概念.首先,当我创建默认的登录活动时,我被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"/>
Run Code Online (Sandbox Code Playgroud)

您应该已经熟悉inputType属性.这只是告知Android所需的文本类型,例如电子邮件地址,密码或电话号码.可以在此处找到可能值的完整列表.

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

然后,您可以通过TextView.OnEditorActionListenerEditText元素定义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;
    } 
});
Run Code Online (Sandbox Code Playgroud)

现在在我的例子中我有android:imeOptions="actionUnspecified"属性.当您想要在按下回车键时登录用户时,此功能非常有用.在您的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;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Coo*_*ind 5

感谢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
    }
}
Run Code Online (Sandbox Code Playgroud)

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