单选,多选及其OnCheckedChange监听器
来源:互联网 发布:audition for mac 编辑:程序博客网 时间:2024/05/16 14:28
java代码
package com.example.zhang.checkbutton;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.RadioGroup;import android.widget.TextView;public class MainActivity extends AppCompatActivity { //创造RadioGroup和TextView的实例 RadioGroup rg; TextView show; //重写Oncreate方法 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //通过findViewById方法获取xml文件中activity界面的两个组件,rg和show rg = (RadioGroup) findViewById(R.id.radioGroup1); show = (TextView) findViewById(R.id.show); //下面是构造RadioGroup的OnCheckedChangeListener监听器,并重写 RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() { String text; @Override //重写,这里用了if判断语句,还有更高好的方法,见下面补充 public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId == R.id.male){ text = "你的性别是男人"; } else text = "你的性别是女人"; show.setText(text); } }; //为rg绑定监听器 rg.setOnCheckedChangeListener(onCheckedChangeListener); }}/*下面介绍一个更好的方法package org.crazyit.ui;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.RadioGroup;import android.widget.TextView;public class MainActivity extends Activity{ RadioGroup rg; TextView show; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取界面上rg、show两个组件 rg = (RadioGroup) findViewById(R.id.rg); show = (TextView) findViewById(R.id.show); // 为RadioGroup组件的OnCheckedChange事件绑定事件监听器 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 根据用户勾选的单选按钮来动态改变tip字符串的值 String tip = checkedId == R.id.male ? "您的性别是男人": "您的性别是女人"; // 修改show组件中的文本 show.setText(tip); } }); }}*/
xml代码
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.zhang.checkbutton.MainActivity" tools:showIn="@layout/activity_main"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别" android:textSize="22dp"/> <RadioGroup android:id="@+id/radioGroup1" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <!--定义两个单选按钮--> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/male" android:text="男" android:checked="true"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="女"/> </RadioGroup> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜欢的颜色" android:textSize="22dp"/> <!--定义一个垂直线性布局--> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--定义三个多选按钮--> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Red" android:text="红色"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Blue" android:text="蓝色"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Yellow" android:text="黄色"/> </LinearLayout> </TableRow> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content"/></TableLayout>
0 0
- 单选,多选及其OnCheckedChange监听器
- RadioGroup OnCheckedChange
- CheckBox OnCheckedChange
- bootstrapTable 单选及其取值
- AWT事件及其监听器
- Java事件及其监听器小结
- Android--UI事件监听器及其处理程序
- Struts2单选按钮标签s:radio的使用及其设置默认值
- Struts2单选按钮标签s:radio的使用及其设置默认值
- Struts2单选按钮标签s:radio的使用及其设置默认值《转载》
- Struts2单选按钮标签s:radio的使用及其设置默认值
- Struts2单选按钮标签s:radio的使用及其设置默认值
- 单词表及其函数
- 单例模式,多例模式及其应用
- 单例模式,多例模式及其应用
- 单选
- MFC单文档及其简介
- 单任务操作系统及其应用程序
- node 请求
- 使用Mahout搭建推荐系统之入门篇4-Mahout实战
- Java之多线程内存可见性_1
- python34+pyserial2.7+PyQt5开发环境配置方法
- QT配置问题
- 单选,多选及其OnCheckedChange监听器
- 一款炫酷Loading动画--加载失败
- Rich Go language support for Visual Studio Code
- 3g驱动移植
- C#使用参数数组
- C++内联(inline)
- 三种按钮,普通,带背景,背景切换
- Velocity新手入门
- Android酷炫实用的开源框架(UI框架)