有想過友Android的小屏幕上把asp.net 的RadionButtonList搬進來用嗎?答案是肯定的,Android 的Tool工具提供了一個RadioGroup控件,可將各大自不同的RadioButton設限於同一個Radio按鈕組,這樣子就可以做到單選的效果了,那要怎麼樣用呢?先看截圖:
我們要做的事是:當用戶猜題或者選擇一個特定某個選項時,我們將在後臺判斷用戶是否選中,如果選中者相應的做出處理,具體處理看各自項目的需求,代碼片段一:
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mRadioButton1.isChecked())
{
mRadioButton1.setChecked(false);
}
if(mRadioButton2.isChecked())
{
mRadioButton2.setChecked(false);
}
if(mRadioButton3.isChecked())
{
mRadioButton3.setChecked(false);
}
if(mRadioButton4.isChecked())
{
mRadioButton4.setChecked(false);
}
}
});
按鈕事件,重置選中按鈕為未選中狀態。
代碼片段二:
list=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton btn=(RadioButton)findViewById(checkedId);
if(btn.isChecked())
{
switch (checkedId) {
case R.id.RadioButton01:
Show("回答錯誤!");
break;
case R.id.RadioButton02:
Show("回答錯誤!");
break;
case R.id.RadioButton03:
Show("回答正確,獎勵你100分");
break;
case R.id.RadioButton04:
Show("回答錯誤!");
break;
}
}
}
};
mGroup.setOnCheckedChangeListener(list);
}
步驟分析:一,通過checkid判斷用戶選中哪一個按鈕再判斷,如果狀態為ischeck()則做出相應處理
二,通過checkid做出彈出處理效果
完整代碼如下:
package cn.terry;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class TestRadioButton extends Activity {
private RadioGroup mGroup;
private Button mButton;
private RadioButton mRadioButton1;
private RadioButton mRadioButton2;
private RadioButton mRadioButton3;
private RadioButton mRadioButton4;
private OnCheckedChangeListener list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGroup=(RadioGroup)findViewById(R.id.group);
mButton=(Button)findViewById(R.id.Button1);
mRadioButton1=(RadioButton)findViewById(R.id.RadioButton01);
mRadioButton2=(RadioButton)findViewById(R.id.RadioButton02);
mRadioButton3=(RadioButton)findViewById(R.id.RadioButton03);
mRadioButton4=(RadioButton)findViewById(R.id.RadioButton04);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mRadioButton1.isChecked())
{
mRadioButton1.setChecked(false);
}
if(mRadioButton2.isChecked())
{
mRadioButton2.setChecked(false);
}
if(mRadioButton3.isChecked())
{
mRadioButton3.setChecked(false);
}
if(mRadioButton4.isChecked())
{
mRadioButton4.setChecked(false);
}
}
});
list=new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
RadioButton btn=(RadioButton)findViewById(checkedId);
if(btn.isChecked())
{
switch (checkedId) {
case R.id.RadioButton01:
Show("回答錯誤!");
break;
case R.id.RadioButton02:
Show("回答錯誤!");
break;
case R.id.RadioButton03:
Show("回答正確,獎勵你100分");
break;
case R.id.RadioButton04:
Show("回答錯誤!");
break;
}
}
}
};
mGroup.setOnCheckedChangeListener(list);
}
public void Show(String msg)
{
new AlertDialog.Builder(TestRadioButton.this)
.setTitle("提示")
.setMessage(msg)
.setPositiveButton("確定", null).create().show();
}
}
源碼下載:/Files/TerryBlog/TestRadioButton.rar