How do I display an alert dialog on Android?

后端 未结 30 3148
清歌不尽
清歌不尽 2020-11-22 03:02

I want to display a dialog/popup window with a message to the user that shows \"Are you sure you want to delete this entry?\" with one button that says \'Delete\'. When

30条回答
  •  庸人自扰
    2020-11-22 03:28

    This is a basic sample of how to create an Alert Dialog :

    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
    dialog.setCancelable(false);
    dialog.setTitle("Dialog on Android");
    dialog.setMessage("Are you sure you want to delete this entry?" );
    dialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            //Action for "Delete".
        }
    })
            .setNegativeButton("Cancel ", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                //Action for "Cancel".
                }
            });
    
    final AlertDialog alert = dialog.create();
    alert.show();
    

提交回复
热议问题