How to set default Android checkbox for android with warning?

any authority will help me ... how to set a default checkbox by default with a warning dialog at startup.?

this is my code, for example: I want to set the radio button at startup, where the elements are "15"

public void showDialog() { final CharSequence[] items = {"5", "10", "15","20"}; AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setTitle("Set limit article"); alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(SettingAppDisplay.this, "You selected item No." + item + ": " + items[item], Toast.LENGTH_SHORT).show(); if (items[item].equals("5")) { //do what you want } else if (items[item].equals("10")) { //do what you want } else if (items[item].equals("15")) { //do what you want } else if (items[item].equals("20")) { //do what you want } dialog.dismiss(); } }); alertDialogBuilder.show(); } 

Thank you, your paticipation .. sorry, my english :)

+4
source share
3 answers

Change the second argument (checkedItem) in setSingleChoiceItems from -1 to any switch position you want to check, here I changed it to "1", so the first radio button will be checked.

  alertDialogBuilder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { Toast.makeText(SettingAppDisplay.this, "You selected item No." + item + ": " + items[item], Toast.LENGTH_SHORT).show(); if (items[item].equals("5")) { //do what you want } else if (items[item].equals("10")) { //do what you want } else if (items[item].equals("15")) { //do what you want } else if (items[item].equals("20")) { //do what you want } dialog.dismiss(); } }); 

See docs

 setSingleChoiceItems (Cursor cursor, int checkedItem, String labelColumn, DialogInterface.OnClickListener listener) 

Options

cursor cursor to extract items from.

checkedItem indicates which item is checked. If -1 items are not marked.

labelColumn The name of the column in the cursor containing the row displayed in the label.

The listener is notified when an item in the list is clicked. The dialog will not be rejected when an item is clicked. It will be rejected only when the button is clicked, if no buttons are provided to the user to close the dialog box.

+6
source

Please check the following android.app.AlertDialog.Builder.setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener) Give the integer position value of the elements as the second checkedItem parameter.

To make the default value as the 15th element, do the following

 alertDialogBuilder.setSingleChoiceItems(items, 14, new DialogInterface.OnClickListener() 
0
source

The element marked with a deft is set by the middle argument in setSingleChoiceItems

 alertDialogBuilder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 

In your code you have set the value to -1, which means that by default the item will not be selected. Just change the value to the one selected in your array. Remember to start at 0 for the first and count your path to the item you want to select.

0
source

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


All Articles