Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7565220
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: Android平台

2015-11-19 14:19:20

单选钮(RadioGroup)上也可以进行事件的处理操作,当用户选中了某选项之后也将触发相应的监听器进行若干处理

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout                     ?    定义线型布局管理器
  3.     xmlns:android=""
  4.     android:orientation="vertical"             ?    所有组件垂直摆放
  5.     android:layout_width="fill_parent"            ?    布局管理器宽度为屏幕宽度
  6.     android:layout_height="fill_parent">        ?    布局管理器高度为屏幕高度
  7.     <TextView                    ?    文本显示组件
  8.         android:id="@+id/show"            ?    组件ID,程序中使用
  9.         android:text="您的性别是:"        ?    默认显示文字
  10.         android:textSize="20px"            ?    文字大小为20像素
  11.         android:layout_width="fill_parent"        ?    组件宽度为屏幕宽度
  12.         android:layout_height="wrap_content"/>    ?    组件高度为文字高度
  13.     <RadioGroup                    ?    单选钮
  14.         android:id="@+id/sex"            ?    组件ID,程序中使用
  15.         android:layout_width="fill_parent"        ?    组件宽度为屏幕宽度
  16.         android:layout_height="wrap_content"    ?    组件高度为文字高度
  17.         android:orientation="vertical"        ?    所有选项垂直摆放
  18.         android:checkedButton="@+id/male">        ?    设置默认选中项
  19.         <RadioButton            ?    定义单选项
  20.             android:id="@+id/male"        ?    组件ID,程序中使用
  21.             android:text="男"/>        ?    默认显示文字
  22.         <RadioButton            ?    定义单选项
  23.             android:id="@+id/female“    ?    组件ID,程序中使用
  24.             android:text="女

点击(此处)折叠或打开

  1. @Override
  2.     public void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.         super.setContentView(R.layout.main);
  5.         this.show = (TextView) super.findViewById(R.id.show) ;    // 取得文本框
  6.         this.sex = (RadioGroup) super.findViewById(R.id.sex) ;// 取得单选钮选项
  7.         this.male = (RadioButton) super.findViewById(R.id.male) ;
  8.         this.female = (RadioButton) super.findViewById(R.id.female) ;
  9.         this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl()) ;
  10.     }
  11.     private class OnCheckedChangeListenerImpl implements
  12.             OnCheckedChangeListener {
  13.         @Override
  14.         public void onCheckedChanged(RadioGroup group, int checkedId) {
  15.             String temp = null ;
  16.             if (MyRadioListenerDemo.this.male.getId() == checkedId) {
  17.                 temp = MyRadioListenerDemo.this.male
  18.                     .getText().toString(); // 取得单选钮文本
  19.             }
  20.             if (MyRadioListenerDemo.this.female.getId() == checkedId) {
  21.                 temp = MyRadioListenerDemo.this.female
  22.                     .getText().toString();// 取得单选钮文本
  23.             }
  24.             MyRadioListenerDemo.this.show
  25.                 .setText("您的性别是:" + temp);    // 设置文本显示
  26.         }
  27.     }
020603_单选钮与OnCheckedChangeListener.ppt

阅读(1496) | 评论(0) | 转发(0) |
0

上一篇:单击事件

下一篇:下拉列表监听

给主人留下些什么吧!~~