• check box小练习


    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>
  • 相关阅读:
    JAVA类型转换的那些坑儿
    记录一次 ajaxSubmit()提交表单
    遍历Map
    整理JS对数组的内置操作函数(转)
    html页面监听事件(转)
    js控制文本框只能输入中文、英文、数字与指定特殊符号(引用)
    target属性 和 重定向 response.sendRedirect("");
    js各种正则表达式(引用)
    前端---js定义函数的方式
    Web Service 根据wsdl调用代码
  • 原文地址:https://www.cnblogs.com/reddead/p/14062947.html
Copyright © 2020-2023  润新知