这一讲视频主要是在Android上使用RadioButton和CheckBox,以及复习一下使用Toast,效果如下,点击查看视频开始学习吧~

RadioButton CheckBox

  • 马上学Android 009 - RadioButton和CheckBox控件的简单使用

布局代码如下:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00BBFF"
android:orientation="vertical"
tools:context="com.chen.activity.MainActivity">

<!-- 本节课添加的xml代码 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"
android:background="#FFFFFFFF">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CCAV调查——你是幸福吗?"
android:textColor="#000000"
android:textSize="20dp" />


<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rgCCAVSuvery">


<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rbSoHappy"
android:text="幸福" />


<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rbNoHappy"
android:text="不幸福" />


<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rbZeng"
android:checked="true"
android:text="我姓曾" />

</RadioGroup>

</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"
android:background="#FFFFFFFF">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CCAV调查——你平时的课外活动是什么?"
android:textColor="#000000"
android:textSize="20dp" />


<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbLOL"
android:text="宿舍LOL" />


<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="陪女友逛街"
android:id="@+id/cbGirlFriend"/>


<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:id="@+id/cbCodingMoney"
android:text="苦逼搬砖" />


</LinearLayout>

</LinearLayout>

对应的java代码:

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
public class MainActivity extends Activity {
private RadioGroup rgCCAVSuvery;
private CheckBox cbLOL, cbGirlFriend, cbCodingMoney;
private Button btnSwitch;
private int money = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定UI布局文件
setContentView(R.layout.activity_main);

//省略无关代码

rgCCAVSuvery = (RadioGroup) findViewById(R.id.rgCCAVSuvery);

cbLOL = (CheckBox) findViewById(R.id.cbLOL);
cbGirlFriend = (CheckBox) findViewById(R.id.cbGirlFriend);
cbCodingMoney = (CheckBox) findViewById(R.id.cbCodingMoney);

rgCCAVSuvery.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId) {
case R.id.rbSoHappy:
Toast.makeText(MainActivity.this, "恭喜你状态很好,继续保持!", Toast.LENGTH_SHORT).show();
break;
case R.id.rbNoHappy:
Toast.makeText(MainActivity.this, "真的吗?推荐你每天看CCAV的新闻X播!", Toast.LENGTH_SHORT).show();
break;
case R.id.rbZeng:
Toast.makeText(MainActivity.this, "你是CCAV的忠实粉丝!", Toast.LENGTH_SHORT).show();
break;
}
}
});

cbLOL.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "骚年,没事多看看书,没事别整天LOL!", Toast.LENGTH_SHORT).show();
}

}
});

cbGirlFriend.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "秀恩爱,你懂的!!!!", Toast.LENGTH_SHORT).show();
}
}
});

cbCodingMoney.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
Toast.makeText(MainActivity.this, "码农,活着就是为了改变世界!", Toast.LENGTH_SHORT).show();
}
}
});
}
}

代码比较直观,配合视频观看应该很容易理解~