android--check-box小练习

java代码

 1 package com.example.myapplication;
 2    
 3 import androidx.appcompat.app.AppCompatActivity;
 4    
 5 import android.annotation.SuppressLint;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.CheckBox;
10 import android.widget.CompoundButton;
11 import android.widget.Toast;
12    
13 public class CheckBoxActivity extends AppCompatActivity {
14     private CheckBox che1;
15     private CheckBox che2;
16     private CheckBox che3;
17     private CheckBox che4;
18     private CheckBox che5;
19     private CheckBox che6;
20     private CheckBox che7;
21     private Button Butt1;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         setContentView(R.layout.checkbox);
25         super.onCreate(savedInstanceState);
26         che1=findViewById(R.id.che1);
27         che2=findViewById(R.id.che2);
28         che3=findViewById(R.id.che3);
29         che4=findViewById(R.id.che4);
30         che5=findViewById(R.id.che5);
31         che6=findViewById(R.id.che6);
32         che7=findViewById(R.id.che7);
33         Butt1=findViewById(R.id.Butt1);
34    
35         che1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
36             @Override
37             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
38                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
39             }
40         });
41         che2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
42             @Override
43             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
44                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
45             }
46         });
47         che3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
48             @Override
49             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
50                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
51             }
52         });
53         che4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
54             @Override
55             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
56                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
57             }
58         });
59         che5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
60             @Override
61             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
62                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
63             }
64         });
65         che6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
66             @Override
67             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
68                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
69             }
70         });
71         che7.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
72             @Override
73             public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
74                 Toast.makeText(CheckBoxActivity.this,b?"选中":"未选中",Toast.LENGTH_SHORT).show();
75             }
76         });
77        Butt1.setOnClickListener(new View.OnClickListener() {
78            @Override
79            public void onClick(View view) {
80                Toast.makeText(CheckBoxActivity.this,"用户喜爱创建完毕",Toast.LENGTH_SHORT).show();;
81            }
82        });
83    
84     }
85 }

xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:padding="10dp">
 6   
 7     <TextView
 8         android:id="@+id/check1"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="请选择你的兴趣爱好:"
12         android:textColor="#000"
13         android:textSize="25dp"/>
14   
15     <CheckBox
16         android:layout_marginTop="15dp"
17         android:id="@+id/che1"
18     android:layout_below="@id/check1"
19     android:layout_width="wrap_content"
20     android:layout_height="wrap_content"
21     android:text="大美女"/>
22     <CheckBox
23         android:layout_marginTop="15dp"
24         android:id="@+id/che2"
25         android:layout_below="@id/che1"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:text="大美女荣荣"/>
29     <CheckBox
30         android:layout_marginTop="15dp"
31         android:id="@+id/che3"
32         android:layout_below="@id/che2"
33         android:layout_width="wrap_content"
34         android:layout_height="wrap_content"
35         android:text="大美女花花"/>
36     <CheckBox
37         android:layout_marginTop="15dp"
38         android:id="@+id/che4"
39         android:layout_below="@id/che3"
40         android:layout_width="wrap_content"
41         android:layout_height="wrap_content"
42         android:text="大美女喵喵"/>
43     <CheckBox
44         android:layout_marginTop="15dp"
45         android:id="@+id/che5"
46         android:layout_below="@id/che4"
47         android:layout_width="wrap_content"
48         android:layout_height="wrap_content"
49         android:text="大美女阿吉"/>
50     <TextView
51         android:layout_marginTop="20dp"
52         android:id="@+id/check2"
53         android:layout_below="@id/che5"
54         android:layout_width="match_parent"
55         android:layout_height="wrap_content"
56         android:text="请选择你的人生目标:"
57         android:textColor="#00ffff"
58         android:textSize="25dp"/>
59     <LinearLayout
60         android:id="@+id/lay1"
61         android:layout_below="@id/check2"
62         android:layout_width="match_parent"
63         android:layout_height="wrap_content"
64         android:orientation="horizontal">
65         <CheckBox
66             android:layout_marginTop="15dp"
67             android:id="@+id/che6"
68             android:layout_below="@id/che4"
69             android:layout_width="wrap_content"
70             android:layout_height="wrap_content"
71             android:textSize="55dp"
72             android:button="@drawable/check_check"
73             android:text="吃饭"/>
74         <CheckBox
75             android:layout_marginTop="15dp"
76             android:id="@+id/che7"
77             android:layout_below="@id/che4"
78             android:layout_width="wrap_content"
79             android:layout_height="wrap_content"
80             android:textSize="55dp"
81             android:layout_marginLeft="80dp"
82             android:button="@drawable/check_check"
83             android:text="睡觉"/>
84     </LinearLayout>
85     <Button
86         android:id="@+id/Butt1"
87         android:layout_below="@id/lay1"
88         android:layout_width="match_parent"
89         android:layout_height="wrap_content"
90         android:text="选择完毕!"
91         android:layout_marginTop="50dp"/>
92 </RelativeLayout>

 

posted @ 2020-12-20 17:05  刘显栋  阅读(110)  评论(0编辑  收藏  举报