如何在按下设备上的后退按钮时完成列表活动

发布于 2024-12-13 14:30:49 字数 717 浏览 3 评论 0原文

我使用以下代码来完成活动,但我想在后按事件上完成列表活动。帮我 提前致谢

public void onBackPressed() {
         AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
            alertDialog.setMessage("Want to exit ?");  
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) { 
                  for(Activity activity:activities)
                       activity.finish();
            } });   
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) {  

            }});   
          alertDialog.show();  
    }

i m using the following code to finish an activity but i want to finish the list activity on back press event . help me
Thanks in advance

public void onBackPressed() {
         AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
            alertDialog.setMessage("Want to exit ?");  
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) { 
                  for(Activity activity:activities)
                       activity.finish();
            } });   
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {  
              public void onClick(DialogInterface dialog, int which) {  

            }});   
          alertDialog.show();  
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一念一轮回 2024-12-20 14:30:49

我不会修改您的代码,只是在这里建议新代码,

public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if(keyCode==KeyEvent.KEYCODE_BACK){
        new AlertDialog.Builder(ProgressController.this).setTitle(
        "Attention").setMessage(
        "Want to exit ?")
        .setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        finish();
                    }
                }).setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {

                    }
                }).show();
        return true;
        }else{
            return false;
        }

        //return super.onKeyDown(keyCode, event);

    }

I am not modifying the your code,just i am suggesting new code here,

public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if(keyCode==KeyEvent.KEYCODE_BACK){
        new AlertDialog.Builder(ProgressController.this).setTitle(
        "Attention").setMessage(
        "Want to exit ?")
        .setPositiveButton("YES",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {
                        finish();
                    }
                }).setNegativeButton("NO",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int which) {

                    }
                }).show();
        return true;
        }else{
            return false;
        }

        //return super.onKeyDown(keyCode, event);

    }
很酷不放纵 2024-12-20 14:30:49

您的代码应如下修改。

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

         AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
         alertDialog.setMessage("Want to exit ?");  
         alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int which) { 
              finish();
         } });   
         alertDialog.setButton2("No", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int which) {  

               return;
         }});   
       alertDialog.show(); 

    }

Your Code should be modified like below..

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub

         AlertDialog alertDialog = new AlertDialog.Builder(this).create();  
         alertDialog.setMessage("Want to exit ?");  
         alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int which) { 
              finish();
         } });   
         alertDialog.setButton2("No", new DialogInterface.OnClickListener() {  
           public void onClick(DialogInterface dialog, int which) {  

               return;
         }});   
       alertDialog.show(); 

    }
┈┾☆殇 2024-12-20 14:30:49

将取消侦听器添加到警报对话框中,然后从其中关闭您的活动。

    alertDialog.setCancelable(true);
    alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            finish();
        }
    });

Add a cancel listener to your alert dialog, and close your activity from within there.

    alertDialog.setCancelable(true);
    alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            finish();
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文