布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.day03_radiobutton.MainActivity" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别" /> <RadioGroup
android:id="@+id/group_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="爱好" /> <RadioGroup
android:id="@+id/group_favor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="网球" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"/> <RadioButton
android:id="@+id/button_fight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
</RadioGroup> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" /> </LinearLayout>

代码:

public class MainActivity extends Activity {
private RadioGroup group_sex, group_favor;
private RadioButton button_fight;
private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group_sex = (RadioGroup) findViewById(R.id.group_sex);
group_favor = (RadioGroup) findViewById(R.id.group_favor);
button_fight = (RadioButton) findViewById(R.id.button_fight);
button_fight.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(MainActivity.this,
"你……确定?",
Toast.LENGTH_LONG).show();
}
}
}); button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
String str = "";
// 读取radioGroup的状态
// 提取radioGroup的子控件
for(int i = 0; i < group_sex.getChildCount(); i++) {
RadioButton button = (RadioButton) group_sex.getChildAt(i);
if(button.isChecked()) {
str += "性别:" + button.getText() + "\n";
break;
}
}
for(int i = 0; i < group_favor.getChildCount(); i++) {
RadioButton button = (RadioButton) group_favor.getChildAt(i);
if(button.isChecked()) {
str += "爱好:" + button.getText();
break;
}
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

radioButton的更多相关文章

  1. RadioButton与CheckBox

    笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...

  2. 【WPF】值是枚举的RadioButton 绑定问题

    源 1.RadioButton 2.IValueConverter 3.枚举 xaml实现 <RadioButton Content="单打热身" GroupName=&qu ...

  3. Android RadioGroup和RadioButton详解

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...

  4. react native RadioButton(单选按钮)

    刚刚写完这个多选按钮,我觉得没有单选的话,总会觉得有一点点不爽,因为在项目中我也没有用到单选,所以我没有好好研究源码,所以我在Github上找了一下,发现有一个挺好的,简单,不花哨. 在Github上 ...

  5. RadioButton(单选按钮)文字在按钮的左边

    <RadioButton style="@style/CustomCheckboxTheme" android:layout_width="fill_parent& ...

  6. RadioGroup、RadioButton、CheckBox、Toast用法

    xml布局文件如下: <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content&qu ...

  7. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  8. 转 Android RadioButton设置选中时文字和背景颜色同时改变

    主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...

  9. WPF中RadioButton绑定数据的正确方法

    RadioButton一般用于单选的时候,也就是从一组值中选择一个值. 比如性别有“男”和“女”两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女. 这种时候一般就会用到RadioBut ...

  10. Android-组件RadioButton使用技巧

    当初第一次接触Android组件的时候,我感觉微信底部的菜单,是用button这样的组件来做的. 可我想错了.却是用RadioButton来做的.那到底怎么做,接下来我就做一下分享!希望看了之后你也觉 ...

随机推荐

  1. 详述Linux ftp命令的使用方法

    转自:http://os.51cto.com/art/201003/186325.htm ftp服务器在网上较为常见,Linux ftp命令的功能是用命令的方式来控制在本地机和远程机之间传送文件,这里 ...

  2. 失落的C语言结构体封装艺术

    Eric S. Raymond <esr@thyrsus.com> 目录 1. 谁该阅读这篇文章 2. 我为什么写这篇文章 3.对齐要求 4.填充 5.结构体对齐及填充 6.结构体重排序 ...

  3. 改变edittext边框颜色

    转载自:点击打开链接 第一步:为了更好的比较,准备两个一模一样的EditText(当Activity启动时,焦点会在第一个EditText上,如果你不希望这样只需要写一个高度和宽带为0的EditTex ...

  4. C++ Set

    set集合容器:实现了红黑树的平衡二叉检索树的数据结构,插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,以保证每个子树根节点键值大于左子树所有节点的键值,小于右子树所有节点的键值:另外,还 ...

  5. 二级域名session 共享方案

    二级域名session 共享方案   1.利用COOKIE存放session_id(); 实例: 域名一文件php代码: <?php session_start(); setcookie(&qu ...

  6. EtherCAT报文寻址

    EtherCAT通信通过主站发送EtherCAT数据帧读写从站设备的内部存储区实现. 一个EtherCAT网段相当于一个以太网设备,主站首先通过以太网数据帧头的MAC地址寻址到网段,然后使用Ether ...

  7. iOS 利用constraint实现2个控件上下的空白是相等的

    说的有点乱,先看个图把 其实这个constrant的目的就是控制两个方形的控件上方和下方的空白大小. 对于每一个方块来说,他们上方和下方的空白是相同的.这种“居中”的设计到处可见.一个控件想实现这种居 ...

  8. Java for LeetCode 068 Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  9. 【JAVA、C++】LeetCode 020 Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  10. codeforces B. Eight Point Sets 解题报告

    题目链接:http://codeforces.com/problemset/problem/334/B 一开始看到题目,有点怯,理解了题目后,其实并不难.这句话是突破口 three distinct ...