Android单选框(RadioButton)
来源:互联网 发布:免费八字排盘 网络中国 编辑:程序博客网 时间:2024/06/03 19:42
●使用Android单选框的时候,要用RadioGroup来分组
●选择RadioGroup里的单选框的时候,将调用RadioGroup的Check·()方法
●追加单选框被选择时处理的时候,
1.调用setOnCheckedChangeListener()方法,并把RadioGroup.OnCheckedChangeListener实例作为参数传入
2.在RadioGroup.OnCheckedChangeListener的onCheckedChanged()方法里,取得被选中
看一个小例子:
package com.xzy.demo.ui;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;public class TestUI_demo001Activity extends Activity {private Button button;private RadioGroup rg;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 设置容器布局文件setContentView(R.layout.main);// 获得单选按钮组和button按钮rg = (RadioGroup) findViewById(R.id.RadioGroup1);button = (Button) findViewById(R.id.Button1);// 为按钮组绑定事件rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton r = (RadioButton) findViewById(checkedId);Toast.makeText(TestUI_demo001Activity.this,"你选择的是:" + r.getText(), 3000).show();}});// 为按钮绑定事件button.setOnClickListener(new OnClickListener() {public void onClick(View v) {for (int i = 0; i < rg.getChildCount(); i++) {RadioButton r = (RadioButton) rg.getChildAt(i);if (r.isChecked()) {Log.i("单选按钮", (String) r.getText());Toast.makeText(TestUI_demo001Activity.this,r.getText(), 3000).show();break;}}}});}}
布局文件:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/background2" android:orientation="horizontal" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="性别" android:height="50px" /> <RadioGroup android:id="@+id/RadioGroup1" android:layout_width ="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/RadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true" /> <RadioButton android:id="@+id/RadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup><Button android:id="@+id/Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择" /></LinearLayout>
对于点击单选框和选择按钮,都会有相应的反应。- Android单选框(RadioButton)
- android RadioButton
- android RadioButton
- Android RadioButton
- RadioButton - android 自定义RadioButton样式
- Android基础控件之单选框(RadioButton)
- [Android]定制RadioButton样式
- android 重写RadioButton
- Android RadioButton example
- android RadioButton注意事项
- Android 定制RadioButton样式
- Android 定制RadioButton样式
- Android 定制RadioButton样式
- android 自定义RadioButton样式
- android RadioButton的问题
- Android视图组件: RadioButton
- Android RadioButton学习笔记
- Android RadioButton的自定义
- java基础--IO流(上)字符流
- VS2010出现warning C4996的解决方法
- Cocos2d-x利用CCSpriteBatchNode提高渲染效率
- 三大移动Web开发框架
- phonegap前后台数据交互
- Android单选框(RadioButton)
- PHP中常用的页面自动跳转方法
- asp.net控件开发基础(3) ----------自定义控件事件处理
- android-Volley框架简介
- 解决问题:EXT4 filefield 文件上传在IE8上返回状态无效,弹出下载页面
- 打印
- Apache与Tomcat的区别
- 在linux下安装mysql服务器
- php cookie禁用时session方案