How to reject AlertDialog using radio buttons in android sdk

I created an alertdialog with two switches in it. When the user selects a parameter, I need to reject alertdialog, but I could not reject 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.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show(); } }); final AlertDialog alert = builder.create(); alert.show(); 

Can someone help me how to do this.

thanks

+6
source share
5 answers

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(); 
+12
source

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(); 
+2
source

Call

 dialog.dismiss() 

in onClick .

+1
source
 if (isChecked()) { dialog.dismiss(); } 
0
source

setSingleChoiceItems has a Dialog instance in it.

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

Use instance to reject it.

 dialog.dismiss(); 

OR

 dialog.cancel(); 
0
source

Source: https://habr.com/ru/post/890982/


All Articles