Android Checkbox对话框(简易)

GFl*_*lam 14 checkbox android if-statement boolean dialog

我有一个带复选框的对话框,当我选择了选项并按下时,我试图做不同的事情.在我阅读了一些教程之后,我以为我知道自己在做什么,但是当我按下它时,即使没有检查,它也只是祝酒"Everything".所以似乎我的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();
}
Run Code Online (Sandbox Code Playgroud)

evi*_*one 11

我会这样做,例如:

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


}
Run Code Online (Sandbox Code Playgroud)


rek*_*eru 7

问题是你使用的keyAt,根据API Docs

从此SparseBooleanArray存储的indexth键值映射返回键.

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

因此,如果您检查两个项目中的三个,那么该CheCked成员将包含2个项目,并且当您调用keyAt(2)它时将返回0(不是IndexOutOfBoundsException,虽然在该位置该地图不保持值).

你应该使用的只是这个get方法,SparseBooleanArray项目位置来自你ListView的参数:

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();
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

你也可以松开== trueif子句的一部分,但那只是语义.

如果您查看该方法的API文档if,您会找到以下解决方案:

返回:
一个getCheckedItemPositions将返回true,每次调用SparseBooleanArray其中get(int position)在列表中的位置,或者position如果选择的模式设置为null.