• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

Android

Android之RadioButton与RadioGroup基本应用

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0720/580.html

 
RadioButton即单选框,是一种底子的UI控件。RadioGroup为我们供应了RadioButton单选按钮的容器,RadioButton平时放于RadioGroup容器中举行应用。RadioButton的选中状况,在xml文件中可以应用android:checked=""来举行配置,选中就配置为true,没选中就配置为false。
 
 
这里先贴上XML的代码:
 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/ll_sex"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="性别:"
android:textSize="15sp" />
<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/nan"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:gravity="center"
android:text="男" />
<RadioButton
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="false"
android:text="女" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="@+id/ll_sex"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="醉心:"
android:textSize="15sp" />
<RadioGroup
android:id="@+id/hobby"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:gravity="center"
android:text="篮球" />
<RadioButton
android:id="@+id/table_tenis"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="false"
android:text="乒乓球" />
<RadioButton
android:id="@+id/badminton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="false"
android:text="羽毛球" />
</RadioGroup>
</LinearLayout>
</RelativeLayout>
 
这边我先容两种获得RadioButton的监听事务处分方法:
 
方法一:通过获得点击的id来实例化并获得选中状况的RadioButton控件
 
 
// 实例化控件
sex = (RadioGroup) findViewById(R.id.sex);
// 方法一监听事务,通过获得点击的id来实例化并获得选中状况的RadioButton控件
sex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 获得选中的RadioButton的id
int id = group.getCheckedRadioButtonId();
// 通过id实例化选中的这个RadioButton
RadioButton choise = (RadioButton) findViewById(id);
// 获得这个RadioButton的text内容
String output = choise.getText().toString();
Toast.makeText(MainActivity.this, "你的性别为:" + output, Toast.LENGTH_SHORT).show();
}
});
效果图:
 
 
 
方法二:通过if的方法来对获得的id与实例化的xml中的RadioButton的ID举行校验,找出选中状况的RadioButton控件
hobby = (RadioGroup) findViewById(R.id.hobby);
baskeball = (RadioButton) findViewById(R.id.basketball);
table_tennis = (RadioButton) findViewById(R.id.table_tenis);
badminton = (RadioButton) findViewById(R.id.badminton);
// 方法二,通过if的方法来对获得的id与实例化的xml中的RadioButton的ID举行校验,找出选中状况的RadioButton控件
hobby.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (baskeball.getId() == checkedId) {
shuchu = baskeball.getText().toString();
}
if (table_tennis.getId() == checkedId) {
shuchu = table_tennis.getText().toString();
}
if (badminton.getId() == checkedId) {
shuchu = badminton.getText().toString();
}
Toast.makeText(MainActivity.this, "你喜好的体育行动为:" + shuchu, Toast.LENGTH_SHORT).show();
}
});
效果图:
 

上篇:上一篇:Android后台长期执行定时循环任务
下篇:下一篇:Android WebView应用