返回顶部

一缕半夏微光

温柔半两,从容一生

导航

复选框的实现

布局文件activity_main.xml

 1         <TextView
 2             android:id="@+id/instruction"
 3             android:layout_width="wrap_content"
 4             android:layout_height="wrap_content"
 5             android:layout_below="@id/temperature"
 6             android:text="特殊情况(可多选,如有其他需报告的情况请说明)"
 7             android:textSize="16sp"
 8             android:layout_marginTop="10dp"/>
 9 
10         <CheckBox
11             android:id="@+id/instruction1"
12             android:layout_width="match_parent"
13             android:layout_height="wrap_content"
14             android:layout_below="@id/instruction"
15             android:text="无"
16             android:background="#FDDEDE"
17             android:textSize="16sp"
18             android:layout_marginTop="10dp"/>
19 
20         <CheckBox
21             android:id="@+id/instruction2"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_below="@id/instruction1"
25             android:text="2020年12月17日至今去过或者现在居住在中高风险地区"
26             android:background="#FDDEDE"
27             android:textSize="16sp"
28             android:layout_marginTop="10dp"/>
29 
30         <CheckBox
31             android:id="@+id/instruction3"
32             android:layout_width="match_parent"
33             android:layout_height="wrap_content"
34             android:layout_below="@id/instruction2"
35             android:text="本人或家人正在集中隔离"
36             android:background="#FDDEDE"
37             android:textSize="16sp"
38             android:layout_marginTop="10dp"/>
39 
40         <CheckBox
41             android:id="@+id/instruction4"
42             android:layout_width="match_parent"
43             android:layout_height="wrap_content"
44             android:layout_below="@id/instruction3"
45             android:text="今日居住地异动"
46             android:background="#FDDEDE"
47             android:textSize="16sp"
48             android:layout_marginTop="10dp"/>
49 
50         <CheckBox
51             android:id="@+id/instruction5"
52             android:layout_width="match_parent"
53             android:layout_height="wrap_content"
54             android:layout_below="@id/instruction4"
55             android:text="其他情况"
56             android:background="#FDDEDE"
57             android:textSize="16sp"
58             android:layout_marginTop="10dp"/>    

MainActivity.java

 1 public class MainActivity extends AppCompatActivity{
 2     //CheckBox复选框
 3     private CheckBox check1;
 4     private CheckBox check2;
 5     private CheckBox check3;
 6     private CheckBox check4;
 7     private CheckBox check5;
 8     private String str="特殊情况:";
 9     //提交按钮
10     private Button mBtnSubmit;
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_main);
16 
17         //CheckBox复选框
18         check1=findViewById(R.id.instruction1);
19         check2=findViewById(R.id.instruction2);
20         check3=findViewById(R.id.instruction3);
21         check4=findViewById(R.id.instruction4);
22         check5=findViewById(R.id.instruction5);
23 
24         //提交按钮
25         mBtnSubmit = findViewById(R.id.btn_submit);
26         mBtnSubmit.setOnClickListener(new View.OnClickListener() {
27             @Override
28             public void onClick(View v) {
29 
30                 //复选框
31                 if(check1.isChecked()){
32                     str+=check1.getText()+",";
33                 }
34                 if(check2.isChecked()){
35                     str+=check2.getText()+",";
36                 }
37                 if(check3.isChecked()){
38                     str+=check3.getText()+",";
39                 }
40                 if(check4.isChecked()){
41                     str+=check4.getText()+",";
42                 }
43                 if(check5.isChecked()){
44                     str+=check5.getText()+",";
45                 }
46                 Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
47         }
48     }
49 
50     //CheckBox复选框
51     private CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener() {
52         @Override
53         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
54             //选中多选框
55             CheckBox checkBox=(CheckBox)buttonView;
56             //取出当前勾选值
57             String str=checkBox.getText().toString();
58             //判断是否勾选状态
59             if(isChecked){
60                 str="你选了"+str;
61             }else{
62                 str="你没选"+str;
63             }
64             Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
65         }
66     };
67 }
68         

posted on 2021-03-06 12:28  一缕半夏微光  阅读(102)  评论(0编辑  收藏  举报