Android之CheckBox实例实现

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".checkbox">


    <TextView
        android:id="@+id/showresult"
        android:layout_width="150dp"
        android:layout_height="30dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="140dp"
        android:layout_marginRight="140dp"
        android:text="TextView2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_notall" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:text="电竞"
        app:layout_constraintEnd_toStartOf="@+id/checkBox2"
        app:layout_constraintHorizontal_bias="0.555"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="64dp"
        android:layout_marginRight="64dp"
        android:text="旅游"
        app:layout_constraintEnd_toStartOf="@+id/checkBox3"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:text="看书"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:text="爱好:"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="68dp"
        android:text="全选"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/btn_notall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="全不选"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        app:layout_constraintStart_toEndOf="@+id/btn_all"
        app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

    <Button
        android:id="@+id/btn_get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:text="获取内容"
        app:layout_constraintEnd_toStartOf="@+id/showresult"
        app:layout_constraintHorizontal_bias="0.163"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_all" />

</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述

需要实现的需求

XML我是通过约束布局通过拖拽的方式来实现的,三个checkbox选项,
以及全选,全部选,获取内容三个Button按钮,和一个获取内容结果的TextView文本框。
1.需要实现的是点击全选,上面的checkBox全部被选中;
2.点击全不选,上面的checkBox全部不被选中,
3.点击获取内容,右边的TextView显示选中的内容

分析

如果想要实现上述内容,我们就需要通过java监听事件来实现,
我们为三个按钮绑定一个监听事件setOnClickListener;
通过switch(R.getId())来进行判断,如果点击了全选按钮,就让checkbox为True
如果全不选,就为false。
对于获取内容按钮,我们首先需要一个List来进行添加,如果我们点击电竞,电竞被点击,就向List中添加其checkbox中的文本。

话不多说,上代码

package com.example.appcheckbox;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class checkbox extends AppCompatActivity {
    private CheckBox box1,box2,box3;
    private Button btn_all,btn_notall,btn_get;
    private TextView showresult;
    private checkBoxListenner checkListenner;
    private  ButtonListenner btlistenner;
    private List<String> lists;//集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);
        init();
        initData();
        setListenner();
    }
	//对List进行初始化
    private void initData() {
        //初始化数据
        lists = new ArrayList<>();
    }


   
	//初始化获取将xml中的按钮转换值java
    private void init() {
        box1=findViewById(R.id.checkBox);
        box2=findViewById(R.id.checkBox2);
        box3=findViewById(R.id.checkBox3);
        btn_all=findViewById(R.id.btn_all);
        btn_notall=findViewById(R.id.btn_notall);
        btn_get=findViewById(R.id.btn_get);
        showresult=findViewById(R.id.showresult);
    }
//设置并为按钮绑定监听事件
    private void setListenner() {
        checkListenner=new checkBoxListenner();
        box1.setOnCheckedChangeListener(checkListenner);
        box2.setOnCheckedChangeListener(checkListenner);
        box3.setOnCheckedChangeListener(checkListenner);
        btlistenner=new ButtonListenner();
        btn_all.setOnClickListener(btlistenner);
        btn_notall.setOnClickListener(btlistenner);
        btn_get.setOnClickListener(btlistenner);

    }
	//实现checkBox点击的响应Toast
    public class checkBoxListenner implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox checkBox=(CheckBox)buttonView;
            switch (checkBox.getId()){
                case R.id.checkBox:
                    if(isChecked){
                        Toast.makeText(checkbox.this,"少玩游戏多写代码",1000).show();
                        box1.setTextColor(Color.RED);
                    }else {
                        box1.setTextColor(Color.BLACK);
                    }
                    Toast.makeText(checkbox.this,"电竞"+isChecked,1000).show();
                    break;
                case R.id.checkBox2:
                    Toast.makeText(checkbox.this,"旅游"+isChecked,1000).show();
                    break;
                case R.id.checkBox3:
                    Toast.makeText(checkbox.this,"看书"+isChecked,1000).show();
                    break;
            }
        }
    }
    //为按钮实现监听   
    class ButtonListenner implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_all:
                    box1.setChecked(true);
                    box2.setChecked(true);
                    box3.setChecked(true);
                    break;
                case R.id.btn_notall:
                    box1.setChecked(false);
                    box2.setChecked(false);
                    box3.setChecked(false);
                    break;
                case R.id.btn_get:
                    if(box1.isChecked()){
                        lists.add(box1.getText().toString());
                    }
                    if(box2.isChecked()){
                        lists.add(box2.getText().toString());
                    }
                    if(box3.isChecked()){
                        lists.add(box2.getText().toString());
                    }
                    showresult.setText(lists.toString());
                    lists.clear();
                    break;

            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述

发布了79 篇原创文章 · 获赞 55 · 访问量 7169

猜你喜欢

转载自blog.csdn.net/qq_45353823/article/details/105034367
今日推荐