调度 keyDown 到 TabActivity

发布于 2024-10-18 11:15:14 字数 927 浏览 6 评论 0原文

在我的应用程序中,我的 TabActivity 有 4 个选项卡,我希望在用户退出我的应用程序时弹出用户确认对话框。

我已经重写了 TabActivityonKeyDown 事件并编写了我的要求,但结果并不符合预期,即当用户退出时没有弹出窗口,但是当我重写我的孩子选项卡要求已完全满足。

是的,我可以简单地为所有子选项卡复制粘贴 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下壹個目標 2024-10-25 11:15:14

您必须重写 onBackPressed() 方法。

@Override
public void onBackPressed() {
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;
}

编辑:似乎您使用的 1.6 没有 onBackPressed

我认为您必须像 示例。

You must override onBackPressed() method.

@Override
public void onBackPressed() {
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;
}

EDIT: Seems that you use 1.6 where there is no onBackPressed

I think that you must override onKeyUp(...) like in this sample.

桜花祭 2024-10-25 11:15:14

这就是我实现它的方法

在我的 TabActivity 中,我创建了一个静态方法来处理来自子选项卡的所有调用

public static boolean hanndleChildBackButtonPress(final Activity activity,int keyCode, KeyEvent event){

    if(keyCode == KeyEvent.KEYCODE_BACK) {
            //HANDLING MY ON KEY DOWN LOGIC
            return true;
    }
    else {
            return false;
    }

}

,在我的子选项卡中,我将 KeyDown 调用从 TabActivity 转移到静态方法

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     if(MyTabActivity.hanndleChildBackButtonPress(this, keyCode, event)) {
            return true;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }

}

This is how I achieved it

In my TabActivity I created a static method to handle my all calls from child Tabs

public static boolean hanndleChildBackButtonPress(final Activity activity,int keyCode, KeyEvent event){

    if(keyCode == KeyEvent.KEYCODE_BACK) {
            //HANDLING MY ON KEY DOWN LOGIC
            return true;
    }
    else {
            return false;
    }

}

And in my Child Tabs I diverted KeyDown call to static method from TabActivity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     if(MyTabActivity.hanndleChildBackButtonPress(this, keyCode, event)) {
            return true;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }

}
执笔绘流年 2024-10-25 11:15:14

您可以像下面这样重写 Tabactivity 的子活动中的 onKeyDown 方法,以避免子活动消耗 KeyEvent.KEYCODE_BACK 。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
    }
}

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.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文