首页 > 解决方案 > 在同一活动中显示警报对话框

问题描述

我有AlertDialog,但我想在同一个活动中展示。我应该在下面的代码中添加什么?

new AlertDialog.Builder(ContactAlertDialogActivity.this)
                .setTitle("Permission Required")
                .setMessage(("To set a contact ringtone,Old Telephone Ringtones needs access to your contacts.We never read " +
                        (",store or share your contact information in any way." +
                                "On the next screen tap Allow")))

                // Specifying a listener allows you to take an action before dismissing the dialog.
                // The dialog is automatically dismissed when a dialog button is clicked.
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Continue with delete operation
                    }
                })

                // A null listener allows the button to dismiss the dialog and take no further action.
                .setNegativeButton("NOT NOW", null)
                .setIcon(R.drawable.phone)
                .show();

标签: android

解决方案


尝试在活动中的任何位置为对话框创建一个方法,然后在要显示警报对话框的每个位置调用它。

private void DisplayAlertDialogBox(string _title, string _body)
{
   AlertDialog alertDialog = new AlertDialog.Builder(this)
   //set icon 
    .setIcon(android.R.drawable.ic__alert)
   //set title
   .setTitle(_title)
   //set message
   .setMessage(_body)
   //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();
}

推荐阅读