zoukankan      html  css  js  c++  java
  • Android---------------CheckBox小练习

    1 Java代码

    package com.example.myapplication;
     
    import androidx.appcompat.app.AppCompatActivity;
     
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.Toast;
     
    public class CheckBoxActivity extends AppCompatActivity {
        private CheckBox che1;
        private CheckBox che2;
        private CheckBox che3;
        private CheckBox che4;
        private CheckBox che5;
        private CheckBox che6;
        private CheckBox che7;
        private Button Butt1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setContentView(R.layout.checkbox);
            super.onCreate(savedInstanceState);
            che1=findViewById(R.id.che1);
            che2=findViewById(R.id.che2);
            che3=findViewById(R.id.che3);
            che4=findViewById(R.id.che4);
            che5=findViewById(R.id.che5);
            che6=findViewById(R.id.che6);
            che7=findViewById(R.id.che7);
            Butt1=findViewById(R.id.Butt1);
     
            che1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
            che2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
            che3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
            che4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
            che5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
            che6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
            che7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
                }
            });
           Butt1.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   Toast.makeText(CheckBoxActivity.this,"用户喜爱创建完毕",Toast.LENGTH_SHORT).show();;
               }
           });
     
        }
    }
    

      2 XML代码

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp">
     
        <TextView
            android:id="@+id/check1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="请选择你的兴趣爱好:"
            android:textColor="#000"
            android:textSize="25dp"/>
     
        <CheckBox
            android:layout_marginTop="15dp"
            android:id="@+id/che1"
        android:layout_below="@id/check1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大美女"/>
        <CheckBox
            android:layout_marginTop="15dp"
            android:id="@+id/che2"
            android:layout_below="@id/che1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大美女荣荣"/>
        <CheckBox
            android:layout_marginTop="15dp"
            android:id="@+id/che3"
            android:layout_below="@id/che2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大美女花花"/>
        <CheckBox
            android:layout_marginTop="15dp"
            android:id="@+id/che4"
            android:layout_below="@id/che3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大美女喵喵"/>
        <CheckBox
            android:layout_marginTop="15dp"
            android:id="@+id/che5"
            android:layout_below="@id/che4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大美女阿吉"/>
        <TextView
            android:layout_marginTop="20dp"
            android:id="@+id/check2"
            android:layout_below="@id/che5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="请选择你的人生目标:"
            android:textColor="#00ffff"
            android:textSize="25dp"/>
        <LinearLayout
            android:id="@+id/lay1"
            android:layout_below="@id/check2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <CheckBox
                android:layout_marginTop="15dp"
                android:id="@+id/che6"
                android:layout_below="@id/che4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="55dp"
                android:button="@drawable/check_check"
                android:text="吃饭"/>
            <CheckBox
                android:layout_marginTop="15dp"
                android:id="@+id/che7"
                android:layout_below="@id/che4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="55dp"
                android:layout_marginLeft="80dp"
                android:button="@drawable/check_check"
                android:text="睡觉"/>
        </LinearLayout>
        <Button
            android:id="@+id/Butt1"
            android:layout_below="@id/lay1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="选择完毕!"
            android:layout_marginTop="50dp"/>
    </RelativeLayout>
    

      

  • 相关阅读:
    4-STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-安装Web服务器软件Nginx(.Windows系统)
    单片机程序底层BUG问题,及解决方案
    3-STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-编写android连接MQTT服务器程序
    2-STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-移植单片机MQTT包,编写stm32+CH395Q连接MQTT服务器程序
    1-STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-购买云主机,安装MQTT软件(linux系统),测试C# 和 网页web连接MQTT服务器
    1-STM32+CH395Q(以太网)基本控制篇(自建物联网平台)-购买云主机,安装MQTT软件(.Windows系统),测试C# 和 网页web连接MQTT服务器
    springmvc缓存--验证通过
    使用nginx缓存服务器上的静态文件
    使用nginx缓存服务器上的静态文件
    Jenkins+maven+Tomcat+SVN一键自动打包部署应用到服务器
  • 原文地址:https://www.cnblogs.com/xuwei123456/p/13972529.html
Copyright © 2011-2022 走看看