Android 如何从软键盘捕捉“完成”按键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3031887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:23:23  来源:igfitidea点击:

How to catch a "Done" key press from the soft keyboard

android

提问by oriharel

how do I catch specific key events from the soft keyboard? specifically I'm interested in the "Done" key.

如何从软键盘捕获特定的按键事件?特别是我对“完成”键感兴趣。

采纳答案by Mia Clarke

Note:This answer is old and no longer works. See the answers below.

注意:这个答案是旧的,不再有效。请参阅下面的答案。

You catch the KeyEvent and then check its keycode. FLAG_EDITOR_ACTION is used to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"

您捕获 KeyEvent,然后检查其键码。FLAG_EDITOR_ACTION 用于识别来自 IME 的输入键,输入键已自动标记为“下一步”或“完成”

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

Find the docs here.

此处查找文档。

回答by Swato

I am not quite sure which kind of listener was used in the accepted answer. I used the OnKeyListenerattached to an EditTextand it wasn't able to catch next nor done.

我不太确定在接受的答案中使用了哪种听众。我使用了OnKeyListener附加到 anEditText并且它无法捕捉 next 或 done。

However, using OnEditorActionListenerworked and it also allowed me to differentiate between them by comparing the action value with defined constants EditorInfo.IME_ACTION_NEXTand EditorInfo.IME_ACTION_DONE.

但是,使用OnEditorActionListenerwork 和它也允许我通过将操作值与定义的常量EditorInfo.IME_ACTION_NEXTEditorInfo.IME_ACTION_DONE.

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
            doSomething();
            return true;
        }
        else {
            return false;
        }
    }
});

回答by Jason Axelson

@Swato's answer wasn't complete for me (and doesn't compile!) so I'm showing how to do the comparison against the DONE and NEXT actions.

@Swato 的回答对我来说并不完整(并且无法编译!)所以我展示了如何与 DONE 和 NEXT 操作进行比较。

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        int result = actionId & EditorInfo.IME_MASK_ACTION;
        switch(result) {
        case EditorInfo.IME_ACTION_DONE:
            // done stuff
            break;
        case EditorInfo.IME_ACTION_NEXT:
            // next stuff
            break;
        }
    }
});

Also I want to point out that for JellyBean and higher OnEditorActionListener is needed to listen for 'enter' or 'next' and you cannot use OnKeyListener. From the docs:

另外我想指出,对于 JellyBean 和更高版本的 OnEditorActionListener 需要侦听“输入”或“下一步”,并且您不能使用 OnKeyListener。从文档:

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method.

由于软输入法可以使用多种创造性的文本输入方式,因此不能保证软键盘上的任何按键按下都会生成按键事件:这由 IME 自行决定,实际上不鼓励发送此类事件。对于软输入法上的任何键,您永远不应依赖接收 KeyEvent。

Reference: http://developer.android.com/reference/android/view/KeyEvent.html

参考:http: //developer.android.com/reference/android/view/KeyEvent.html

回答by Arash GM

just do like this :

就这样做:

editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_ACTION_DONE)
    {
       //Do Something
    }

    return false;
}
});

回答by Siraj Uddin

    etSearchFriends = (EditText) findViewById(R.id.etSearchConn);
    etSearchFriends.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
                Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show();
              return true;
            }
            return false;
        }

    }); 

回答by Yamil Garcia

To catch a “Done” key press from the soft keyboard override Activity's onKeyUp method. Setting a OnKeyListener listener for a view won't work because key presses in software input methods will generally not trigger the methods of this listener, this callback is invoked when a hardware key is pressed in the view.

要从软键盘中捕获“完成”按键,请覆盖 Activity 的 onKeyUp 方法。为视图设置 OnKeyListener 侦听器将不起作用,因为软件输入法中的按键按下通常不会触发此侦听器的方法,当在视图中按下硬件键时会调用此回调。

// Called when a key was released and not handled by any of the views inside of the activity. 
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            // code here
            break;
        default:
            return super.onKeyUp(keyCode, event);
    }
    return true;
}

回答by Farhan

you can override done key event by this method:

您可以通过此方法覆盖完成的键事件:

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

回答by Bilal Ahmad

editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // code here
        }
        return false;
    }
});

回答by Hari krishna Andhra Pradesh

Note : inputtype mention in your edittext.

注意:在您的编辑文本中提及输入类型。

<EditText android:id="@+id/select_category" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >

edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
                    //do something here.
                    return true;
                }
                return false;
            }
        });

回答by Ranko1977

I have EditText that searches names, and it automatically shows results below in ListView. SoftInput keyboard only showed "next" button and enter sign - which didn't do anything. I wanted only Done button (no next or enter sign) and also I wanted it when it was pressed, it should close keyboard because user should see results below it.

我有搜索名称的 EditText,它会自动在 ListView 中显示下面的结果。SoftInput 键盘只显示“下一步”按钮并输入符号 - 这没有做任何事情。我只想要完成按钮(没有下一个或输入符号),而且我想要它被按下时,它应该关闭键盘,因为用户应该看到它下面的结果。

Solution that I found /by Mr Cyril Mottier on his blog/ was very simple and it worked without any additional code: in xml where EditText is located, this should be written: android:imeOptions="actionDone"

我发现/由 Cyril Mottier 先生在他的博客上找到的解决方案非常简单,它无需任何额外代码即可工作:在 EditText 所在的 xml 中,应该这样写:android:imeOptions="actionDone"

so hidding keyboard with Done button, EditText should look like this:

所以用“完成”按钮隐藏键盘,EditText 应该是这样的:

<EditText
        android:id="@+id/editText1central"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_toLeftOf="@+id/imageView2tie"
        android:ems="10"
        android:imeOptions="actionDone"
        android:hint="@string/trazi"
        android:inputType="textPersonName" />