如何closuresandroid中的AlertDialog

我创build了包含4个button的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()方法后解雇它,我该怎么做? 提前致谢。

Solutions Collecting From Web of "如何closuresandroid中的AlertDialog"

实际上AlertDialog.Builder类没有任何cancel()dismiss()方法。

所以,而不是AlertDialog.Builder OptionDialog使用AlertDialog实例。

喜欢,

 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(); } }); 

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

 AlertDialog.Builder db = new AlertDialog.Builder(context); db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface d, int arg1) { d.cancel(); }; }); 

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

尝试这个:

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

有两种方法可以closures警报对话框。

选项1:

AlertDialog#create().dismiss();

选项2:

The DialogInterface#dismiss();

开箱即用,框架调用DialogInterface#dismiss(); 当您为button定义事件侦听器时:

  1. AlertDialog#setNegativeButton();
  2. AlertDialog#setPositiveButton();
  3. AlertDialog#setNeutralButton();

提示对话框。

试试这个 –

 OptionDialog.create().dismiss(); 

希望这会解决你的问题。

这里是我如何closures我的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; } }); 

只需将视图设置为null,即可简单地closuresAlertDialog。

closures或取消AlertDialog.Builder

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

你可以在对话界面上调用dismiss()