简体   繁体   English

如何在android sdk中使用单选按钮关闭AlertDialog

[英]how to dismiss AlertDialog with radio buttons in android sdk

i have created a alertdialog with two radio buttons in it.when user select an option i need to dismiss the alertdialog but i am not been able to dismiss it. 我已经创建了一个带有两个单选按钮的alertdialog。当用户选择一个选项时我需要关闭alertdialog但是我无法解雇它。

 final CharSequence[] items = {"First Option", "Second Option"};

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("Choose an option"); 
 builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
       public void  onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
       } 
 });
 final AlertDialog alert = builder.create();
 alert.show();

Can someone help me how to do this. 有人可以帮我怎么做。

Thanks 谢谢

Please try this.. 请试试这个..

final CharSequence[] items = {"First Option", "Second Option"};

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
 builder.setTitle("Choose an option"); 
 builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
   public void  onClick(DialogInterface dialog, int item) {
        dialog.dismiss();
        Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
   } 
});
final AlertDialog alert = builder.create();
alert.show();

Just add 只需添加

dialog.cancel();


final CharSequence[] items = {"First Option", "Second Option"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose an option"); 
        builder.setSingleChoiceItems(items, -1, new  DialogInterface.OnClickListener() { 
              public void  onClick(DialogInterface dialog, int item) {
                   Toast.makeText(getApplicationContext(), items[item],  Toast.LENGTH_SHORT).show(); 
                   dialog.cancel();
              } 
        });
        final AlertDialog alert = builder.create();
        alert.show();

Call 呼叫

dialog.dismiss()

in onClick . onClick

setSingleChoiceItems has Dialog instance in it. setSingleChoiceItems有Dialog instance

@Override
public void onClick(DialogInterface dialog, int which) { // here is the instance
          countryBtn.setHint(countryList.get(which));
          dialog.cancel();
         }

Use that instance to dismiss it either by. 使用该instance来解雇它。

dialog.dismiss();

OR 要么

dialog.cancel();
if (isChecked())
{
  dialog.dismiss();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM