【问题标题】:how to write code for dialog box when we click on exit and code for dialog xml in android?当我们单击退出时如何编写对话框代码和android中的对话框xml代码?
【发布时间】:2013-09-09 01:32:06
【问题描述】:

我想在下面的代码中为对话框编写代码,当按下退出按钮时,它的显示消息如果“是”,你真的要退出吗?如果不是,则完全关闭应用程序页面,它会返回当前页面。我已经使用 system.exit(0) 但它关闭了当前活动并出现在菜单页面上。所以请编写一些代码来完全终止应用程序。对于那个对话框,我需要创建新的 xml 文件吗?

@Override
    public void onClick(View arg0) {
        if(arg0.getId()==R.id.btnId)
        {
              //write code here 
        }
              }

这里的 btnId 是退出按钮的 id。我在stackoverflow上看到了一个与带有对话按钮的后退按钮相关的答案。但它与我的代码略有不同。所以我很困惑。

【问题讨论】:

    标签: android xml dialog exit


    【解决方案1】:

    我希望这会有所帮助。

            public void onClick(View v) {
                new AlertDialog.Builder(YOUR_ACTIVITY)
                        .setMessage("Are you sure?")
                        .setCancelable(true)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        finish();
                                    }
                                }).setNegativeButton("No", null).show();
    
            }
    

    【讨论】:

    • 在 YOUR_ACTIVITY 的位置上,我必须编写活动,如果是,我将在其中添加此代码。然后它在那一行显示错误。
    • 是的,你应该把你的活动放在那里。例如,如果您的活动名称是 MainActivity,那么您可以将 MainActivity.this
    • 它可以工作,但是当对话框出现时,它只显示一秒钟。在我们按下是或否之前,有什么方法可以按住它。
    • 嗯。它实际上不应该发生。但尝试 .setCancelable(false) 而不是 .setCancelable(true)
    • 兄弟。它的工作。实际上我的代码中有一些问题,现在我解决了它并且您的代码正常工作。很好的答案。
    【解决方案2】:

    对话框

    试试这个

    AlertDialog.Builder ad = new AlertDialog.Builder(act);
            ad.setCancelable(false);
            ad.setTitle("EXIT CONFIRMATION");
            ad.setMessage("are you sure you want to exit?");
            ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    finish();
                onDestroy();
                }
            });
            ad.show();
    

    我的代码中的行为是活动或上下文

    【讨论】:

      【解决方案3】:

      为“是”和“否”制作一个完整的对话框。执行以下操作:

      注意:对于此代码,您需要一个按钮。

      希望对你有帮助。

      按钮按钮 = (Button) findViewById(R.id.button1);

          button.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
      
                  alertDialogBuilder.setTitle("Alert");
                  alertDialogBuilder.setMessage("Do you really want to exit?");
      
                  AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
                  ad.setCancelable(false);
                  ad.setTitle("EXIT CONFIRMATION");
                  ad.setMessage("are you sure you want to exit?");
                  ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                          finish();
      
      
      
                      }
                  });
                  ad.setNegativeButton("No", new DialogInterface.OnClickListener() {
      
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          // TODO Auto-generated method stub
                           dialog.cancel();
                      }
                  }); 
      
                  ad.show();
      
                }
              });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多