范例说明
接下来要介绍的是RadioGroup的组事件。RadioGroup可将各自不同的RadioButton设限于同一个Radio按钮组,同属一个RadioGroup组里的按钮,只能做出单一选择(单选题),虽然前一章曾经介绍过RadioGroup与RadioButton,但当时使用的是Button事件,在此要示范的是,"单击"的同时运行事件处理,而不再需要按钮(Button)的辅助了。
先设计一个TextView Widget,以及一个RadioGroup,并于该RadioGroup内放置两个RadioButton,默认为都不选择,在程序运行阶段,利用onCheckedChanged作为启动事件装置,让User在选择其中一个按钮时,显示被选择的内容,最后将RadioButton的选项文字显示于TextView当中。
运行结果(见图4-6)
图4-6 单击"帅哥"或"美女"按钮的同时,会立即显示事件结果 |
范例程序
- src/com.example.ex04_6/ex04_6.java
利用OnCheckedChangeListener来启动RadioGroup的事件,随后将被勾选的RadioButton(mRadio1.getText())的文字显示于TextView。
package com.example.ex04_6;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class ex04_6 extends Activity {
public TextView mTextView1;
public RadioGroup mRadioGroup1;
public RadioButton mRadioButton1;
public RadioButton mRadioButton2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*取得 TextView、RadioGroup、RadioButton对象*/
mTextView1=(TextView)findViewById(R.id.TextView01);
mRadioGroup1=(RadioGroup)findViewById(R.id.RadioGroup01);
mRadioButton1=(RadioButton)findViewById(R.id.RadioButton01);
mRadioButton2=(RadioButton)findViewById(R.id.RadioButton02);
answerButton=(RadioButton)findViewById(R.id.AnswerButton);
clearButton=(RadioButton)findViewById(R.id.clearButton);
/*RadioGroup用OnCheckedChangeListener来运行*/
mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
}
private RadioGroup.OnCheckedChangeListener mChangeRadio=new
RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==mRadioButton1.getId())
{
/*把mRadioButton1的内容传到mTextView1*/
mTextView1.setText(mRadioButton1.getText());
}
else if(checkedId==mRadioButton2.getId())
{
/*把mRadioButton2的内容传到mTextView1*/
mTextView1.setText(mRadioButton2.getText());
}
}
};
2、res/layout/main.xml
<?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/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请问你是?"
/>
<RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- 在 RadioGroup里添加几个RadioButton-->
<RadioButton
android:text="帅哥"
android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<RadioButton
android:text="美女"
android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
</RadioGroup>
</LinearLayout>
扩展学习
在扩展学习里,请试着在其中加上两个Button,一个为回答,另一个为清除RadioButton的选择状态。程序有随机设置的答案选项,当User单击"回答"按钮时,比较答案是否正确。若正确,则以AlertDialog对话窗口显示答案结果。
private RadioButton.OnClickListener manswerButton=new RadioButton.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new AlertDialog.Builder(ex04_6.this)
.setIcon(R.drawable.icon)
.setTitle("核对")
.setPositiveButton("确定", null)
.setMessage("请核对刚才的选择")
.create()
.show();
}
};
在清除Button.onClickListener的事件处理中,只需将被选择的RadioButton取消掉,回到等待回答的状态。
- mRadioGroup1.clearCheck();