Android 后退按钮覆盖帮助

发布于 2024-11-04 20:04:59 字数 244 浏览 3 评论 0原文

我正在制作一个应用程序,一个游戏,我希望玩家能够使用后退按钮进行跳跃(对于单点触摸设备)。我的目标平台是2.1(API级别7)。

我已经尝试过 onKeyDown() 和 onBackPressed(),但它们仅在释放后退按钮时调用,而不是在按下后退按钮时调用。

1)这正常吗?

2)如何才能在按下按钮时记录按下情况?

编辑: 我还想补充一点,它使用键盘可以正常工作(按下键时会调用 onKeyDown)。

I am making an app, a game, and I want the player to be able to use the back button for jumping(for single-touch devices). My target platform is 2.1(API level 7).

I've tried both onKeyDown() and onBackPressed(), but they are only called when the back button is RELEASED, not when it is pressed down.

1) Is this normal?

2) How can I make it so it registers the press when the button is pressed?

EDIT:
I'd also like to add that it works correctly using a keyboard(onKeyDown is called when a key is pressed down).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2024-11-11 20:04:59

更新:我对此很好奇。看一下 android.view.View 源代码: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View.java

一个典型的示例是处理 BACK 键来更新应用程序的 UI,而不是让 IME 看到它并自行关闭。

代码:

/**
 * Handle a key event before it is processed by any input method
 * associated with the view hierarchy.  This can be used to intercept
 * key events in special situations before the IME consumes them; a
 * typical example would be handling the BACK key to update the application's
 * UI instead of allowing the IME to see it and close itself.
 *
 * @param keyCode The value in event.getKeyCode().
 * @param event Description of the key event.
 * @return If you handled the event, return true. If you want to allow the
 *         event to be handled by the next receiver, return false.
 */
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    return false;
}

使用dispatchKeyEvent:

@Override
public boolean dispatchKeyEvent (KeyEvent event) {
    Log.d("**dispatchKeyEvent**", Integer.toString(event.getAction()));
    Log.d("**dispatchKeyEvent**", Integer.toString(event.getKeyCode()));
    if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
        Toast.makeText(this, "Back button pressed", Toast.LENGTH_LONG).show();
        return true;
    }
    return false;
}

独立记录两个事件即使是后退键。由于某种原因,唯一未记录的密钥是 KEYCODE_HOME。事实上,如果您保持按下后退按钮,您将连续看到多个 ACTION_DOWN (0) 事件(如果您 return false; 则会看到更多事件)。在 Eclair 模拟器和 Samsung Captivate(定制 Froyo ROM)中进行了测试。

Update: I got curious about this. Take a look at the android.view.View source code: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View.java

a typical example would be handling the BACK key to update the application's UI instead of allowing the IME to see it and close itself.

code:

/**
 * Handle a key event before it is processed by any input method
 * associated with the view hierarchy.  This can be used to intercept
 * key events in special situations before the IME consumes them; a
 * typical example would be handling the BACK key to update the application's
 * UI instead of allowing the IME to see it and close itself.
 *
 * @param keyCode The value in event.getKeyCode().
 * @param event Description of the key event.
 * @return If you handled the event, return true. If you want to allow the
 *         event to be handled by the next receiver, return false.
 */
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    return false;
}

Using dispatchKeyEvent:

@Override
public boolean dispatchKeyEvent (KeyEvent event) {
    Log.d("**dispatchKeyEvent**", Integer.toString(event.getAction()));
    Log.d("**dispatchKeyEvent**", Integer.toString(event.getKeyCode()));
    if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
        Toast.makeText(this, "Back button pressed", Toast.LENGTH_LONG).show();
        return true;
    }
    return false;
}

Logs the two events independently even for the back key. The only key that did not log was KEYCODE_HOME, for some reason. In fact, if you maintain the back button pressed, you will see several ACTION_DOWN (0) events in a row (and much more if you return false; instead). Tested in an Eclair emulator and a Samsung Captivate (custom Froyo ROM).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文