【问题标题】:How to get dynamic check box id?如何获取动态复选框ID?
【发布时间】:2016-11-15 20:58:37
【问题描述】:

我在我的项目中使用以下代码制作动态复选框,例如如何获取复选框 id 并为其创建命令:

if(checkbox1.ischecked()==true){
//do 
}

提前谢谢你。

public void fetchContacts() {
    final LinearLayout Vll=(LinearLayout)findViewById(R.id.Vll);

    CheckBox cb = new CheckBox(getApplicationContext());
                        cb.setText(outputName+":"+outputNumber);
                        Vll.addView(cb);
}

【问题讨论】:

    标签: android select checkbox dynamic command


    【解决方案1】:

    您应该将所有CheckBox 的所有引用存储在ArrayList

    List<CheckBox > allCheckBox = new ArrayList<CheckBox>();
    

    当您创建任何CheckBox 时,您需要将其添加到您的List

    CheckBox cb = new CheckBox(getApplicationContext());
    ...
    allCheckBox.add(cb);
    

    之后您可以通过

    访问CheckBox
    allCheckBox.get(position).isChecked() == true
    // allCheckBox.get(position) -> with return a CheckBox
    // position is the index when you add checkbox to ArrayList, for example you have create 2 CheckBox dynamic
    // then CheckBox1 is allCheckBox.get(0) and CheckBox2 is allCheckBox.get(1)
    

    【讨论】:

    • allCheckBox.get(position).isChecked() == true
    • 我必须把鳕鱼放在哪里?
    • @mrreza 如果您认为我的回答解决了您的问题,请将其标记为正确答案
    【解决方案2】:

    你可以这样做

    cb.setId(anyPositiveInteger)
    

    试试这个以了解更多详情How can I assign an ID to a view programmatically?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-29
      • 2023-03-04
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2019-12-20
      相关资源
      最近更新 更多