Android的复选框对话框(简易) [英] Android Checkbox Dialog (Easy)

查看:153
本文介绍了Android的复选框对话框(简易)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框一个对话框,我试图做不同的事情时,选项被选中,没事的时候是pressed。我想我知道读了一些教程后,我在做什么,但是当我preSS还好只是祝酒词一切,即使它不被选中。这样看来,我的if语句工作不正常,但我不知道为什么。

这是我在做什么错误,以及如何解决这将是极大的AP preciated任何建议!

 最后的CharSequence []项目= {项目1,项目2,3项};
    最终布尔[]状态= {假的,假的,假};
    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    builder.setTitle(什么你想干什么?);
    builder.setMultiChoiceItems(项目,国家,新DialogInterface.OnMultiChoiceClickListener(){
        公共无效的onClick(DialogInterface dialogInterface,INT项,布尔州){
        }
    });
    builder.setPositiveButton(好,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话框,INT ID){
            。SparseBooleanArray选中=((AlertDialog)对话框).getListView()getCheckedItemPositions();
            如果(CheCked.get(CheCked.keyAt(0))==真){
                Toast.makeText(Backup.this项目1,Toast.LENGTH_SHORT).show();
            }
            如果(CheCked.get(CheCked.keyAt(1))==真){
                Toast.makeText(Backup.this,项目2,Toast.LENGTH_SHORT).show();
            }
            如果(CheCked.get(CheCked.keyAt(2))==真){
                Toast.makeText(Backup.this,第3项,Toast.LENGTH_SHORT).show();
            }
        }
    });
    builder.setNegativeButton(取消,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话框,INT ID){
             dialog.cancel();
        }
    });
    builder.create()显示()。
}
 

解决方案

现在的问题是,你使用 keyAt ,其中,根据API文档

  

返回从index个关键   键 - 值映射,这   SparseBooleanArray店。

和本 SparseBooleanArray 包含您的列表中只检查的项目( getCheckedItemPositions )!

所以,如果你检查两个项目三个,那么检查成员将有2款产品,而当你调用 keyAt(2)则返回0(不是 IndexOutOfBoundsException异常,但在这个位置上的地图不保值)。

你应该使用的仅仅是这个 GET 方法 SparseBooleanArray ,从你的<$项目位置C $ C>的ListView 作为参数:

  builder.setPositiveButton(好,新DialogInterface.OnClickListener()
{
    公共无效的onClick(DialogInterface对话框,INT ID)
    {
        。SparseBooleanArray选中=((AlertDialog)对话框).getListView()getCheckedItemPositions();
        如果(CheCked.get(0))
        {
            Toast.makeText(TmpActivity2.this项目1,Toast.LENGTH_SHORT).show();
        }
        如果(CheCked.get(1))
        {
            Toast.makeText(TmpActivity2.this,项目2,Toast.LENGTH_SHORT).show();
        }
        如果(CheCked.get(2))
        {
            Toast.makeText(TmpActivity2.this,第3项,Toast.LENGTH_SHORT).show();
        }
    }
});
 

您也可以松 ==真部分条款的话,但是这只是语义。

如果你看一下在 getCheckedItemPositions 方法 API文档,你会发现这个解决方案:

返回
A SparseBooleanArray 将返回true每次调用 的get(INT位置) ,其中位置在列表中的位置,或如果选择的模式设置为 CHOICE_MODE_NONE

I have a dialog with checkboxes and I was trying to do different things when options are selected and when okay is pressed. I thought I knew what I was doing after reading some tutorials, but when I press okay it just toasts "Everything" even if it isn't checked. So it seems that my if statements are not working correctly, but I don't know why.

Any suggestions on what I'm doing wrong and how to fix it would be greatly appreciated!

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

解决方案

The problem is that you use keyAt, which, according to API Docs

returns the key from the indexth key-value mapping that this SparseBooleanArray stores.

and this SparseBooleanArray contains only the checked items of your list (getCheckedItemPositions)!

So if you check two items of three, then the CheCked member will contain 2 items, and when you call keyAt(2) it will return 0 (not IndexOutOfBoundsException, though at that position the map does not hold value).

What you should use is simply the get method of this SparseBooleanArray, with the item positions from your ListView as arguments:

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

You can also loose the == true part of the if clause, but that's just semantics.

If you take a look at the API Docs of the getCheckedItemPositions method, you find this solution:

Returns:
A SparseBooleanArray which will return true for each call to get(int position) where position is a position in the list, or null if the choice mode is set to CHOICE_MODE_NONE.

这篇关于Android的复选框对话框(简易)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆