用于删除的Android确认消息

Rak*_*esh 6 android android-layout

嗨,我是android开发新手.我想添加一个确认消息框进行删除.如何在Android中做到这一点..?

Ram*_*ran 38

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;         
}
Run Code Online (Sandbox Code Playgroud)

调用

AlertDialog diaBox = AskOption();
diaBox.show();
Run Code Online (Sandbox Code Playgroud)


Pra*_*mar 10

你应该用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();
Run Code Online (Sandbox Code Playgroud)