开发者

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.

 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.makeTe开发者_StackOverflow中文版xt(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.


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


setSingleChoiceItems has Dialog instance in it.

@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.

dialog.dismiss();

OR

dialog.cancel();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜