调度 keyDown 到 TabActivity
在我的应用程序中,我的 TabActivity
有 4 个选项卡,我希望在用户退出我的应用程序时弹出用户确认对话框。
我已经重写了 TabActivity
的 onKeyDown
事件并编写了我的要求,但结果并不符合预期,即当用户退出时没有弹出窗口,但是当我重写我的孩子选项卡要求已完全满足。
是的,我可以简单地为所有子选项卡复制粘贴 onKeyDown
代码,但是是否有最佳实践,也许我可以将 onKeyDown
分派到我的 TabActivity。
代码如下:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setTitle("Confirm Quit")
.setMessage("Really Quit ?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Wait!", null)
.show();
return true;
}
else {
return super.onKeyDown(keyCode, event);
}
}
In my application I have 4 tabs in my TabActivity
I want a user confirmation dialog popup when user exits my application.
I have overridden the onKeyDown
event of TabActivity
and coded my requirements but the result is not as expected i.e. no popup comes when user exits, but when I am overriding on key down of my child tabs the requirement is full filled.
Yes I can simply copy paste the onKeyDown
code for all my child tabs, but is there a best practice for this, may be I can dispatch the onKeyDown
to my TabActivity.
Heres the code:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(this)
.setTitle("Confirm Quit")
.setMessage("Really Quit ?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Wait!", null)
.show();
return true;
}
else {
return super.onKeyDown(keyCode, event);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须重写 onBackPressed() 方法。
编辑:似乎您使用的 1.6 没有
onBackPressed
我认为您必须像 此示例。
You must override onBackPressed() method.
EDIT: Seems that you use 1.6 where there is no
onBackPressed
I think that you must override onKeyUp(...) like in this sample.
这就是我实现它的方法
在我的
TabActivity
中,我创建了一个静态方法来处理来自子选项卡的所有调用,在我的子选项卡中,我将 KeyDown 调用从
TabActivity
转移到静态方法This is how I achieved it
In my
TabActivity
I created a static method to handle my all calls from child TabsAnd in my Child Tabs I diverted KeyDown call to static method from
TabActivity
您可以像下面这样重写 Tabactivity 的子活动中的 onKeyDown 方法,以避免子活动消耗 KeyEvent.KEYCODE_BACK 。
You can just override the onKeyDown method in the subactivities of the Tabactivity like following, to avoid the KeyEvent.KEYCODE_BACK is consumed by the subactivities.