Android Checkbox对话框(简易)

时间:2011-05-12 19:02:27

标签: android checkbox dialog if-statement boolean

我有一个带复选框的对话框,当我选择了选项并按下时,我试图做不同的事情。在我阅读了一些教程之后,我以为我知道自己在做什么,但是当我按下它时,即使没有检查也只是为了“祝你好运”。所以看来我的if语句不能正常工作,但我不知道为什么。

对于我做错了什么以及如何修复它的任何建议都将不胜感激!

    final CharSequence[] items = {"Item 1", "Item 2", "Item 3"};
    final boolean[] states = {false, false, false};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("What would you like to do?");
    builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener(){
        public void onClick(DialogInterface dialogInterface, int item, boolean state) {
        }
    });
    builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            SparseBooleanArray CheCked = ((AlertDialog)dialog).getListView().getCheckedItemPositions();
            if(CheCked.get(CheCked.keyAt(0)) == true){
                Toast.makeText(Backup.this, "Item 1", Toast.LENGTH_SHORT).show();
            }
            if(CheCked.get(CheCked.keyAt(1)) == true){
                Toast.makeText(Backup.this, "Item 2", Toast.LENGTH_SHORT).show();
            }
            if(CheCked.get(CheCked.keyAt(2)) == true){
                Toast.makeText(Backup.this, "Item 3", Toast.LENGTH_SHORT).show();
            }
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
             dialog.cancel();
        }
    });
    builder.create().show();
}

3 个答案:

答案 0 :(得分:10)

我会这样做,例如:

public class Main extends Activity {

    CharSequence[] items = {"Google", "Apple", "Microsoft"};
    boolean[] itemsChecked = new boolean[items.length];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.btnDialog);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(0);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("Dialog with simple text")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    for (int i = 0; i < items.length; i++) {
                    if (itemsChecked[i]) {
                        Toast.makeText(getBaseContext(), items[i] + " checked!", Toast.LENGTH_LONG).show();
                    }
                }
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_LONG).show();
                }
            })
            .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Toast.makeText(getBaseContext(), items[which] + (isChecked ? "checked!" : "unchecked!"), Toast.LENGTH_SHORT).show();
                }
            })
            .create();
        }

        return null;
    }


}

答案 1 :(得分:7)

问题是您使用keyAt,根据API文档

  

从indexth返回密钥   这个键值映射   SparseBooleanArray存储。

SparseBooleanArray仅包含列表中的已检查项目(getCheckedItemPositions)!

因此,如果您检查两项中的三项,则CheCked成员将包含2项,当您致电keyAt(2)时,它将返回0(不是IndexOutOfBoundsException,但在该位置地图不具有价值)。

您应该使用的只是此get的{​​{1}}方法,其中SparseBooleanArray的项目位置为参数:

ListView

你也可以松开if子句的builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { SparseBooleanArray CheCked = ((AlertDialog) dialog).getListView().getCheckedItemPositions(); if (CheCked.get(0)) { Toast.makeText(TmpActivity2.this, "Item 1", Toast.LENGTH_SHORT).show(); } if (CheCked.get(1)) { Toast.makeText(TmpActivity2.this, "Item 2", Toast.LENGTH_SHORT).show(); } if (CheCked.get(2)) { Toast.makeText(TmpActivity2.this, "Item 3", Toast.LENGTH_SHORT).show(); } } }); 部分,但那只是语义。

如果您查看== true方法的 API文档,就会找到此解决方案:

<强>返回
getCheckedItemPositions对于 SparseBooleanArray 的每次调用都会返回true,其中get(int position)是列表中的位置,如果选择模式为position则为null设为CHOICE_MODE_NONE

答案 2 :(得分:2)

这应该有帮助

.setMultiChoiceItems(items, itemsChecked,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked) {




                                Toast.makeText(
                                        getBaseContext(),
                                        items[which]
                                                + (isChecked ? " checked!"
                                                        : " unchecked!"),
                                        Toast.LENGTH_SHORT).show();
                            }
                        })

但是,你如何在另一个班级中使用被检查的项目?