How do I display an alert dialog on Android?

后端 未结 30 2492
清歌不尽
清歌不尽 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:34

    Use AlertDialog.Builder :

    AlertDialog alertDialog = new AlertDialog.Builder(this)
    //set icon 
     .setIcon(android.R.drawable.ic_dialog_alert)
    //set title
    .setTitle("Are you sure to Exit")
    //set message
    .setMessage("Exiting will call finish() method")
    //set positive button
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
       //set what would happen when positive button is clicked    
            finish();
        }
    })
    //set negative button
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
       //set what should happen when negative button is clicked
            Toast.makeText(getApplicationContext(),"Nothing Happened",Toast.LENGTH_LONG).show();
        }
    })
    .show();
    

    You will get the following output.

    To view alert dialog tutorial use the link below.

    Android Alert Dialog Tutorial

提交回复
热议问题