简体   繁体   中英

How to set checkbox checked and disabled

I have this code for showing list of languages for download:

public void onCreateDialog(ArrayList<String>fullLangArray, final ArrayList<String>codeLangArray) {

    final String[] items = fullLangArray.toArray(new String[fullLangArray.size()]);

    final ArrayList mSelectedItems = new ArrayList();

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    // Set the dialog title
    builder.setTitle("Updates...")
            .setMultiChoiceItems(items, null,
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int indexSelected,
                                            boolean isChecked) {
                            if (isChecked) {
                                  mSelectedItems.add(Utils.SERVER_ADDRESS + "/" + codeLangArray.get(indexSelected) + ".zip");
                            } else if (mSelectedItems.contains(indexSelected)) {
                                  mSelectedItems.remove(Integer.valueOf(indexSelected));
                            }
                        }
                    })

            .setPositiveButton("Download", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                    DownloadTask downloadTask = new DownloadTask(MainActivity.this);
                    downloadTask.execute(mSelectedItems.toString());

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {

                }
            });

    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

I want to make a one item of a checkbox is checked and " disabled " like in a photo ( Option 3 ) when AlertDialog is loaded.

在此处输入图片说明

Can you help me how to do it?

You can check the checkbox by using the setChecked() method which boolean value as parameter.

Example:

   option1.setChecked(true);

and also uncheck it using

   option2.setChecked(false);

If you want to set it to checked and disabled you have use setEnabled() which takes boolean as it's parameters.

Example.

option3.setChecked(true);
option3.setEnabled(false); 

This will disable your checkbox and even check it. I hope this was helpful. ThankYou.

For setting Opacity

mSelectedItems.getBackground().setAlpha(128);

Where the INT ranges from 0 (fully transparent) to 255 (fully opaque).

For setChecked item

mSelectedItems.setChecked(true);

Disable checking

mSelectedItems.setEnabled(false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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