我怎样才能建立在物理菜单按钮,长时间触摸事件? [英] How can I create a long touch event on the physical menu button?
问题描述
现在,当你按住菜单按钮,在Android设备上它拉起来柔软keyboard.Is有没有办法覆盖此?我宁愿选择在长触摸此按钮会发生什么。
Right now when you hold down the menu button on my android device it pulls up a soft keyboard.Is there a way to override this? I would rather choose what happens on a long touch of this button.
在使用onKeyLong preSS它,只有当后退按钮不放检测。我怎样才能使这项工作的菜单按钮?
When using onKeyLongPress it only detects when the "Back" button is held down. How can I make this work for the menu button?
推荐答案
有关这一点,你可以使用 onKeyLong $ P $(PSS)
-method,由提供 KeyEvent.Callback
级(可在活动的被使用过,因为它们的子类的 KeyEvent.Callback
- 类)。
For this, you can use the onKeyLongPress()
-method, offered by the KeyEvent.Callback
-class (can be used in Activity's too, since they are a subclass of the KeyEvent.Callback
-class).
还有一个小窍门,使这项工作:你要告诉Android的跟踪长期preSS点击菜单 - 按钮为 onKeyLong $ P $ (PSS)
-method不会被触发,否则。这是在正常完成的onkeydown()
-method。
There is also a little trick to make this work: You'll have to tell Android to track a long-press click on the "Menu"-button as the onKeyLongPress()
-method will not be triggered otherwise. This is done in the normal onKeyDown()
-method.
所以你的code可能是这样的:
So your code might look like this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// this tells the framework to start tracking for
// a long press and eventual key up. it will only
// do so if this is the first down (not a repeat).
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_MENU){
// Do what you want...
Toast.makeText(this, "I'm down!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyLongPress(keyCode,event);
}
一个伟大的与文章进一步的信息可以在Android开发者博客上找到。
A great article with further informations can be found on the Android Developer Blog.
这篇关于我怎样才能建立在物理菜单按钮,长时间触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!