繁体   English   中英

Android检测到OnScreen键盘的完成按键

[英]Android detect Done key press for OnScreen Keyboard

是否可以检测到何时按下onScreen键盘的Done键?

对的,这是可能的:

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;

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

您可以尝试使用IME_ACTION_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;
    }
});

使用Butterknife你可以做到这一点

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM