Android常见控件之RadioGroup,RadioButton,CheckBox和Toast

一、RadioGroupRadioButton
RadioButton单选按钮是一种双状态的按钮,可以选择或不选中。在单选按钮没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够通过界面取消选中,但是可以通过代码来取消选中状态。
RadioGroup用于创建一组选中状态相互排斥的单选按钮组。一个组中,只有一个按钮可以被选中,选中了一个RadioButton会自动取消其它按钮的选中状态。初始状态下,所有的单选按钮都未勾选,虽然不能取消一个特定的单选按钮的勾选状态,但可以通过单选按钮组去消除它的勾选状态。RadioGroup和RadioButton通常是放在一起使用的。
Interface RadioGroup.OnCheckedChangeListener是当单选按钮组中的单选按钮的勾选状态发生改变时,所要调用的回调函数的接口类。当我们希望当RadioGroup中的RadioButton按钮的选中状态改变时执行相应的操作,为RadioGroup添加监听器。在这个类中我们需要复写public void _disibledevent=>二、CheckBox
CheckBox继承自CompoundButton,是有双状态按钮的复选框,可以选中也可以不选中。CheckBox没有组的概念,可以独立使用。可以为CheckBox添加监听器,在监听器的代码中需要复写public void _disibledevent=>三、Toast
Toast是一种提供给用户简洁信息的视图。Toast类帮助你创建和显示该信息。该视图已浮于应用程序之上的形式呈现给用户。因为它并不获得焦点,即使用户正在输入什么也不会受到影响。它的目标是尽可能已不显眼的方式,使用户看到你提供的信息。使用该控件最简单的方法是调用该类的静态方法public static Toast makeText (Context context, CharSequence text, int duration) 返回一个Toast对象,然后再调用Show()方法就可以了。例如Toast. makeText (Context context, CharSequence text, int duration).show()。这个方法的第一个参数是上下文对象,通常是你的应用程序或者Activity对象,第二个参数就是要显示的文本内容,可以格式化文本,第三个参数是持续多长时间来显示消息,有两个常量:LENGTH_SHORT 或者LENGTH_LONG。
示例:
新建一个Android应用程序。 编写main.xml文件,添加一个RadioGroup标签(还可以设置此标签内的布局方向是水平布局还是垂直布局)。 在RadioGroup标签内添加两个RadioButton。然后再添加3个CheckBox复选框。当我们点击单选按钮或者选中复选框的时候会提示相应的信息。
Android_Select.java
1: package com.idea.org;
2:
3: import android.app.Activity;
4: import android.os.Bundle;
5: import android.widget.CheckBox;
6: import android.widget.CompoundButton;
7: import android.widget.RadioButton;
8: import android.widget.RadioGroup;
9: import android.widget.Toast;
10:
11: public class Android_Select extends Activity {
12: private RadioGroup radioGroup=null;
13: private RadioButton radioButtonFemale=null;
14: private RadioButton radioButtonMale=null;
15: private CheckBox checkBoxSwim=null;
16: private CheckBox checkBoxRun=null;
17: private CheckBox checkBoxRead=null;
18: /** Called when the activity is first created. */
19: @Override
20: public void _disibledevent=> 21: super.onCreate(savedInstanceState);
22: setContentView(R.layout.main);
23: radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
24: radioButtonFemale=(RadioButton)findViewById(R.id.radioButtonFemale);
25: radioButtonMale=(RadioButton)findViewById(R.id.radioButtonMale);
26: checkBoxSwim=(CheckBox)findViewById(R.id.checkBoxSwim);
27: checkBoxRun=(CheckBox)findViewById(R.id.checkBoxRun);
28: checkBoxRead=(CheckBox)findViewById(R.id.checkBoxRead);
29: //为radioGroup添加监听器
30: radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
31:
32: @Override
33: public void _disibledevent=>int checkedId) {
34: // TODO Auto-generated method stub
35: if(radioButtonFemale.getId()==checkedId)
36: {
37: //调用Toast的静态方法显示提示消息
38: Toast.makeText(Android_Select.this,"女性",Toast.LENGTH_SHORT).show();
39: }
40: else if(radioButtonMale.getId()==checkedId)
41: {
42: Toast.makeText(Android_Select.this,"男性",Toast.LENGTH_SHORT).show();
43: }
44: }
45: });
46: //为复选框添加监听器
47: checkBoxSwim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
48:
49: @Override
50: public void _disibledevent=>boolean isChecked) {
51: // TODO Auto-generated method stub
52: if(isChecked==true)
53: Toast.makeText(Android_Select.this,"游泳",Toast.LENGTH_SHORT).show();
54: }
55: });
56: checkBoxRun.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
57:
58: @Override
59: public void _disibledevent=>boolean isChecked) {
60: // TODO Auto-generated method stub
61: if(isChecked==true)
62: Toast.makeText(Android_Select.this,"跑步",Toast.LENGTH_SHORT).show();
63: }
64: });
65: checkBoxRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
66:
67: @Override
68: public void _disibledevent=>boolean isChecked) {
69: // TODO Auto-generated method stub
70: if(isChecked==true)
71: Toast.makeText(Android_Select.this,"阅读",Toast.LENGTH_SHORT).show();
72: }
73: });
74: }
75: }
main.xml
1:
2: 3: android:orientation="vertical"
4: android:layout_width="fill_parent"
5: android:layout_height="fill_parent"
6: >
7: 8: android:layout_width="fill_parent"
9: android:layout_height="wrap_content"
10: android:text="@string/hello"
11: />
12:
13: 14: android:orientation="vertical"
15: android:id="@+id/radioGroup"
16: android:layout_width="wrap_content"
17: android:layout_height="wrap_content"
18: >
19: 20: android:id="@+id/textView1"
21: android:layout_width="wrap_content"
22: android:layout_height="wrap_content"
23: android:text="@string/selectSex"
24: />
25: 26: android:id="@+id/radioButtonFemale"
27: android:layout_width="wrap_content"
28: android:layout_height="wrap_content"
29: android:text="@string/female"
30: />
31: 32: android:id="@+id/radioButtonMale"
33: android:layout_width="wrap_content"
34: android:layout_height="wrap_content"
35: android:text="@string/male"
36: />
37:

38: 39: android:id="@+id/textView2"
40: android:layout_width="fill_parent"
41: android:layout_height="wrap_content"
42: android:text="@string/selectInterests"
43: />
44:
45: 46: android:id="@+id/checkBoxSwim"
47: android:layout_width="wrap_content"
48: android:layout_height="wrap_content"
49: android:text="@string/swim"
50: />
51: 52: android:id="@+id/checkBoxRun"
53: android:layout_width="wrap_content"
54: android:layout_height="wrap_content"
55: android:text="@string/run"
56: />
57: 58: android:id="@+id/checkBoxRead"
59: android:layout_width="wrap_content"
60: android:layout_height="wrap_content"
61: android:text="@string/read"
62: />
63:

64:
strings.xml
1:
2:
3: Hello World, Android_Select!
4: Android_Select
5: 请选择性别
6: 请选择爱好
7: 女性
8: 男性
9: 游泳
10: 跑步
11: 阅读
12:

13:
界面:
未命名Android常见控件之RadioGroup,RadioButton,CheckBox和Toast
运行效果:选中阅读复选框,Toast弹出提示信息。
QQ截图未命名未命名Android常见控件之RadioGroup,RadioButton,CheckBox和Toast
Tags: 

延伸阅读

最新评论

发表评论