main.xml
<?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="@string/hello" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/genderRg">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/maleRb"
android:text="@string/maleTxt"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/femaleRb"
android:text="@string/femaleTxt"/>
</RadioGroup>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">性别选择</string>
<string name="app_name">RadioButton</string>
<string name="maleTxt">男</string>
<string name="femaleTxt">女</string>
</resources>
RadioButtonActivity.java
package com.qianlong.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class RadioButtonActivity extends Activity {
private RadioGroup genderRg;
private RadioButton maleRb;
private RadioButton femaleRb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.genderRg = (RadioGroup) this.findViewById(R.id.genderRg);
this.maleRb = (RadioButton) this.findViewById(R.id.maleRb);
this.femaleRb = (RadioButton) this.findViewById(R.id.femaleRb);
this.genderRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int maleRbId = RadioButtonActivity.this.maleRb.getId();
int femaleRbId = RadioButtonActivity.this.femaleRb.getId();
String youchecked = "您选择了:";
if(maleRbId == checkedId) {
youchecked += "男";
Toast.makeText(RadioButtonActivity.this, youchecked, Toast.LENGTH_SHORT).show();
}
if(femaleRbId == checkedId) {
youchecked += "女";
Toast.makeText(RadioButtonActivity.this, youchecked, Toast.LENGTH_SHORT).show();
}
}
});
}
}
分享到:
相关推荐
Android_实例_利用RadioButton实现分类筛选
Android入门程序_RadioButton的使用
android 自定义RadioButton的样式android 自定义RadioButton的样式
Python GUI编程(第一部分) 10_Radiobutton_Checkbutton详解.mp4
单选框确认选择文件或文件夹(radioButton1选择文件_radioButton2选择文件夹).zip
单选框确认选择文件或文件夹(radioButton1选择文件_radioButton2选择文件夹).rar
master GroupBox, SingleLineEdit, CommandButton, RadioButton, PictureButton controls such as the similarities and differences to further understanding of data objects window displays various occasions...
android 自定义 单选按钮 radioButton
RadioButton的个人调查 android:id="@+id/ib1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一题" android:textSize="15dp" /> android:id="@+id/ib2" ...
<com.triman.android.ui.MyRadioButton android:id="@+id/rb_sys_set" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:...
原来在用android的TabHost生成的选项卡时有一定的局限性,它不能很好地用布局控制,而我转而采用android的RadioButton来生成选项卡并实现选项卡功能,并且这个代码还实现了ViewFlipper实现选项卡根据手势左右滑动的...
android中RadioButton的图标在左边,文字在右边,如何实现文字在左边,图标在右边? 第一步:将RadioButton自带的图标隐藏, 第二步:在隐藏图标的右边添加系统的图标 第三步:在隐藏图标的位置显示文字. 具体参考: ...
自定义android RadioButton样式,代码说明地址:http://blog.csdn.net/jason_996/article/details/62889374
android组件 RadioButton,CheckBox,Toast简单介绍,
android Menu 和 RadioGroup RadioButton的用法
最近写项目有个需求是单选框必须改成设计要的样子,然后再网上找了好久,自己调试了一下,就好了
tabhost原效果并不理想 这里利用button 改善
支持使用selector的自定义RadioButton
安卓Android源码——(RadioButton与监听).zip
android自定义RadioButton,可用于竖直方向的tabhost组件的自定义设计,实现了tabWiget的效果。