Android:单选按钮
2016-01-06 13:30
441 查看
要完成单选框显示,我们需要使用到RadioGroup和RadioButton(单选框),RadioGroup用于对单选框进行分组,相同组内的单选框只有一个单选框能被选中。
lRadioGroup.check(R.id.dotNet);将id名为dotNet的单选框设置成选中状态。
l(RadioButton)
findViewById(radioGroup.getCheckedRadioButtonId());//获取被选中的单选框。
lRadioButton.getText();//获取单选框的值
l调用setOnCheckedChangeListener()方法,处理单选框被选择事件,把RadioGroup.OnCheckedChangeListener实例作为参数传入
界面设计:
<?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"
>
<RadioGroup android:id="@+id/radioGroup"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java" />
<RadioButton android:id="@+id/dotNet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dotNet" />
<RadioButton android:id="@+id/php"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PHP" />
</RadioGroup>
</LinearLayout>
处理程序:
public void onCreate(Bundle savedInstanceState) {
......
RadioGroupradioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener() {
publicvoid onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) findViewById(checkedId);
Log.i(TAG, String.valueOf(radioButton.getText()));
}
});
}
相关文章推荐
- 关于android 联系人数据库处理
- Android:对话框
- android studio下载地址
- Android 传感器 II-运动传感器
- Android adb命令整理
- android studio IDE 下,设置ACTIVITY全屏
- Android中采用JNI对Bitmap进行处理,比Canvas处理快85倍左右。
- 本地导入Android-PullToRefresh库问题解决
- Android系统源码下载
- Android材料设计第一篇
- 1.1 Android环境准备有哪些
- Android进阶之性能优化
- Android:TabHost
- 在Android上实现多进程构架的浏览器(浏览器开发)的讨论
- Android:ListView数据的分批加载
- Android系统之路(初识MTK) ------ 客制化开机LOGO跟开机动画/系统默认壁纸
- Android实现打电话和发短信
- Android:ListView数据异步加载、Handler、AsyncTask
- ArrayList 扩容 Android Java 真的不一样
- FindBugs-AndroidStudio代码错误自动检查插件