Android使用Checkbox实现单选 ( 简单使用 (一))

Android使用Checkbox实现单选 ( 简单使用 (一))

实现

通过android自带的checkbox实现单选框。

效果

在这里插入图片描述

第一步,修改activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //防止该页面子控件过多无法获取焦点
        android:descendantFocusability="blocksDescendants"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/click_cb_wc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:clickable="true"
            android:textColor="#fff"
            android:textSize="14sp" />

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"

            android:text="完成"
            android:textColor="#282828"
            android:textSize="14sp" />

        <CheckBox
            android:id="@+id/click_cb_wwc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:clickable="true"
            android:textColor="#fff"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:gravity="right"
            android:text="未完成"
            android:textColor="#282828"
            android:textSize="14sp" />
    </LinearLayout>


</LinearLayout>

第二步,修改MainActivity.java

package com.example.checkboxtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class MainActivity extends AppCompatActivity {
    CheckBox cb_wc;
    CheckBox cb_wwc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        cb_wc = (CheckBox) findViewById(R.id.click_cb_wc);
        cb_wwc = (CheckBox) findViewById(R.id.click_cb_wwc);
        //监听选中取消事件
        cb_wc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   if (isChecked){
                       cb_wc.setChecked(true);
                       cb_wwc.setChecked(false);
                   }else {
                       cb_wc.setChecked(false);
                   }
            }
        });
        cb_wwc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    cb_wwc.setChecked(true);
                    cb_wc.setChecked(false);
                }else {
                    cb_wwc.setChecked(false);
                }
            }
        });
    }
}

本篇只是基本使用,后续将会实现页面记住选中的状态以及选择后的传值,未选择的传值等。
有问题欢迎批评指正!

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪の星空朝酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值