<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="性别:" /> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sex"> <RadioButton android:id="@+id/b1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="男"/> <RadioButton android:id="@+id/b2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认"/> </LinearLayout>
package com.android.RadioButton; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class Main extends Activity { private Button button; private RadioGroup group; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button)this.findViewById(R.id.button); group = (RadioGroup)this.findViewById(R.id.sex); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub int len = group.getChildCount();//获得单选按钮组的选线个数 String msg = ""; for (int i=0;i<len;i++){ //找到所有的radiobutton RadioButton radioButton = (RadioButton)group.getChildAt(i); //判断radiobutton是否被选中 if(radioButton.isChecked()){ msg = radioButton.getText().toString(); break; } } Toast.makeText(Main.this, msg, 1).show(); } }); } }
RadioGroup
公共方法
public void addView (View child, int index, ViewGroup.LayoutParams params)
使用指定的布局参数添加一个子视图
参数
child 所要添加的子视图
index 将要添加子视图的位置
params 所要添加的子视图的布局参数
public void check (int id)
如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作
参数
id 该组中所要勾选的单选按钮的唯一标识符(id)
参见
getCheckedRadioButtonId()
clearCheck()
public void clearCheck ()
清除当前的选择状态,当选择状态被清除,则单选按钮组里面的所有单选按钮将取消勾选状态,getCheckedRadioButtonId()将返回null
参见
check(int)
getCheckedRadioButtonId()
public RadioGroup.LayoutParams generateLayoutParams (AttributeSet attrs)
基于提供的属性集合返回一个新的布局参数集合
参数
attrs 用于生成布局参数的属性
返回值
返回一个ViewGroup.LayoutParams或其子类的实例
public int getCheckedRadioButtonId ()
返回该单选按钮组中所选择的单选按钮的标识ID,如果没有勾选则返回-1
返回值
返回该单选按钮组中所选择的单选按钮的标识ID
参见
check(int)
clearCheck()
public void setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)
注册一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数
参数
listener 当单选按钮勾选状态发生改变时所要调用的回调函数
public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)
注册一个当子内容添加到该视图或者从该视图中移除时所要调用的回调函数
参数
listener 当层次结构发生改变时所要调用的回调函数
相关推荐
android 自定义 单选按钮 radioButton
参考博文: http://blog.csdn.net/joe_storm/article/details/13773125
今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
本文将通过radiogroup和radiobutton实现组内信息的单选, 其中radiogroup就是将radiobutton进行分组,同一管理和控制 同时实现默认选中情况,获取默认值.
NULL 博文链接:https://andy2019.iteye.com/blog/1537189
Android RadioButton按钮组应用学习实例,本例将通过Android Tool提供的RadioGroup控件,将大大小小的RadioButton设定在一个按钮组中,实现单选效果,你可将本例所演示的功能用于做题或考试应用中,或者问卷调查中。
用于处理 android 中的单选按钮的自定义单选组如果您使用原生 android RadioGroup 类来处理带有波斯语文本的单选按钮,则在排列它们时会遇到一些问题。 通过使用 PersianRadioGroup,您可以将单选按钮分开布局(不在...
通过RadioButton与GridView,实现了单选按钮成表格布局,同时具备单选效果的功能。实现效果见http://blog.csdn.net/ddxxll2008/article/details/49421129
在Android中,单选按钮用RadioButton表示,而RadioButton类又是Button子类。 通常情况下,RadioButton组件需要与RadioGroup组件一起使用。 设置单选框 android:checked 为指定选中状态,即设定一个默认选择的...
一个动态添加RadioGroup的RadioButton的例子,只需要添加一小段的代码就可以动态生成一个单选按钮,项目编码GBK默认编译版本4.4.2
android-segmentedradiobutton, iPhone控件分段的Android实现 分割的用于Android的单选按钮这是我通过扩展RadioGroup和RadioButton来实现对Android的ios控件分段的实现。 包含的示例项目。屏幕截图 用法对于只有文本...
自实现模仿美图的支付功能的单选按钮的操作,具有自定义的布局,具体内容请见http://blog.csdn.net/js931178805/article/details/8823306
摘要:Java源码,Android,RadioButtonAndCheckBox RadioButton And CheckBox——Android单选多选按钮的应用举例源代码...单选按钮和多选按钮在Android中应用十分广泛,因此对于Android开发来说,本实例有必要学习掌握。
老罗Android开发视频教程》:查看http协议内容的工具httpwatch.zip 《老罗Android开发视频教程》第四集:如何根据范围和检索词发起范围检索.avi ...《老罗Android开发视频教程》第十集:RadioButton单选按钮的使用.mp4
android 自定义RadioButton(单选按钮)图标随便定.(组合控件的应用)
RadioButton是单选按钮,多个RadioButton放在一个RadioGroup控件中,也就是说每次只能有1个RadioButton被选中。而CheckBox是多选按钮,Toatst是android中带的一个用于显示提示小窗口消息的控件,其提示的内容过一会...
无圆圈单选按钮 只是没有圆形的简单单选按钮 检查屏幕截图。
由于Android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用GridView进行改造,实现单选效果,而要实现这样的效果重点就在GridView的适配器这块了。 首先是GridView的item的XML: <...
6.10 RadioButton单选按钮的使用 6.11 ToggleButton按钮的使用 6.12 CheckBox复选框控件使用 6.13 SeekBar拖动控件的使用 6.14 ImageView的基本用法 6.15 ImageView实现适屏和裁剪图片 6.16 ImageView实现图片旋转和...