Android开发——控件基础(六)CheckBox组件:属性、示例代码

实现界面:

复选框经典用法、自定义复选框按钮、设置复选框监听

常用属性:

android:text    设置选项文本
android:button     自定义复选框按钮
android:textSize   设置文本字体大小
android:checked="true"    设置默认选中
android:textColor    设置文本颜色
android:textSize    设置文本大小

代码实现:

activity_check_box.xml:布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_check_box"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.exercise.CheckBoxActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你会哪些编程语言?"
        android:textSize="16sp"
        android:textColor="@color/colorBlack"
        android:id="@+id/tv_1"
        android:layout_marginBottom="10dp"/>
    <!--设置复选框的字体、选中、内容-->
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_1"
        android:text="Java"
        android:checked="true"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_1"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_1"
        android:text="C++"
        android:checked="true"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_2"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_2"
        android:text="Python"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_3"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_3"
        android:text="PHP"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_4"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_4"
        android:text="C"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_5"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="你有哪些爱好?"
        android:id="@+id/tv_2"
        android:textSize="16sp"
        android:textColor="@color/colorBlack"
        android:layout_below="@+id/cb_5"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

    <!--设置自定义选项框-->
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_2"
        android:text="编程"
        android:textColor="@color/colorOrange"
        android:button="@drawable/selector_orange_checkbox"
        android:paddingLeft="5dp"
        android:textSize="14sp"
        android:id="@+id/cb_6"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_6"
        android:text="马云演讲"
        android:paddingLeft="5dp"
        android:button="@drawable/selector_orange_checkbox"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_7"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_7"
        android:paddingLeft="5dp"

        
        android:text="励志电影"
        android:button="@drawable/selector_orange_checkbox"
        android:textColor="@color/colorOrange"
        android:textSize="14sp"
        android:id="@+id/cb_8"/>
</RelativeLayout>
CheckBoxActivity:java文件
package com.example.administrator.exercise;

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

public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
    private CheckBox checkBox,checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,
    checkBox7;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        checkBox = (CheckBox) findViewById(R.id.cb_1);
        checkBox.setOnCheckedChangeListener(this);
        checkBox1 = (CheckBox) findViewById(R.id.cb_2);
        checkBox1.setOnCheckedChangeListener(this);
        checkBox2 = (CheckBox) findViewById(R.id.cb_3);
        checkBox2.setOnCheckedChangeListener(this);
        checkBox3 = (CheckBox) findViewById(R.id.cb_4);
        checkBox3.setOnCheckedChangeListener(this);
        checkBox4 = (CheckBox) findViewById(R.id.cb_5);
        checkBox4.setOnCheckedChangeListener(this);
        checkBox5 = (CheckBox) findViewById(R.id.cb_6);
        checkBox5.setOnCheckedChangeListener(this);
        checkBox6 = (CheckBox) findViewById(R.id.cb_7);
        checkBox6.setOnCheckedChangeListener(this);
        checkBox7 = (CheckBox) findViewById(R.id.cb_8);
        checkBox7.setOnCheckedChangeListener(this);
    }
    //监听器的参数
    //第一个参数:被点击的按钮
    //第二个参数:是否选中
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this,b?compoundButton.getText():"未选中",Toast.LENGTH_SHORT).show();
    }
}

 selector_orange_checkbox.xml:自定义背景文件

选中时选取背景1,未选中时选取背景2

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/icon_checked_true"/>
    <item android:state_checked="false" android:drawable="@drawable/icon_checked_false"/>
</selector>

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

狮子座的程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值