如何设置复选框已选中和已禁用

时间:2016-03-29 06:20:08

标签: java android android-alertdialog android-checkbox

我有此代码用于显示下载语言列表:

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();
}

我想要选中一个复选框,然后选中&#34; 禁用&#34;加载AlertDialog时的照片(选项3 )。

enter image description here

你能帮我怎么做吗?

2 个答案:

答案 0 :(得分:2)

您可以使用setChecked()方法检查复选框,其中布尔值为参数。

示例:

   option1.setChecked(true);

并使用

取消选中它
   option2.setChecked(false);

如果要将其设置为选中和禁用,则使用setEnabled(),它将布尔值作为参数。

实施例

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

这将禁用您的复选框甚至检查它。我希望这可以帮到你。 THANKYOU。

答案 1 :(得分:0)

用于设置不透明度

  。

mSelectedItems.getBackground()setAlpha(128);

INT的范围从0(完全透明)到255(完全不透明)。

适用于setChecked项目

  

mSelectedItems.setChecked(真);

禁用检查

  

mSelectedItems.setEnabled(假)

相关问题