Android:onKeyDown直到鼠标点击后才会触发
我有一个 onKeyDown 事件,一旦触发,它应该显示图像,但我注意到,尽管多次按下相应的键,但直到我用鼠标单击画布上的任意位置时,图像才会出现。对实际问题以及如何进行有什么建议吗?这个概念相当新,所以不太确定可能缺少什么。 *完整编辑和粘贴类。 谢谢
public class BuccaneerView extends TileView {
public static final int PLAYER = 1;
public static final int GREEN_STAR = 2;
Coordinate P_Location;
public BuccaneerView(Context context, AttributeSet attrs) {
super(context, attrs);
initBucc();
}
private void initBucc() {
this.setFocusable(true);
Resources r = this.getContext().getResources();
resetTiles(4);
loadTile(PLAYER, r.getDrawable(R.drawable.aerialplayer));
loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar));
/**/
P_Location = new Coordinate(5,5);
setTile(PLAYER, P_Location.x, P_Location.y);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_SPACE)
{
setTile(GREEN_STAR, 1, 0);
}
return super.onKeyDown(keyCode, msg);
}
public void update()
{
}
}
I have an onKeyDown Event which is supposed to display an image once triggered, but I have noticed that despite pressing the corresponding key several times, the image does not appear until I click anywhere on the canvas with my mouse. Any suggestions on the actual problem and how to proceed? Pretty new to the concept so not quite sure what may be missing.
*Edited and pasted class in its entirety.
Thanks
public class BuccaneerView extends TileView {
public static final int PLAYER = 1;
public static final int GREEN_STAR = 2;
Coordinate P_Location;
public BuccaneerView(Context context, AttributeSet attrs) {
super(context, attrs);
initBucc();
}
private void initBucc() {
this.setFocusable(true);
Resources r = this.getContext().getResources();
resetTiles(4);
loadTile(PLAYER, r.getDrawable(R.drawable.aerialplayer));
loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar));
/**/
P_Location = new Coordinate(5,5);
setTile(PLAYER, P_Location.x, P_Location.y);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_SPACE)
{
setTile(GREEN_STAR, 1, 0);
}
return super.onKeyDown(keyCode, msg);
}
public void update()
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎将 onKeyDown 视为您的 on 方法之一。
这样做是一件坏事,就好像您调用了这个函数并想要返回按下的键一样。将其更改为简单的 return false 这意味着您正在处理键盘正在执行的操作。
编辑
您使用 onKeyDown 而不是 onKey 有什么原因吗?这是我使用的一些额外代码,它使用一个布尔数组(pressedKeys,长度为 128),稍后您可以使用它来检查该数组以查看是否按下了某个键,
因此您可以说
You seem to be treating onKeyDown as one of your on methods.
Is a bad thing to do, its as if you have called this function and want to return what key has been pressed. Change it to simply be return false which will mean you are handling what they keyboard is doing.
EDIT
Is there any reason you use onKeyDown and not onKey? Here is some extra code which I use, it uses an array of booleans (pressedKeys, which is 128 in length) and you can later use it to check the array to see if a key is pressed
So with this you can then say
据猜测,按键事件不会传递到您设置按键侦听器的任何位置。如果之间存在另一个视图,该视图是侦听器并且停止关键事件的传播(即从此方法返回 true),则可能会发生这种情况。有一些视图默认执行此操作(例如大多数键的 EditText)。如果您可以编辑您的问题并包含更多代码,或者描述您的活动是如何设置的,那将会很有帮助。
通过“单击画布”,您可能会更改焦点并使关键事件传递到不同的视图。这可以解释为什么您在单击后突然看到关键侦听器正在工作。
At a guess, the key event is not being delivered to whatever you have set the key listener on. This can happen if there is another view in between which is a listener and which stops the propagation of the key event (i.e. returning true from this method). There are some views which do this by default (e.g. EditText for most keys). It would be helpful if you could edit your question and include more code, or describe how your activity is setup.
By 'clicking on the canvas' you are probably changing the focus and making the key event being delivered to a different view. This could explain why you suddenly see the key listener working after clicking.