Android之CheckBox实例实现

Android之CheckBox实例实现,第1张

概述XML<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas.android.com/apkes-auto&qu 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文本框。@H_404_11@ 1.需要实现的是点击全选,上面的checkBox全部被选中;@H_404_11@ 2.点击全不选,上面的checkBox全部不被选中,@H_404_11@ 3.点击获取内容,右边的TextVIEw显示选中的内容

分析

如果想要实现上述内容,我们就需要通过java监听事件来实现,@H_404_11@ 我们为三个按钮绑定一个监听事件setonClickListener;@H_404_11@ 通过switch(R.getID())来进行判断,如果点击了全选按钮,就让checkBox为True@H_404_11@ 如果全不选,就为false。@H_404_11@ 对于获取内容按钮,我们首先需要一个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;            }        }    }}

@H_404_11@

总结

以上是内存溢出为你收集整理的Android之CheckBox实例实现全部内容,希望文章能够帮你解决Android之CheckBox实例实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1038397.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存