簡體   English   中英

android:復選框問題?

[英]android : checkbox problem?

我正在嘗試選中列表中的所有復選框。 為什么要獲得特定復選框,只有true。 代碼是:-

            ListView listview = (ListView)findViewById(R.id.listview1);
        for(int i=0; i < listview.getChildCount(); i++)
        {
            AbsoluteLayout itemLayout = (AbsoluteLayout)listview.getChildAt(i);
            CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkBox1);
            if(cb.isChecked())
            {
                cb.setChecked(false);
            }
            else
            {
                cb.setChecked(true);
            }
        }

提前致謝。

不確定我是否完全理解這個問題。

但我相信:

CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.checkBox1);

即使您的列表中有多個復選框,也將始終返回相同的CheckBox(ID為checkBox1的CheckBox)。

嘗試:

   for(int i=0; i < listview.getChildCount(); i++)
        {
            CheckBox cb = (CheckBox)listview.getChildAt(i).findViewById(R.id.checkBox1);  //if the child views are properly populated in each row item then on this particular positon ChBox will be found and instantiated
            if(cb.isChecked())
            {
                cb.setChecked(false);
            }
            else
            {
                cb.setChecked(true);
            }
        }

您可以通過全selectallunselectall兩個按鈕來使用它

    public void checkallbtn_Click(View view)
{
    ListView lv = (ListView)findViewById(R.id.backuplist);
    CheckBox cb;
    try
    {
        for(int i=0;i<lv.getCount();i++)
        {
            cb = (CheckBox)lv.getChildAt(i).findViewById(R.id.checkBox1);
            if(!cb.isChecked())
            {
                cb.setChecked(true);
            }
        }

    }catch(Exception e) {e.printStackTrace();}
}
public void uncheckallbtn_Click(View view)
{
    ListView lv = (ListView)findViewById(R.id.backuplist);
    try
    {
        for(int i=0;i<lv.getCount();i++)
        {
            CheckBox cb = (CheckBox)lv.getChildAt(i).findViewById(R.id.checkBox1);
            if(cb.isChecked())
            {
                cb.setChecked(false);
            }
        }           
    }catch(Exception e) 
    {
        e.printStackTrace();
    }
}

希望這會幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM