这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似。
1.建立字符串资源文件strings.xml;
<resources>
<string name="app_name">健身建议</string>
<string name="promptSex">性别:</string>
<string name="promptAge">年龄:</string>
<string name="promptBtnDoSug">健身建议</string>
<string name="sugResult">结果:</string>
<string name="sugYoga">进行瑜伽运动</string>
<string name="sugRun">跑步运动</string>
<string name="sugSwim">游泳运动</string>
<string name="male">男生</string>
<string name="female">女生</string>
<string name="maleAgeRng1">小于28岁</string>
<string name="maleAgeRng2">28~33岁</string>
<string name="maleAgeRng3">大于33岁</string>
<string name="femaleAgeRng1">小于25岁</string>
<string name="femaleAgeRng2">25~30岁</string>
<string name="femaleAgeRng3">大于30岁</string>
</resources>
2.在接口布局文件中使用RadioGroup 和RadioButton标签,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="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"> <TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/promptSex"/>
<RadioGroup android:id="@+id/radGSex"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/radMale">
<RadioButton android:id="@+id/radMale"
android:textSize="20sp"
android:text="@string/male"/>
<RadioButton android:id="@+id/radFemale"
android:textSize="20sp"
android:text="@string/female"/>
</RadioGroup>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/promptSex"/>
<RadioGroup android:id="@+id/radGAge"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/radAgeRng1">
<RadioButton android:id="@+id/radBtnAgeRng1"
android:textSize="20sp"
android:text="@string/maleAgeRng1"/>
<RadioButton android:id="@+id/radBtnAgeRng2"
android:textSize="20sp"
android:text="@string/maleAgeRng2"/>
<RadioButton android:id="@+id/radBtnAgeRng3"
android:textSize="20sp"
android:text="@string/maleAgeRng3"/>
</RadioGroup>
<Button android:id="@+id/btnDoSug"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/promptBtnDoSug"/>
<TextView android:id="@+id/txtResult"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/sugResult"/>
</LinearLayout> 3.程序代码MainActivity
public class MainActivity extends Activity
{
private Button btnDoSug;
private RadioGroup radGSex, radGAge;
private RadioButton radBtnAgeRng1, radBtnAgeRng2, radBtnAgeRng3;
private TextView txtResult; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setupViewComponent();
} private void setupViewComponent()
{
btnDoSug = (Button)findViewById(R.id.btnDoSug);
radGSex = (RadioGroup)findViewById(R.id.radGSex);
radGAge = (RadioGroup)findViewById(R.id.radGAge);
radBtnAgeRng1 = (RadioButton)findViewById(R.id.radBtnAgeRng1);
radBtnAgeRng2 = (RadioButton)findViewById(R.id.radBtnAgeRng2);
radBtnAgeRng3 = (RadioButton)findViewById(R.id.radBtnAgeRng3);
txtResult = (TextView)findViewById(R.id.txtResult); btnDoSug.setOnClickListener(btnDoSugOnClick);
radGSex.setOnCheckedChangeListener(radGSexOnCheChanLis);
} private RadioGroup.OnCheckedChangeListener radGSexOnCheChanLis = new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(checkedId == R.id.radMale)
{
radBtnAgeRng1.setText(getString(R.string.maleAgeRng1));
radBtnAgeRng2.setText(getString(R.string.maleAgeRng2));
radBtnAgeRng3.setText(getString(R.string.maleAgeRng3));
}
else if(checkedId == R.id.radFemale)
{
radBtnAgeRng1.setText(getString(R.string.femaleAgeRng1));
radBtnAgeRng2.setText(getString(R.string.femaleAgeRng2));
radBtnAgeRng3.setText(getString(R.string.femaleAgeRng3));
}
}
}; private Button.OnClickListener btnDoSugOnClick = new Button.OnClickListener()
{
public void onClick(View v)
{
int iCheckedRadBtn = radGSex.getCheckedRadioButtonId();
String strSug = getString(R.string.sugResult);
switch (iCheckedRadBtn )
{
case R.id.radMale:
switch (radGAge.getCheckedRadioButtonId())
{
case R.id.radBtnAgeRng1:
strSug += getString(R.string.sugYoga);
break;
case R.id.radBtnAgeRng2:
strSug += getString(R.string.sugRun);
break;
default:
strSug += getString(R.string.sugSwim);
}
break;
case R.id.radFemale:
switch (radGAge.getCheckedRadioButtonId())
{
case R.id.radBtnAgeRng1:
strSug += getString(R.string.sugYoga);
break;
case R.id.radBtnAgeRng2:
strSug += getString(R.string.sugRun);
break;
default:
strSug += getString(R.string.sugSwim);
}
break;
}
txtResult.setText(strSug);
}
};
}
效果图:
我们发现,当我们选择不同的性别时,下面的年龄范围也不一样,原因是setupViewComponent()方法的最后一行把RadioGroup.OnCheckedChangeListener
对象设置给性别的RadioGroup对象,因此当我们选择不同的性别时,年龄选项就会跟着改变。