CheckBox的单选框实现

本文介绍了三种实现单选框的方法:直接使用CheckBox通过代码控制,利用RadioGroup和自定义RadioButton,以及结合CheckBox和ListView。每种方法都有其优缺点,如CheckBox实现简单但需设置ID,RadioButton布局简洁但调整困难,而CheckBox+ListView则布局简单但代码复杂。
摘要由CSDN通过智能技术生成

这里有三种方法可以实现:
第一种是:直接使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值