以下是java 文件
public class Activity01 extends Activity {
/**
* 创建TextView对象
* 创建RadioGroup对象
* 创建4个RadioButton对象
*/
TextView m_T;
RadioGroup m_RG;
RadioButton m_R1, m_R2, m_R3, m_R4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/**
* 获得TextView对象
* 获得RadioGroup对象
* 获得4个RadioButton对象
*/
m_T = (TextView)findViewById(R.id.TextView01);
m_RG =(RadioGroup)findViewById(R.id.RadioGroup01);
m_R1=(RadioButton)findViewById(R.id.RadioButton01);
m_R2 =(RadioButton)findViewById(R.id.RadioButton02);
m_R3 =(RadioButton)findViewById(R.id.RadioButton03);
m_R4 =(RadioButton)findViewById(R.id.RadioButton04);
/* 设置事件监听 */
m_RG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
if(checkedId == m_R2.getId()){
DisplayToast("恭喜你" + m_R2.getText()+ ",进入iphone4系统中~~~~");
}else{
DisplayToast("系统错误,该系统已损坏!");
}
}
});
}
public void DisplayToast(String str){
Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
//设置toast显示的位置 Gravity 重力
toast.setGravity(Gravity.TOP, 0, 220);
//显示该Toast
toast.show();
}
}
以下是UI的XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id = "@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<!-- 单选组框 --> <!-- 选项按钮RadioButton -->
<RadioGroup
android:id = "@+id/RadioGroup01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/RadioGroup01"
>
<RadioButton
android:id="@+id/RadioButton01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/RadioButton01" />
<RadioButton
android:id = "@+id/RadioButton02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/RadioButton02"
/>
<RadioButton
android:id = "@+id/RadioButton03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/RadioButton03"
/>
<RadioButton
android:id = "@+id/RadioButton04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/RadioButton04"
/>
</RadioGroup>
</LinearLayout>
以下是string .xml里面的
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">选择进入系统</string>
<string name="app_name">系统选择</string>
<string name="RadioGroup01">RadioGroup01</string>
<string name="RadioButton01">android2.3.3</string>
<string name="RadioButton02">iphone4</string>
<string name="RadioButton03">windowPhone8</string>
<string name="RadioButton04">塞班系统</string>
</resources>
运行图片后为:
选择iPhone4 则显示