【问题标题】:check box should like radio buttons in android复选框应该像 android 中的单选按钮
【发布时间】:2014-05-12 14:20:14
【问题描述】:

我有一个要求,比如复选框应该像单选按钮一样工作。

我以编程方式相对于数组排列复选框(如果数组大小为 10,则以编程方式放置 10 个复选框)。

如果选中了一个复选框,则应取消选中其他复选框(应该像单选按钮一样工作)。

单击复选框后,应生成复选框 ID。

请给我一个想法。

谢谢, 乌代塞卡

【问题讨论】:

    标签: android checkbox


    【解决方案1】:
    <RadioButton style="@android:style/Widget.CompoundButton.CheckBox" />`
    

    这就是你如何获得复选框的外观

    【讨论】:

      【解决方案2】:

      为每个复选框提供 OnCheckChangeListener() 的通用实现。

      CompoundButton.OnCheckedChangeListener() listener = new CompoundButton.OnCheckedChangeListener() {  
         @override  
          onCheckedChange((CompoundButton buttonView, boolean isChecked){  
                   int id = buttonView.getId();  
                     int[] ids = new int[]{R.id.cb1,R.id.cb2 .....R.id.cb10};    
                     for (int i=0;i<10;i++){    
                         CheckBox cb =(CheckBox)view.findViewById(ids[i]);  
                         if(id==ids[i]{  
                             cb.setChecked(isChecked);}  
                         else  
                             cb.setChecked(false);         
                     }  
      };  
      

      view 是视图对象。 希望这会有所帮助

      【讨论】:

      • 它可以工作,但是您需要设置 onClickListner 否则当您将 cb.setCheckted 设置为某个值时它会执行递归调用
      【解决方案3】:

      只需在复选框中添加样式

      style="@android:style/Widget.CompoundButton.RadioButton"
      

      如果您想将单选按钮更改为复选框,则只需将以下行添加到您的 xml 中

      style="@android:style/Widget.CompoundButton.CheckBox"
      

      【讨论】:

        猜你喜欢
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 2020-04-05
        • 2011-06-09
        相关资源
        最近更新 更多