Android检测到屏幕键盘的完成按键


Answers:


276

是的,有可能:

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) {
            // do your stuff here
        }
        return false;
    }
});

请注意,您将必须导入以下库:

import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

谢谢,您给了我一个很好的起点。只是我需要使用EditorInfo.IME_ACTION_SEARCH镜头搜索按钮。
TechNyquist

嗨,ButterKnife有可能吗?@ mikeyaworski @ SzabolcsBerecz
Maulik Dodia

1
@MaulikDodia您可以使用黄油刀的@OnEditorAction()
Ridcully

谢谢。我将与它
@Ridcully

3

当您必须处理Android应用程序中的任何类型的用户输入时,Editor Info是最有用的类。例如,在登录/注册/搜索操作中,我们可以将其用于更准确的键盘输入。编辑器信息类描述了文本编辑对象的几个属性,输入方法将直接与编辑文本内容进行通信。

您可以尝试使用IME_ACTION_DONE

此操作将执行任何Done操作而无需输入任何内容,并且IME将被关闭。

使用setOnEditorActionListener

EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            /* Write your logic here that will be executed when user taps next button */
            handled = true;
        }
        return handled;
    }
});

如何使用自定义键盘处理相同的问题?
Gaju Kollur

0

使用黄油刀可以做到这一点

@OnEditorAction(R.id.signInPasswordText)
boolean onEditorAction(TextView v, int actionId, KeyEvent event){
    if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
        /* Write your logic here that will be executed when user taps next button */
    }
    return false;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.