我是靠谱客的博主 微笑砖头,这篇文章主要介绍Android界面编程之利用单选框和复选框实现对学历和爱好进行选择,现在分享给大家,希望可以做个参考。

Android界面编程之利用单选框和复选框实现对学历和爱好进行选择

首先我们要了解一下单选框和复选框:

单选框(Radio Button):当用户选择某单选按钮时,同一组中的其他单选按钮不能同时选定,通常进行单选的按钮放到一个RadioGroup中

复选框(Check Box):可以选择任意数目,没有必要放到一个组中

定位被选中的按钮
单选框哪个按钮被选中时,需要在组中进行选择,复选框则不需要,所以单选框要对组进行监听,而复选框可以对个体进行监听,也可以创建一个数组用来循环扫描是否选中。

我使用数组来循环扫面,就不可以创建全局的变量了,不过写起来简单些,复制粘贴,出于代码的规范性最好选择不要创建数组,可以创建几个全局变量,然后自定义一个扫描的方法,本文就不一一掩饰了

下面看代码:


布局:采用Tablelayout布局

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学历" android:textSize="20sp"/> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:id="@+id/small" android:text="小学"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/midle" android:text="中学"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/large" android:text="大学"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/secises" android:text="研究生"/> </RadioGroup> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱好:" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跑步" /> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="游泳" /> <CheckBox android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打球" /> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读书" /> </LinearLayout> </TableRow> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableLayout>

MainActivity

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
public class MainActivity extends AppCompatActivity { String stu; String hob; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tablelayout); final TextView info =(TextView)findViewById(R.id.info); RadioGroup radioGroup =(RadioGroup)findViewById(R.id.radioGroup1); final CheckBox checkBox[] = new CheckBox[4]; checkBox[0] = (CheckBox)findViewById(R.id.swim); checkBox[1] = (CheckBox)findViewById(R.id.run); checkBox[2] = (CheckBox)findViewById(R.id.read); checkBox[3] = (CheckBox)findViewById(R.id.play); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { if (i == R.id.small) stu="您的学历是小学"; else if (i ==R.id.midle) stu="您的学历是中学"; else if (i ==R.id.large) stu="您的学历是大学"; else if (i==R.id.secises) stu="您的学历是研究生"; info.setText(print()); } }); checkBox[0].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { hob = null; if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked()) hob="您的爱好是: "; for (int k = 0 ; k < checkBox.length ; k++ ){ if (checkBox[k].isChecked()) hob=hob+checkBox[k].getText().toString()+" "; } info.setText(print()); } }); checkBox[1].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { hob = null; if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked()) hob="您的爱好是: "; for (int k = 0 ; k < checkBox.length ; k++ ){ if (checkBox[k].isChecked()) hob=hob+checkBox[k].getText().toString()+" "; } info.setText(print()); } }); checkBox[2].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { hob = null; if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked()) hob="您的爱好是: "; for (int k = 0 ; k < checkBox.length ; k++ ){ if (checkBox[k].isChecked()) hob=hob+checkBox[k].getText().toString()+" "; } info.setText(print()); } }); checkBox[3].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { hob = null; if (checkBox[0].isChecked()||checkBox[1].isChecked()||checkBox[2].isChecked()||checkBox[3].isChecked()) hob="您的爱好是: "; for (int k = 0 ; k < checkBox.length ; k++ ){ if (checkBox[k].isChecked()) hob=hob+checkBox[k].getText().toString()+" "; } info.setText(print()); } }); } public String print(){ String str = null; if(stu == null && hob == null){ str = "请选择您的学历和爱好" ; } else if (stu != null && hob == null){ str = stu ; } else if (stu == null && hob != null){ str = hob ; } else { str = stu + "n" + hob; } return str; } }

实现效果: 每次更改单选框或复选框都会更新提示
这里写图片描述

最后

以上就是微笑砖头最近收集整理的关于Android界面编程之利用单选框和复选框实现对学历和爱好进行选择的全部内容,更多相关Android界面编程之利用单选框和复选框实现对学历和爱好进行选择内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部