我为Android设计了这个应用程序,用户希望我为按钮操作添加一个确认弹出窗口以继续。如果单击Yes
,则操作会继续,但如果单击No
,则该过程会中断。我已经通过网络阅读并看到dialog
用于此目的。有人可以用示例代码解释这是如何工作的。
答案 0 :(得分:0)
看看下面的示例代码。如果你是从活动传递中调用它,那么这个样本#34;在showDialog上下文参数中,如果您在片段中显示该对话框,则传递getActivity()而不是上下文
public void showDialog(Context context,String title,String msg){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle(title);
// set dialog message
alertDialogBuilder.setMessage(msg).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 :(得分:0)
new AlertDialog.Builder(this)
.setTitle("Confirm")
.setMessage("Do you really want to do Perform this Action?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
}})
.setNegativeButton(android.R.string.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();
}
}).show();