这里有三种方法可以实现:
第一种是:直接使用CheckBox,再使用代码来修改checkBox的选中状态;
优点:实现简单,容易懂
缺点:需要给每个checkBox设置id
第二种是:在RadioGroup中使用自定义的RadioButton(其实就是使用图片,改变效果);
优点:代码简单,多使用的是布局文件
缺点:布局文件不好调
第三种是:使用CheckBox+ListView实现;
优点:布局简单
缺点:代码多而复杂
一、第一种方法,只使用CheckBox实现单选
(1)配置文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#F5F5F5" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="以下情况,请代表根据实际情况打勾注明:"
android:textColor="#5E5E5E"
android:textSize="15sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:text="1.通过调查研究形成:"
android:textColor="#9CE1FF"
android:textSize="15sp" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:checked="true"
android:text="专题调研"
android:textColor="#5E5E5E"
android:textSize="15sp" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="视察"
android:textColor="#5E5E5E"
android:textSize="15sp" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="座谈"
android:textColor="#5E5E5E"
android:textSize="15sp" />
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="走访等其他调研方式"
android:textColor="#5E5E5E"
android:textSize="15sp" />
</LinearLayout>
(2)主方法MainActivity.java
package com.example;
import com.example.aaa.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
public class MainActivity extends Act