您的位置:首页 > 其它

第八节--控件(一)RadioGroup,RadioButton,CheckBox,Toast

2011-03-16 11:48 561 查看
RadioGroup:

java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.LinearLayout
android.widget.RadioGroup
RadioButton:

java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
android.widget.CompoundButton
android.widget.RadioButton
原意:

A radio button is a two-states button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it.

译文:

单选按钮是两种状态的按钮,可以是选中或不选中状态。当单选按钮未被选中,用户可以按下或点击它来选中它。

CheckBox:

java.lang.Object
android.view.View
android.widget.TextView
android.widget.Button
android.widget.CompoundButton
android.widget.CheckBox
原意:

A checkbox is a specific type of two-states button that can be either checked or unchecked

译文:

一个checkbox是一个指定的两种状态类型的按钮,它要么被选中要么不被选中。

Toast:

java.lang.Object
android.widget.Toast
原意:

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

译文:

一个Toast是一个快速显示一小段信息给用户的视图。toast类可以帮组我们创建和显示这些信息。

代码示例:

java代码:
public class RadioTest extends Activity {
//对控件对象进行声明
private RadioGroup firstGroup = null;
private RadioGroup secondGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
private RadioButton sunzoneButton = null;
private RadioButton pro360Button = null;
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
//通过调用findViewById方法,获取对应Id控件的对象
firstGroup = (RadioGroup)findViewById(R.id.firstGroup);
secondGroup = (RadioGroup)findViewById(R.id.secondGroup);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
maleButton = (RadioButton)findViewById(R.id.maleButton);
sunzoneButton=(RadioButton)findViewById(R.id.sunzoneButton);
pro360Button=(RadioButton)findViewById(R.id.edu360Button);

swimBox = (CheckBox)findViewById(R.id.swim);
runBox = (CheckBox)findViewById(R.id.run);
readBox = (CheckBox)findViewById(R.id.read);
//为RadioGroup设置监听器
firstGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(femaleButton.getId() == checkedId){
Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
}
else if(maleButton.getId() == checkedId)
{
System.out.println("male");
Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show();
}
}
});
secondGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(sunzoneButton.getId()==checkedId){
Toast.makeText(RadioTest.this, "森纵教育", Toast.LENGTH_LONG).show();
}else if(pro360Button.getId()==checkedId){
Toast.makeText(RadioTest.this, "职业教育360", Toast.LENGTH_SHORT).show();
}

}
});

//为多选按钮添加监听器
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(RadioTest.this, "你选择了swim", Toast.LENGTH_SHORT).show();
System.out.println("swim is checked");
}
else
{
System.out.println("swim is unchecked");
Toast.makeText(RadioTest.this, "你取消了swim", Toast.LENGTH_SHORT).show();
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(RadioTest.this, "你选择了run", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(RadioTest.this, "你取消了run", Toast.LENGTH_SHORT).show();
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
System.out.println("read is checked");
Toast.makeText(RadioTest.this, "你选择了read", Toast.LENGTH_SHORT).show();
}
else
{
System.out.println("read is unchecked");
Toast.makeText(RadioTest.this, "你取消了read", Toast.LENGTH_SHORT).show();
}
}
});
}

}


布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="控件Test"
/>
<RadioGroup
android:id="@+id/firstGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"

/>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup>
<TextView android:layout_width="100px"
android:layout_height="2dp"
android:background="#00ddcc"
/>
<RadioGroup
android:id="@+id/secondGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/sunzoneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="森纵教育"
/>
<RadioButton
android:id="@+id/edu360Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="职业教育"
/>
</RadioGroup>
<TextView android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#00ddcc"
/>
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#00ddcc"
/>
</LinearLayout>


演示效果:





原意:

This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.

Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.
译文:

这个类是用来创建具有相互排斥的一个可选择radio按钮。在同一组内,选择其中一个按钮就预示着要取消另一个按钮的选择。
最初,单选按钮都是未选中。如果一个radio组被移除和取消,是不可能去选择一个radio按钮的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: