checkbox控件使用

本文详细介绍了Android中Checkbox控件的使用方法,包括XML布局配置和Java代码逻辑处理。通过实例展示了如何设置和监听Checkbox的状态变化,以及如何获取用户的选择结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

checkbox(复选框),用于多个选择选中或者多个选择不选等情况下使用,例如你喜欢吃的水果可有多种,或者都没有,也可以只有一种。checkbox控件其实就是一个button并附带一个可以检测目前是否被选中的状态变量isChecked,如果被选中isChecked为true,否则为flase.

例如如下的效果图:
在这里插入图片描述

xml布局
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/id_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你喜欢的水果的是?"/>

    <CheckBox
        android:id="@+id/id_apple"
        android:layout_below="@id/id_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="苹果"/>


    <CheckBox
        android:id="@+id/id_banana"
        android:layout_below="@id/id_apple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="香蕉"/>

    <CheckBox
        android:id="@+id/id_orange"
        android:layout_below="@id/id_banana"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="橙子"/>




</RelativeLayout>

java代码逻辑:

package com.gentle.checkbox;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView txShow;
    private String selectTotal="";
    private String appleSelected ="";
    private String bananaSelected ="";
    private String orangeSelected ="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox apple = findViewById(R.id.id_apple);
        apple.setOnCheckedChangeListener(checkBoxListenr);

        CheckBox banana = findViewById(R.id.id_banana);
        banana.setOnCheckedChangeListener(checkBoxListenr);

        CheckBox orange = findViewById(R.id.id_orange);
        orange.setOnCheckedChangeListener(checkBoxListenr);


    }


    private CompoundButton.OnCheckedChangeListener checkBoxListenr = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(buttonView.getId() == R.id.id_apple){
                appleSelected = isChecked?"苹果":"";
            }else if(buttonView.getId() == R.id.id_banana){
                bananaSelected = isChecked?"香蕉":"";
            }else if(buttonView.getId() == R.id.id_orange){
                orangeSelected = isChecked?"橙子":"";
            }else
                Log.d("--->test","check id is invalid");

            selectTotal = appleSelected + bananaSelected + orangeSelected;

            Toast.makeText(MainActivity.this,selectTotal,Toast.LENGTH_SHORT).show();
        }
    };

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值