下面是一个代码,当按下按钮时退出应用程序.当按下按钮时,会显示退出应用程序的Toast消息.我想在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();
}
});
}
Run Code Online (Sandbox Code Playgroud)
相反,您可以使用: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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1193 次 |
| 最近记录: |