安卓编程——单选框RadioGroup,单选按钮RadioButton的使用
来源:互联网 发布:西安深蓝软件培训 编辑:程序博客网 时间:2024/04/29 04:03
最近开始学习安卓一些基本的编程,于是开始总结、整理各个要点,便于记住以及回顾。
RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法。
RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中。
使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响应按钮的事件;
下面是一个实例:
layout文件:
<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.testapp.MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/information" /><LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sex"/> <RadioGroup android:id="@+id/rgroup" android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="match_parent" > <RadioButton android:id="@+id/ch1" android:text="@string/male" android:layout_height="wrap_content" android:layout_width="wrap_content" android:checked="true" /> <RadioButton android:id="@+id/ch2" android:text="@string/female" android:layout_height="wrap_content" android:layout_width="wrap_content" android:checked="true" /> </RadioGroup></LinearLayout> <TextView android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/hint" /></LinearLayout>
这里RadioButton的android:checked属性可以多个都选择为true,但运行之后只会选择最后一个checked属性作为初始状态。
很多初学者都会遇到一个问题:程序代码(包括xml文件)均无错误提示,但是在设备上运行时候却出错,其中一个原因就是布局或者组件没有指定layout_width和layout_height属性,导致运行出错!
Java代码:
import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.TextView;public class MainActivity extends ActionBarActivity {private RadioGroup rgroup;private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rgroup=(RadioGroup)findViewById(R.id.rgroup); show=(TextView)findViewById(R.id.show); //通过setOnCheckedChangeListener( )来响应按钮的事 rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup rg,int checkedId) { switch(checkedId){ case R.id.ch1:show.setText("您的性别为:男性");break; case R.id.ch2:show.setText("您的性别为:女性");break; } } }); }}
1 0
- 安卓编程——单选框RadioGroup,单选按钮RadioButton的使用
- 单选框RadioGroup,单选按钮RadioButton的使用
- 单选框RadioGroup,单选按钮RadioButton的使用
- 安卓控件之单选按钮 (RadioButton和RadioGroup)
- 安卓RadioButton,RadioGroup,Button按钮的使用
- 单选按钮RadioButton&RadioGroup
- 单选按钮RadioGroup与RadioButton实例
- android 单选按钮 RadioGroup RadioButton
- RadioButton单选按钮实现RadioGroup.OnCheckedChangeListener
- 单选按钮RadioGroup和RadioButton
- 安卓控件使用系列10:RadioButton单选按钮的使用
- 单选按钮RadioGroup的使用
- 使用RadioButton和RadioGroup实现多种多样的单选情况
- 安卓RadioGroup和RadioButton简单使用
- Android—RadioGroup和RadioButton的使用
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- Android 单选按钮RadioButton的使用
- 单选按钮 RadioButton 的使用
- android 获取imei(国际移动设备身份码)号码详解及其应用场景
- Eclipse for PHP + Xdebug搭建PHP环境
- 【索引】Chapter 3. Data Structures
- 前端开发工具---FIS使用总结
- 题目1002:Grading
- 安卓编程——单选框RadioGroup,单选按钮RadioButton的使用
- opendns 可视化框架
- aspx,ascx和ashx使用小结
- 简单的ajax异步传参
- inux 下的mongodb搭建
- convert C++
- 进程与线程(一)
- 【索引】Fundamental Data Structures
- 大文件上传Jquery 插件Uploadify-v2.1.4使用图解