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

问题描述 投票:164回答:7

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

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

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

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

android android-edittext ime
7个回答
196
投票

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

102
投票

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


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;
    } 
});

28
投票
   <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;
    }
    });

25
投票

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

我尝试使用Android Developers website中的示例代码(如下所示),但它没有用。所以我检查了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"/>

然后,示例代码工作。


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

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

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

然后,您可以通过为TextView.OnEditorActionListener元素定义EditText来监听操作按钮上的按下。如在你的例子中:

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"属性。当您想要在按下回车键时登录用户时,此功能非常有用。在您的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;
        }
    });

15
投票

有关如何设置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;
}

给定EditText对象:

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

3
投票

感谢kazlin的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
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.