2.常用控件:RadioGroup和RadioButton
来源:互联网 发布:淘宝店铺怎么搞双11 编辑:程序博客网 时间:2024/06/05 15:26
常用控件:RadioGroup和RadioButton
一.核心代码:
视图:
在相应的Activity布局文件中声明<RadioGroup/><RadioButton/>。
<RadioGroupandroid:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /></RadioGroup>
数据:
在Activity中可以通过View android.app.Activity.findViewById(int id)函数返回RadioGroup对象。
在Activity中可以通过View android.app.Activity.findViewById(int id)函数返回RadioButton对象。
为RadioGroup对象设置监听器genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()):
创建一个匿名内部类并实现android.widget.RadioGroup.OnCheckedChangeListener接口,作为参数传入RadioGroup对象的setOnCheckedChangeListener(OnCheckedChangeListener listener)函数中。
//通过控件的ID来得到代表控件的对象 genderGroup = (RadioGroup)findViewById(R.id.genderGroup); femaleButton = (RadioButton)findViewById(R.id.femaleButton); maleButton = (RadioButton)findViewById(R.id.maleButton); //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同 genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");}}});
二.控件图例:
三.具体应用:
XML文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><RadioGroupandroid:id="@+id/genderGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /></RadioGroup></LinearLayout>
Activity.java文件:
package mars.activity07;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class RadioTest extends Activity { /** Called when the activity is first created. *///对控件对象进行声明private RadioGroup genderGroup = null;private RadioButton femaleButton = null;private RadioButton maleButton = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio); //通过控件的ID来得到代表控件的对象 genderGroup = (RadioGroup)findViewById(R.id.genderGroup); femaleButton = (RadioButton)findViewById(R.id.femaleButton); maleButton = (RadioButton)findViewById(R.id.maleButton); //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同 genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(femaleButton.getId() == checkedId){System.out.println("famale");Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();}else if(maleButton.getId() == checkedId){System.out.println("male");}}}); } }
四.项目文件地址
阅读全文
0 0
- 2.常用控件:RadioGroup和RadioButton
- 控件RadioGroup、RadioButton和CheckBox的使用
- Android控件——RadioButton和RadioGroup
- Android基本控件RadioGroup和RadioButton学习
- Android按钮控件之RadioGroup和RadioButton
- Android控件之RadioGroup和RadioButton
- Android控件之RadioButton和RadioGroup
- Android控件RadioButton&RadioGroup
- android应用开发笔记---常用控件RadioGroup和RadioButton及Toast
- Android开发之四(五):常用控件之单项选择(RadioGroup和RadioButton)
- Android常用控件(一)【RadioGroup、RadioButton、CheckBox、Toast】
- RadioGroup和RadioButton
- RadioGroup和RadioButton
- RadioGroup和RadioButton
- RadioGroup和RadioButton
- 使用RadioButton 和RadioGroup
- RadioGroup和Radiobutton
- Android RadioGroup和RadioButton
- Einbahnstrasse HDU
- 文章标题
- 2002: [Hnoi2010]Bounce 弹飞绵羊
- 模糊查询
- 设计模式:各个模式间的对比
- 2.常用控件:RadioGroup和RadioButton
- 使用adb时,为什么只有RockChip的ID需要加入到adb_usb.ini文件里呢?
- 静态库和动态库
- hadoop、Storm该选哪一个
- listview加载跟多
- Git错误fatal: unable to access 'https://github.com/****.git/': Couldn't resolve host 'github.com'
- springMVC3和springMVC4解决ajax跨域请求的问题
- 自定义简陋版SpringMVC
- 图论题集收藏