用于删除的Android确认消息

时间:2012-07-31 12:41:24

标签: android android-layout

您好我是Android开发的新手。我想添加一个删除确认消息框。如何在Android中做到这一点..?

2 个答案:

答案 0 :(得分:35)

 private AlertDialog AskOption()
 {
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
        //set message, title, and icon
        .setTitle("Delete") 
        .setMessage("Do you want to Delete") 
        .setIcon(R.drawable.delete)

        .setPositiveButton("Delete", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) { 
                //your deleting code
                dialog.dismiss();
            }   

        })



        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }
        })
        .create();
        return myQuittingDialogBox;

    }

<强>调用

AlertDialog diaBox = AskOption();
diaBox.show();

答案 1 :(得分:9)

您应该使用AlertDialog

执行此操作
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            //Do your Yes progress
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            //Do your No progress
            break;
        }
    }
};
AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Are you sure to delete?").setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("No", dialogClickListener).show();