RadioButton
来源:互联网 发布:网络射像头 编辑:程序博客网 时间:2024/05/16 09:07
Radio buttons 只运行用户选择一个选项,一般都是在RadioGroup中放置一组Radio buttons
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
上面这两个RadioButton的回调函数都是android:onClick="onRadioButtonClicked。我们可以在activity中根据android:id="@+id/radio_ninjas"来判断是哪个RadioButton 被选中了。
如下所示:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
和checkbox 一样RadioButton也是CompoundButton.java的子类,可以调用setChecked(boolean)和 toggle() 来改变RadioButton的状态.
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
上面这两个RadioButton的回调函数都是android:onClick="onRadioButtonClicked。我们可以在activity中根据android:id="@+id/radio_ninjas"来判断是哪个RadioButton 被选中了。
如下所示:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
和checkbox 一样RadioButton也是CompoundButton.java的子类,可以调用setChecked(boolean)和 toggle() 来改变RadioButton的状态.
0 0
- radiobutton
- radioButton
- Radiobutton
- RadioButton
- RadioButton
- RadioButton
- radiobutton
- RadioButton
- RadioButton
- RadioButton
- Radiobutton
- RadioButton
- RadioButton
- RadioButton
- RadioButton
- RadioButton与自定义RadioButton
- RadioButton控件
- RadioButton 类
- Web上传文件报错java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest
- 跨平台移动开发工具 Weex
- 主板诊断卡代码含义及处理方法
- Oracle:like模糊查询
- Android中文件的读写操作
- RadioButton
- [又值奥运季] 2016年里约奥运会--8月13日赛事
- 数据结构实验之图论四:迷宫探索
- TableView 或者collectionView 获取点击的cell 和当前行数的方法
- 8大常用通讯中间件/MQ使用总结
- Java多线程编程之同步器
- MVC下在控制器与试图中获取当前页面的URL与controller、action
- UVA 10815 Andy's First Dictionary
- Oracle集合运算