Android 中 CheckBox 的基本使用

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

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

1. 基本属性

Android 中 CheckBox 的基本使用_第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. 自定义样式

Android 中 CheckBox 的基本使用_第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. 监听事件

Android 中 CheckBox 的基本使用_第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 SeekBar自定义使用图片和颜色显示
  2. RecyclerView item imageview 图片宽高自适应
  3. Android UI设计系统-android selector 开始自定义样式
  4. android AppCompatEditText 样式(线条粗细等)修改
  5. android之实现各个组件点击事件处理
  6. Android 微信分享,微信分享图片
  7. android 处理鼠标滚轮事件

随机推荐

  1. 在VS下用C语言连接SQLServer2008
  2. 基于GUI和SQL的剧院票务管理系统
  3. mysql5.6和5.7的权限密码设置
  4. JOIN vs. WHERE:为什么获得相同结果的两
  5. hibernate连接mysql,不能自动建表和添加数
  6. 数据库实践二:高级sql语句
  7. 利用LVS+Keepalived实现MySQL高可用1
  8. 简单统计报表例子(存储过程)
  9. 关于mysql jsp字符编码的问题解决
  10. SQL大量数据查询的优化 及 非用like不可