添加yes和no按钮退出

时间:2013-12-04 09:52:59

标签: android eclipse

下面是一个代码,用于在点击button2时退出应用程序。当按下按钮时,会显示退出应用程序的Toast消息。我想在其中包含“yes”和“no”(确认按钮)吐司。有人可以告诉我该怎么做?

public void addListenerOnButton2()
{ 
    exit = (Button) findViewById(R.id.button2);
    exit.setOnClickListener( new OnClickListener() {
        @Override
        public void onClick(View v) {
            exme="Are you sure you want to quit?";
            Toast t = Toast.makeText(MainActivity.this,exme, 
                    Toast.LENGTH_SHORT); 
            t.show();
        }
    });
}

2 个答案:

答案 0 :(得分:6)

相反,您可以使用:AlertDialog

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);

        // set title
        alertDialogBuilder.setTitle("Your Title");

        // set dialog message
        alertDialogBuilder
            .setMessage("Click yes to exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    MainActivity.this.finish();
                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }

答案 1 :(得分:1)

将下面的代码放在按钮点击事件

btnButton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        MainActivity.this);

    // set title
    alertDialogBuilder.setTitle("Your Title");

    // set dialog message
    alertDialogBuilder
        .setMessage("Click yes to exit!")
        .setCancelable(false)
        .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                exme="Ok button Pressed";
        Toast t = Toast.makeText(MainActivity.this,exme, 
                Toast.LENGTH_SHORT); 
        t.show();
                MainActivity.this.finish();
            }
          })
        .setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                exme1="Cancel button Pressed";
        Toast t1 = Toast.makeText(MainActivity.this,exme1, 
                Toast.LENGTH_SHORT); 
        t1.show();
                dialog.cancel();
            }
        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
}
});