假的 KeyEvent 并不像“真实”的那样“完全”工作。按钮。为什么?
我试图用 KEYCODE_BACK 调用我的主要活动的 onKeyDown() ,这样它的行为就好像我自己按下了“后退”按钮一样。我使用以下代码执行此操作:
KeyEvent goBackDown = new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK,0,0);
goBackDown.dispatch(activity);
SystemClock.sleep(50); // as if human pressed the key
KeyEvent goBackUp = new KeyEvent(0,0,KeyEvent.ACTION_UP,KeyEvent.KEYCODE_BACK,0,0);
goBackUp.dispatch(activity);
我的活动的 onKeyDown() 当前仅调用:
return super.onKeyDown(keyCode, event);
然而,与真实后退按钮不同,当调用“假”代码时,活动拒绝变得不可见。
为什么?
I am trying to invoke my main activity's onKeyDown() with KEYCODE_BACK, so that it behaves as if I pressed the 'back' button myself. I do that using the following code:
KeyEvent goBackDown = new KeyEvent(0,0,KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK,0,0);
goBackDown.dispatch(activity);
SystemClock.sleep(50); // as if human pressed the key
KeyEvent goBackUp = new KeyEvent(0,0,KeyEvent.ACTION_UP,KeyEvent.KEYCODE_BACK,0,0);
goBackUp.dispatch(activity);
My activity's onKeyDown() currently only calls:
return super.onKeyDown(keyCode, event);
Yet, unlike the real Back button, when the "fake" code is called, the activity refuses to become invisible.
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
http://developer.android。 com/reference/android/view/View.html#dispatchKeyEvent%28android.view.KeyEvent%29
use
http://developer.android.com/reference/android/view/View.html#dispatchKeyEvent%28android.view.KeyEvent%29
Aleadam 方法不适用于我的 android 4.1.2。所以我写了解决方法:
Aleadam method don't work on my android 4.1.2. So I write workaround:
尝试使用这个
Try using this