radioButton
布局:
<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的更多相关文章
- RadioButton与CheckBox
笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...
- 【WPF】值是枚举的RadioButton 绑定问题
源 1.RadioButton 2.IValueConverter 3.枚举 xaml实现 <RadioButton Content="单打热身" GroupName=&qu ...
- Android RadioGroup和RadioButton详解
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...
- react native RadioButton(单选按钮)
刚刚写完这个多选按钮,我觉得没有单选的话,总会觉得有一点点不爽,因为在项目中我也没有用到单选,所以我没有好好研究源码,所以我在Github上找了一下,发现有一个挺好的,简单,不花哨. 在Github上 ...
- RadioButton(单选按钮)文字在按钮的左边
<RadioButton style="@style/CustomCheckboxTheme" android:layout_width="fill_parent& ...
- RadioGroup、RadioButton、CheckBox、Toast用法
xml布局文件如下: <RadioGroup android:id="@+id/sex" android:layout_width="wrap_content&qu ...
- 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch
[源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...
- 转 Android RadioButton设置选中时文字和背景颜色同时改变
主要应用在购物车,像淘宝的那样,点击以后弹出一个选择种类颜色这样的popuwindow以后,然后这个选择种类的地方要用到类似这个玩意儿. 搜了一下,效果和这个文章一致.转了. 原文地址:http:// ...
- WPF中RadioButton绑定数据的正确方法
RadioButton一般用于单选的时候,也就是从一组值中选择一个值. 比如性别有“男”和“女”两种取值,而对于一个员工的实例来说,性别的取值要么是男,要么是女. 这种时候一般就会用到RadioBut ...
- Android-组件RadioButton使用技巧
当初第一次接触Android组件的时候,我感觉微信底部的菜单,是用button这样的组件来做的. 可我想错了.却是用RadioButton来做的.那到底怎么做,接下来我就做一下分享!希望看了之后你也觉 ...
随机推荐
- java动态生成带下拉框的Excel导入模板
在实际开发中,由于业务需要,常常需要进行Excel导入导出操作.以前做一些简单的导入时,先准备一个模板,再进行导入,单有十几. 二十几个导入模板时,往往要做十几.二十几个模板.而且,当在模板中需要有下 ...
- axis2 webservice 发布、调用与项目集成
发布 1.在apache官网下载axis2包,下载Binary Distribution和War Distribution两个zip. 2.将war放入tomcat webapps下部署.并输入 ht ...
- [Effective JavaScript 笔记]第42条:避免使用轻率的猴子补丁
41条对违反抽象原则行为的讨论之后,下面聊一聊终极违例.由于对象共享原型,因此每一个对象都可以增加.删除或修改原型的属性.这个有争议的实践通常称为猴子补丁. 猴子补丁示例 猴子补丁的吸引力在于其强大. ...
- unity3d web.config设置
原地址:http://www.cnblogs.com/88999660/archive/2013/03/22/2976105.html <?xml version="1.0" ...
- 对target="framename"的理解(实现分页的demo)
先上图,说明一下我主要想实现什么功能. 一.演示图 演示首页: 演示内容页(包括按钮切换页+模板内容页): 演示首页到演示内容页的一个演变过程:
- Tomcat ClassLoader机制介绍
本文旨在介绍JVM的类加载机制:同时分析Tomcat不能采用默认的加载机制的原因,并对其加载机制做了介绍. 1.JVM中的类加载机制 在Java2之后的版本中,类的加载采用的是一种称为双亲委派的代理模 ...
- python-twisted
环境:win7 64位,python 2.7.3 安装: http://twistedmatrix.com/Releases/Twisted/14.0/Twisted-14.0.0.win-amd64 ...
- Android-自定义meta-data扩展数据
在接入第三方渠道SDK的时候,经常会看到其配置文件AndroidManifest.xml有类似如下的定义: [html] view plaincopy <!-- appid --> < ...
- PHP 冒泡原理
header('Content-Type: text/html; charset=utf-8'); // 简单冒泡算法 $a = array(5,43,3,2,1); function mp($a){ ...
- 【Django】Django 如何支持 分组查询、统计?
代码: from django.db.models import Sum alarm_sum_group_items = models.FILE_PROTECT_ALARM.objects.filte ...