【问题标题】:How to get value of selected checked boxes which are created programatically in android如何获取在android中以编程方式创建的选定复选框的值
【发布时间】:2014-04-16 05:09:03
【问题描述】:

我用来创建复选框的代码

try{
for (int i = 0; i < Utstyr.size(); i++) {
cb = new CheckBox(getApplicationContext());
cb.setText(""+Utstyr.get(i));
cb.setTextColor(Color.parseColor("#000000"));
cb.setTag(""+list_sted.get(i));
cb.setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle);
checkbox_lay.addView(cb);
}}
catch(Exception e){
 System.out.println("ohh i got busted...!!!");
}

如何获取选中的复选框的值.. 我想要复选框的名称

【问题讨论】:

    标签: android eclipse tags android-checkbox


    【解决方案1】:
    CheckBox[] chkArray = new CheckBox[Utstyr.size()];//
    for (int i = 0; i < Utstyr.size(); i++) {
        chkArray[i] = new CheckBox(getApplicationContext());
        chkArray[i].setText(""+Utstyr.get(i));
        chkArray[i].setTextColor(Color.parseColor("#000000"));
        chkArray[i].setTag(""+Utstyr.get(i));
        chkArray[i].setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle);
        checkbox_lay.addView(chkArray[i]);
    }
    for (int k = 0; k < Utstyr.size(); k++){
        if(chkArray[k].isChecked()){
            //Do something
        }
    }
    

    希望这会有所帮助.. :)

    【讨论】:

    • 查看我的编辑。没有说它的确切原因,但它可能是一个原因。
    • if(chkArray[k].isChecked()==true) 给出 java.lang.NullPointerException
    • @ManishYadav > 我可以看到它变得多么糟糕。 :P 可能你被否决了。顺便说一句,我可以知道复选框数组的用途吗?因为有一个更好的东西叫做 simple_list_item_single_choice。
    【解决方案2】:

    使用循环生成复选框,不需要设置全局变量。

    for (int i = 0; i < Utstyr.size(); i++) {
    cb = new CheckBox(getApplicationContext());
    ......
    }
    

    你必须像下面这样初始化而不是上面的代码。

     for (int i = 0; i < Utstyr.size(); i++) {
        CheckBox cb = new CheckBox(getApplicationContext());
        ......
        }
    

    要获得选中的复选框,您必须使用以下代码

    int childcount = checkbox_lay.getChildCount();
    for (int i=0; i < childcount; i++){
          View v = checkbox_lay.getChildAt(i);
    
         if(v instanceof Checkbox){
    
        Checkbox ck=(Checkbox)v;
           boolean isSelected = ch.isChecked(); 
          }
    
    }
    

    【讨论】:

      【解决方案3】:

      您需要参考这些复选框。 创建一组复选框并在每次创建时添加它,然后您就可以从它们那里得到您需要的...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多