0

嘿,Android 开发人员,我目前遇到以下代码的问题。我无法弄清楚我可以轻松引用哪个复选框被单击的方法,目前下面的代码只是一个首选项,当单击它时,会显示一个带有多个复选框的 AlertDialog。

目标是在选中该复选框时做一些特定的事情,但是我想对每个项目做一些不同的事情。

代码

Preference checkboxalert = (Preference) findPreference("checkboxalert");
           checkboxalert
            .setOnPreferenceClickListener(new OnPreferenceClickListener() {

        final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
        final ArrayList<Integer> selectedItems=new ArrayList<Integer>();

        public boolean onPreferenceClick(Preference preference) {
               AlertDialog.Builder builder = new AlertDialog.Builder(context);
                 builder.setTitle("Select The Difficulty Level");
                 builder.setMultiChoiceItems(items, null,
                   new DialogInterface.OnMultiChoiceClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int indexSelected,
                                 boolean isChecked) {
                        if (isChecked) { 
                            //WHERE I WANT TO REFERENCE WHICH CHECKBOX IS CLICKED

                            selectedItems.add(indexSelected);
                            Log.i("Preference - Checkbox", "Something was clicked");

                             } else if (selectedItems.contains(indexSelected)) { 
                                 selectedItems.remove(Integer.valueOf(indexSelected));
                             }
                         }
                     })
                     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int id) {
                         }
                     })
                     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                         @Override
                         public void onClick(DialogInterface dialog, int id) {
                         }
                     });
                        dialog = builder.create();
                        dialog.show();

                return true;
                }
            });
4

1 回答 1

1

使用 有什么问题index

@Override
public void onClick(DialogInterface dialog, int indexSelected,boolean isChecked) 
{
    if (isChecked) { 
     selectedItems.add(indexSelected);

    //WHERE I WANT TO REFERENCE WHICH CHECKBOX IS CLICKED
    switch (indexSelected)
    {
         case 0:
            // do something if the first box is checked
            break;
         case 1:
            // do something if the second box is checked
            break;
         ...
    }
}

除非我错过了您想要的东西,否则这似乎应该可行。

于 2013-10-13T03:19:06.680 回答