在相同活动中显示警报对话框

问题描述 投票:0回答:1

我有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
1个回答
0
投票

尝试在活动中的任何位置创建对话框的方法,请在要显示警报对话框的任何地方调用它。

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.