如何在android中关闭AlertDialog

rsl1atfo  于 2022-12-25  发布在  Android
关注(0)|答案(8)|浏览(622)

我创建了包含4个按钮的AlertDialog

OptionDialog = new AlertDialog.Builder(this);
        OptionDialog.setTitle("Options");
        LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.options, null, false);
        background = (Button) v.findViewById(R.id.bkgSpinnerLabel);
        SoundVib = (Button) v.findViewById(R.id.SoundVibSpinnerLabel);
        
        OptionDialog.setView(v);
        OptionDialog.setCancelable(true);
        OptionDialog.setNeutralButton("Ok",
                new DialogInterface.OnClickListener() {
                
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
        background.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SetBackground();
             // here I want to dismiss it after SetBackground() method 
            }
        });
        

        SoundVib.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent soundVibIntent = new Intent(SebhaActivity.this, EditPreferences.class);
                startActivity(soundVibIntent);
            }
        });
        
        OptionDialog.show();

我想在SetBackground()方法之后关闭它,我该怎么做?

au9on6nz

au9on6nz1#

实际上,AlertDialog.Builder类中没有任何cancel()dismiss()方法。
因此,使用AlertDialog示例代替AlertDialog.Builder optionDialog
比如

AlertDialog optionDialog = new AlertDialog.Builder(this).create();

现在,只需调用optionDialog.dismiss();

background.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        SetBackground();
        // here I want to dismiss it after SetBackground() method 
        optionDialog.dismiss();
    }
});
gcmastyq

gcmastyq2#

我想有个更简单的解决办法:只需使用传递给onClick方法的DialogInterface参数。

AlertDialog.Builder db = new AlertDialog.Builder(context);
        db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface d, int arg1) {
                db.cancel();
                //here db.cancel will dismiss the builder

            };  
        });

例如,请参见http://www.mkyong.com/android/android-alert-dialog-example

nnt7mjpx

nnt7mjpx3#

试试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
   AlertDialog OptionDialog = builder.create();
  background.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SetBackground();
       OptionDialog .dismiss();
        }
    });
wydwbb8l

wydwbb8l4#

关闭或取消AlertDialog.Builder

dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

在对话框界面上调用dismiss()

txu3uszq

txu3uszq5#

以下是关闭alertDialog的方法

lv_three.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                GetTalebeDataUser clickedObj = (GetTalebeDataUser) parent.getItemAtPosition(position);
                alertDialog.setTitle(clickedObj.getAd());
                alertDialog.setMessage("Öğrenci Bilgileri Güncelle?");
                alertDialog.setIcon(R.drawable.ic_info);
                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // User pressed YES button. Write Logic Here
                    }
                });
                alertDialog.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //alertDialog.
                        alertDialog.setCancelable(true); // HERE

                    }
                });
                alertDialog.show();
                return true;
            }
        });
gmol1639

gmol16396#

关闭警报对话框的方法有两种。
备选办法1:
第一个月
备选方案二:
The DialogInterface#dismiss();
当您为按钮定义事件侦听器时,框架会立即调用DialogInterface#dismiss();

  1. AlertDialog#setNegativeButton();
  2. AlertDialog#setPositiveButton();
  3. AlertDialog#setNeutralButton();
    用于"报警"对话框。
tktrz96b

tktrz96b7#

与Kotlin:

fun Activity.showDialog() {
    val builder = AlertDialog.Builder(this)
    builder.setMessage(R.string.message_id)
        .setPositiveButton(
            R.string.confirm_action_text_id
        ) { dialogInterface, _ ->
            dialogInterface.dismiss()
            SignUpActivity.start(this)
        }
        .setNegativeButton(R.string.cancel) { _, _ -> }
    builder.create().show()
}
nwo49xxi

nwo49xxi8#

只需将视图设置为null,这将简单地关闭AlertDialog。

相关问题