显示软键盘时后退键的Android键事件? [英] Android key event for back key when soft keyboard is shown?

查看:109
本文介绍了显示软键盘时后退键的Android键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在显示软键盘时捕获后退"键事件.

I am trying to capture the "back" key event when the soft keyboard is shown.

我正在使用SDK ver 8,并且只有按如下所示隐藏软键盘时,才能成功捕获返回键事件:

I am using SDK ver 8 and can successfully capture the back key event ONLY when the soft keyboard is HIDDEN as follows:

@Override
public void onBackPressed() {
    // do something
    super.onBackPressed();
}

问题是显示软键盘时系统未调用此方法.我试图登录onKeyDown/Up()方法以及上面的方法来弄清楚在这种情况下正在发生的事情是无济于事的.见下文:

The problem is that the system is not calling this method while the soft keyboard is shown. I have tried to log onKeyDown/Up() methods as well as the above method to work out what is happening in this scenario to no avail. See below:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    //DEBUGGING
    Log.d(TAG + "KeyUP", String.valueOf(event));
}

将按预期返回音量和菜单键的日志,但仅在隐藏软键盘和菜单时才记录返回键.

Logs are returned for volume and menu keys as expected but the back key is only logged when the soft keyboard and menu are hidden.

有没有可以解释这种现象的后退键"大师?

Are there any "back key" guru's out there that can explain this phenomenon?

非常感谢您的帮助.

推荐答案

在对该网站和Android API进行深入研究之后,我发现

After digging around further on this site and the Android API I have found that

KeyEvent.KEYCODE_BACK

被具有输入法连接的IME捕获并吞噬,并且当前显示该输入法(换句话说,未隐藏软键盘).这意味着事件是在系统调用Activity类的onKeyDown()或onKeyUp()方法之前消耗的.

is caught and gobbled up by an IME that has an input method connection and that input method is currently shown (in other words; the soft keyboard is NOT hidden). This means that the event is consumed before the system calls the Activity classes onKeyDown() or onKeyUp() methods.

要解决此问题,请创建IME小部件的子类(TextView或其子类,例如EditText)并实现

To get around this, create a sub-class of your IME widget (TextView or its child classes such as EditText) and implement onKeyPreIme().

Stack用户i2097i已在此处中的活动中发布了一个很好的解决方案,用于实现onKeyPreIme(). 如果您希望保留Android的默认行为(即关闭键盘),只需确保在onKeyPreIme()覆盖中返回FALSE.

Stack user i2097i has posted a good solution to implementing onKeyPreIme() in an activity here. Just make sure to return FALSE in your onKeyPreIme() Override if you wish to retain Androids default behaviour (i.e. closing the keyboard).

这篇关于显示软键盘时后退键的Android键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆