Android 中 CheckBox 的基本使用

    • 1. 基本属性
    • 2. 自定义样式
    • 3. 监听事件

还有其他一些 Button 控件, 使用一样: ToggleButton, Switch.

1. 基本属性

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:padding="15dp"    tools:context=".CheckBoxActivity">    <TextView        android:id="@+id/cb_tv_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="15dp"        android:text="@string/cb_tv_text"        android:textColor="@color/black"        android:textSize="21sp" />    <CheckBox        android:id="@+id/cb_1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/cb_tv_1"        android:checked="true"        android:text="@string/Android"        android:textColor="@color/blue"        android:textSize="20sp" />    <CheckBox        android:id="@+id/cb_2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/cb_1"        android:text="@string/Java"        android:textColor="@color/black"        android:textSize="20sp" />    <CheckBox        android:id="@+id/cb_3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/cb_2"        android:text="@string/Python"        android:textColor="@color/orange_low"        android:textSize="20sp" />    <CheckBox        android:id="@+id/cb_4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/cb_3"        android:text="@string/PHP"        android:textColor="@color/purple_700"        android:textSize="20sp" />    <CheckBox        android:id="@+id/cb_5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/cb_4"        android:text="@string/H5"        android:textColor="@color/teal_700"        android:textSize="20sp" />    <CheckBox        android:id="@+id/cb_6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/cb_5"        android:text="@string/other"        android:textColor="@color/blue"        android:textSize="20sp" />RelativeLayout>

2. 自定义样式

    <LinearLayout        android:id="@+id/cb_ll_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/cb_6"        android:layout_marginTop="15dp"        android:orientation="vertical">        <TextView            android:id="@+id/cb_tv_2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginBottom="15dp"            android:text="@string/cb_tv_text2"            android:textColor="@color/black"            android:textSize="21sp" />        <CheckBox            android:id="@+id/cb_7"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginBottom="10dp"            android:button="@drawable/bg_checkbox"            android:checked="true"            android:paddingLeft="5dp"            android:paddingRight="5dp"            android:text="@string/chess"            android:textColor="@color/purple_700"            android:textSize="20sp" />        <CheckBox            android:id="@+id/cb_8"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:button="@drawable/bg_checkbox"            android:paddingLeft="5dp"            android:paddingRight="5dp"            android:text="@string/swimming"            android:textColor="@color/purple_700"            android:textSize="20sp" />    LinearLayout>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/check_true" android:state_checked="true" />    <item android:drawable="@mipmap/check_false" android:state_checked="false" />selector>

3. 监听事件

package com.example.hello;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.Toast;public class CheckBoxActivity extends AppCompatActivity {         // 声明    private CheckBox cb7, cb8;    @Override    protected void onCreate(Bundle savedInstanceState) {             super.onCreate(savedInstanceState);        setContentView(R.layout.activity_check_box);        // 得到 CheckBox        cb7 = findViewById(R.id.cb_7);        cb8 = findViewById(R.id.cb_8);        // 监听事件        cb7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                 @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                     Toast.makeText(CheckBoxActivity.this, isChecked ? "选中: " + buttonView.getText() : "未选中: " + buttonView.getText(), Toast.LENGTH_SHORT).show();            }        });        cb8.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {                 @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                     Toast.makeText(CheckBoxActivity.this, isChecked ? "选中: " + buttonView.getText() : "未选中: " + buttonView.getText(), Toast.LENGTH_SHORT).show();            }        });    }}

更多相关文章

  1. Android(安卓)利用方向传感器实现 指南针
  2. Android在全屏状态下键盘覆盖输入框问题
  3. android apache HTTP demo 互联网访问
  4. Android(安卓)P SystemUI之StatusBar Wifi图标更新逻辑
  5. Android(安卓)最火快速开发框架AndroidAnnotations使用详解
  6. android之实现各个组件点击事件处理
  7. android 处理鼠标滚轮事件
  8. Android(安卓)最火快速开发框架AndroidAnnotations使用详解
  9. android拖动图片移动效果

随机推荐

  1. Android之SAX解析xml
  2. Android高手进阶教程(三)之----Android(
  3. 开发规范:《阿里巴巴Android开发手册》之
  4. 修改Android开机画面
  5. 如何画好人体动态?动漫人体动态画法教程
  6. RabbitMQ入门 -- 阿里云服务器安装Rabbit
  7. nginx常见502错误提示原因和解决方法
  8. 动漫设计需要先学什么?动漫人物设计步骤
  9. linux三剑客(grep、sed、awk)基本使用
  10. linux三剑客(grep、sed、awk)基本使用