复选框是否具有像单选按钮一样的权限.我正在开发一个测验应用程序,其中选项中有单选按钮的行为,选项的图标就像复选框一样,我可以将复选框分组为我们组合单选按钮?
小智 22
如果你想要看起来像复选框的单选按钮.将RadioButton的样式设置为@android:style/Widget.CompoundButton.CheckBox
例如:
<RadioButton style="@android:style/Widget.CompoundButton.CheckBox" />
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是最好的解决方案,但您可以为复选框创建一个“管理器”,并在单击其中任何一个复选框时运行它。
为了简单起见,我在 xml 代码中添加了管理器,但也可以随意使用setOnClickListener或setOnCheckedChangeListener。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1"
android:onClick="cbgroupmanager"
/>
...
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox5"
android:onClick="cbgroupmanager"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
您需要一个 ArrayList 来迭代,这样您就可以在单击其中任何一个复选框时确定每个复选框的状态。
public class Q6910875 extends Activity
ArrayList<CheckBox> cb = new ArrayList<CheckBox>();
int CheckBoxNum = 5; //number of checkboxes
Iterator<CheckBox> itr ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cb.add((CheckBox) findViewById(R.id.checkBox1));
cb.add((CheckBox) findViewById(R.id.checkBox2));
cb.add((CheckBox) findViewById(R.id.checkBox3));
cb.add((CheckBox) findViewById(R.id.checkBox4));
cb.add((CheckBox) findViewById(R.id.checkBox5));
itr = cb.iterator();
}
Run Code Online (Sandbox Code Playgroud)
这里我们有我们的管理器,一个遍历整个列表的迭代器,取消选中所有内容,当它到达单击的列表时,进行检查!
public void cbgroupmanager(View v) {
CheckBox cbaux;
while(itr.hasNext()) {
cbaux = (CheckBox) itr.next(); // we need this because it returns a Object, and we need the setChecked, which is a CheckBox method.
Log.d("soa", "click");
if (cbaux.equals(v)) //if its the one clicked, mark it as checked!
cbaux.setChecked(true);
else
cbaux.setChecked(false);
}
Run Code Online (Sandbox Code Playgroud)
我还可以找到下面链接的其他解决方案,您可以更改复选框的主题,但我没有任何主题经验,因此我无法进一步帮助您了解这种方法。
| 归档时间: |
|
| 查看次数: |
7383 次 |
| 最近记录: |