安卓基础之《(8)—中级控件(2)选择按钮》

一、复选框CheckBox

1、CompoundButton类是抽象的复合按钮,由它派生而来的子类包括:复选框CheckBox、单选按钮RadioButton以及开关按钮Switch

2、下图描述了复合按钮的继承关系

在Android体系中,CompoundButton类是抽象的复合按钮,因为是抽象类,所以它不能直接使用。实际开发中用的是CompoundButton的几个派生类,主要有复选框CheckBox、单选按钮RadioButton以及开关按钮Switch,这些派生类均可使用CompoundButton的属性和方法。加之CompoundButton本身继承了Button类,故以上几种按钮同时具备Button的属性和方法

3、CompoundButton在XML文件中主要使用下面两个属性

(1)checked:指定按钮的勾选状态,true表示勾选,false则表示未勾选。默认为未勾选

(2)button:指定左侧勾选图标的图形资源,如果不指定就使用系统的默认图标

4、CompoundButton在Java代码中主要使用下列4种方法

(1)setChecked:设置按钮的勾选状态

(2)setButtonDrawable:设置左侧勾选图标的图形资源

(3)setOnCheckedChangeListener:设置勾选状态变化的监听器

(4)isChecked:判断按钮是否勾选

5、例子

checkbox_selector.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/check_choose" android:state_checked="true" />
    <item android:drawable="@drawable/check_unchoose" android:state_checked="false" />

</selector>

activity_check_box.xml

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"
    android:padding="5dp"
    tools:context=".CheckBoxActivity">

    <CheckBox
        android:id="@+id/ck_system"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="这是系统的CheckBox"/>

    <CheckBox
        android:id="@+id/ck_custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_selector"
        android:padding="5dp"
        android:text="这个CheckBox换了图标"/>

</LinearLayout>

CheckBoxActivity.java

java 复制代码
package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        CheckBox ck_system = findViewById(R.id.ck_system);
        CheckBox ck_custom = findViewById(R.id.ck_custom);

        ck_system.setOnCheckedChangeListener(this);
        ck_custom.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String desc = String.format("您%s了这个CheckBox", b ? "勾选" : "取消勾选");
        compoundButton.setText(desc);
    }
}
相关推荐
鸣弦artha30 分钟前
Flutter 框架跨平台鸿蒙开发 —— Image Widget 图片处理:圆角、裁剪、阴影
android·flutter·harmonyos
—Qeyser39 分钟前
Flutter ListView 列表组件完全指南
android·flutter·ios
独自破碎E1 小时前
包含min函数的栈
android·java·开发语言·leetcode
毕设源码-邱学长1 小时前
【开题答辩全过程】以 基于Android的健康码系统架构为例,包含答辩的问题和答案
android·系统架构
冬奇Lab1 小时前
稳定性性能系列之十五——系统稳定性监控体系建设:从指标到预警的完整方案
android·性能优化·debug
沈千秋.1 小时前
简单文件包含案例
android·ide·android studio·文件包含
冬奇Lab1 小时前
【Kotlin系列06】面向对象进阶:从接口到多态,设计灵活可扩展的代码
android·kotlin·编程语言
·云扬·2 小时前
ClickHouse数据备份与恢复实战:从基础操作到工具应用
android·java·clickhouse
消失的旧时光-19432 小时前
Android + Flutter 混合架构全景图:从接入到系统的完整方法论
android·flutter
MengFly_2 小时前
Compose案例 — Android 调用系统相机拍照
android·kotlin·compose