GitOPEN's Home.

《Monkey Android》第13课CheckBox和RadioButton

Word count: 885 / Reading time: 5 min
2016/04/11 Share

通过本节课可以学习到的内容:

  • CheckBox的用法
  • RadioButton的用法

实例代码:

运行效果参见本课程示例App:安卓猴Demos

github地址:https://github.com/opengit/MonkeyAndroid


效果图

CheckBox和RadioButton

CheckBox和RadioButton的用法

CheckBox,复选框,它允许用户选择一个或者多个。
RadioButton,单选按钮,只能选取一个选项。

xml文件源码:

一些用到的xml属性的含义已经在注释中给出。

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/activity_horizontal_margin"
android:gravity="center_horizontal"
android:orientation="vertical"
>


<CheckBox
android:id="@+id/checkbox_xiaoshuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小说"
/>

<CheckBox
android:id="@+id/checkbox_youxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏"
/>

<CheckBox
android:id="@+id/checkbox_dianying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电影"
/>

<!--
这里是一个1dp高度view,用做一个分割线
-->

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
/>


<!--
android:checkedButton="@+id/radiobutton_apple"
表示默认选中的RadioButton;

android:orientation="horizontal"
表示RadioGroup中的RadioButton的是竖直排列还是水平排列
-->


<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/radiobutton_apple"
android:orientation="horizontal"
>


<RadioButton
android:id="@+id/radiobutton_banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="香蕉"
/>

<RadioButton
android:id="@+id/radiobutton_apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="苹果"
/>

<RadioButton
android:id="@+id/radiobutton_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="橘子"
/>

</RadioGroup>

</LinearLayout>

Activity中的源码:

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
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.sunjiajia.monkeyandroid;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
* Created by monkey on 1/1/16.
*
* 实现了CheckButton的接口CompoundButton.OnCheckedChangeListener
*
* 实现了RadioGroup的接口RadioGroup.OnCheckedChangeListener
*/
public class CheckBoxRadioButtonActivity extends BaseActivity
implements CompoundButton.OnCheckedChangeListener, RadioGroup.OnCheckedChangeListener {
@Override public int giveViewResId() {
return R.layout.activity_checkbox_radiobutton;
}

private CheckBox mCbXs, mCbYx, mCbDy;
private RadioGroup mRg;
private RadioButton mRbBanana, mRbApple, mRbOrange;

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

initViews();

configViews();
}

/**
* 配置各种控件,在这里只是设置了绑定了监听事件
*/
private void configViews() {
mCbXs.setOnCheckedChangeListener(this);
mCbYx.setOnCheckedChangeListener(this);
mCbDy.setOnCheckedChangeListener(this);

mRg.setOnCheckedChangeListener(this);
}

/**
* init各种控件
*/
private void initViews() {
mCbXs = (CheckBox) findViewById(R.id.checkbox_xiaoshuo);
mCbYx = (CheckBox) findViewById(R.id.checkbox_youxi);
mCbDy = (CheckBox) findViewById(R.id.checkbox_dianying);

mRg = (RadioGroup) findViewById(R.id.radio_group);

mRbBanana = (RadioButton) findViewById(R.id.radiobutton_banana);
mRbApple = (RadioButton) findViewById(R.id.radiobutton_apple);
mRbOrange = (RadioButton) findViewById(R.id.radiobutton_orange);
}

/**
* CheckBox的监听事件
*/
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

switch (buttonView.getId()) {
case R.id.checkbox_xiaoshuo:
checkedShowToast(buttonView, isChecked);
break;
case R.id.checkbox_youxi:
checkedShowToast(buttonView, isChecked);
break;
case R.id.checkbox_dianying:
checkedShowToast(buttonView, isChecked);
break;
}
}

/**
* RadioGroup的监听事件
*/
@Override public void onCheckedChanged(RadioGroup group, int checkedId) {

switch (checkedId) {
case R.id.radiobutton_banana:
radioGroupChecked(mRbBanana);
break;
case R.id.radiobutton_apple:
radioGroupChecked(mRbApple);
break;
case R.id.radiobutton_orange:
radioGroupChecked(mRbOrange);
break;
}
}

private void radioGroupChecked(RadioButton button) {
Toast.makeText(CheckBoxRadioButtonActivity.this, "您选中了#" + button.getText().toString() + "#",
Toast.LENGTH_SHORT).show();
}

private void checkedShowToast(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(CheckBoxRadioButtonActivity.this,
"您选中了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CheckBoxRadioButtonActivity.this,
"您取消了#" + buttonView.getText().toString() + "#", Toast.LENGTH_SHORT).show();
}
}
}

下课

这一节课,我们主要学习了CheckBox和RadioButton的用法,CheckBox主要用于多选或者全选的场景,RadioButton主要用于有限选项而且仅允许单选的场景。


欣慰帮到你 一杯热咖啡
【奋斗的Coder!】企鹅群
【奋斗的Coder】公众号
CATALOG
  1. 1. 效果图
  2. 2. CheckBox和RadioButton的用法
  3. 3. xml文件源码:
  4. 4. Activity中的源码:
  5. 5. 下课