的onkeydown并不总是叫Android应用 [英] onKeyDown not always called in Android app

查看:120
本文介绍了的onkeydown并不总是叫Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的android游戏的基础上,月球着陆器样品,并且我在使用处理按键事件的一个问题。当活动开始,是的onkeydown或的onkeyup被调用的唯一密钥是DPAD上/下/左/右按键。既不是菜单,背部或dpad_center键触发onKey方法。但是,一旦我已经推了DPAD上/下/左/右按键,$ P $之一pssing的菜单,返回,或者dpad_center键做触发这些方法。我没有得到任何错误,或任何迹象表明发生了什么错误。

I've created a simple android game, based on the Lunar Lander sample, and I'm having a problem with handling key events. When the activity starts, the only keys that onKeyDown or onKeyUp get called for are the dpad up/down/left/right keys. Neither the menu, back, or dpad_center keys trigger onKey methods. However, once I've pushed one of the dpad up/down/left/right buttons, pressing the menu, back, or dpad_center keys do trigger these methods. I'm not getting any errors, or any indication of what's going wrong.

这有可能是将焦点设置错误 - 该活动是由屏幕上的一个按钮启动,因此它可以在触摸屏模式。如果是这样的话,应该不会触及后退按钮让我在正确的对焦模式,这样我可以赶上事件?

It's possible that the focus is set wrong - the activity is started from a button on screen, so it could be in touchscreen mode. If that's the case, shouldn't touching the back button get me in to the right focus mode so that I can catch the event?

我使用的SDK-1.5r3模拟器。我一直没能尝试这种在真正的手机呢。这是我的onkeydown。

I'm using the emulator from SDK-1.5r3. I have not been able to try this on a real phone yet. Here's my onKeyDown.

public boolean onKeyDown(int keyCode, KeyEvent msg)
{
    Log.d(TAG, "onKeyDown: " + keyCode);
    return super.onKeyDown(keyCode, msg);
}

感谢

推荐答案

这是的onkeydown在视图中或在活动?

Is this onKeyDown in a view or in the activity?

如果的setContentView被称为传递一个观点,并且认为有setFocusable(真)调用它,所有的按键事件将绕过活动,并直接进入该视图。

If setContentView is called passing in a view, and that view has setFocusable(true) called on it, all key events will bypass the activity and go straight into the view.

在另一方面,如果你的onkeydown在视图,你有没有呼吁视图上的活动和setFocusable(真)的setContentView,那么你的活动将获得的关键事件,而不是查看。

On the other hand, if your onKeyDown is in the view, and you haven't called setContentView on the Activity and setFocusable(true) on the view, then your Activity will get the key events and not the View.

寻找那些特定的电话,但我认为你是对的它是一个焦点问题。

Look for those specific calls but I think you're right about it being a focus issue.

这篇关于的onkeydown并不总是叫Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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