android开发之RadioGroup的使用
来源:互联网 发布:软件升级工具 编辑:程序博客网 时间:2024/06/02 01:35
最近自学android开发,所以想写下一点东西来见证自己的成长,就先从RadioGroup开始。
RadioGroup是一系列radiobutton的集合,可以分别设置radiobutton的点击事件,所以这篇博客教会大家怎么使用radiogroup 先上图
可以看到,背景音乐,游戏音效,玩家机型旁边都有radiobutton,但我们使用的是radiogroup,每个radiogroup中有2个radiobutton。界面设置完成之后就可以为radiogroup绑定监听事件了,而这里的监听事件是 OnCheckedChangeListener(),当点击一个radio button时做出相应的行为。
直接贴代码吧,这个比较简单
首先是布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="背景音乐" /> <RadioGroup android:id="@+id/rbtn_bmusic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rbtn_bmusic_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开" /> <RadioButton android:id="@+id/rbtn_bmusic_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="游戏音效" /> <RadioGroup android:id="@+id/rbtn_sound" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/rbtn_sound_open" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开" /> <RadioButton android:id="@+id/rbtn_sound_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="玩家机型" /> <RadioGroup android:id="@+id/rbtn_planetype" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:orientation="horizontal"> <RadioButton android:id="@+id/rbtn_planetype_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="机型一"></RadioButton> <RadioButton android:id="@+id/rbtn_planetype_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:text="机型二" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <ImageView android:id="@+id/iv_type1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/green_plane" /> <ImageView android:id="@+id/iv_type2" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/paper_plane" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/btn_comfirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> </LinearLayout> <TextView android:id="@+id/tv_comfirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaa" /></LinearLayout>
然后是Activity里的代码:
因为我这里设置了,把选择之后的情况提交之后,在下面的textview中显示,所以还为button绑定了一个监听器
package com.example.hc.leishengame;import android.app.Activity;import android.os.Bundle;import android.support.annotation.IdRes;import android.support.annotation.Nullable;import android.view.View;import android.widget.Button;import android.widget.RadioGroup;import android.widget.TextView;/** * Created by hc on 2017/8/2. */public class GameSettingActivity extends Activity { private Button btn_comfirm; private RadioGroup rbtn_bmusic, rbtn_sound, rbtn_planetype; private TextView tv_comfirm; String bgmusic = "开"; String sound = "开"; String planetype = "机型一"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.parameterset); initView(); initEvent(); } public void initView() { btn_comfirm = (Button) findViewById(R.id.btn_comfirm); rbtn_bmusic = (RadioGroup) findViewById(R.id.rbtn_bmusic); rbtn_sound = (RadioGroup) findViewById(R.id.rbtn_sound); rbtn_planetype = (RadioGroup) findViewById(R.id.rbtn_planetype); tv_comfirm = (TextView) findViewById(R.id.tv_comfirm); } public void initEvent() { RadioGroup.OnCheckedChangeListener checkedChangeListener = new MyChangedOnClickListenr(); rbtn_bmusic.setOnCheckedChangeListener(checkedChangeListener); rbtn_sound.setOnCheckedChangeListener(checkedChangeListener); rbtn_planetype.setOnCheckedChangeListener(checkedChangeListener); View.OnClickListener myOnClickListener = new MyOnClickListener(); btn_comfirm.setOnClickListener(myOnClickListener); } class MyChangedOnClickListenr implements RadioGroup.OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup radioGroup,int i) { switch (radioGroup.getId()) { case R.id.rbtn_bmusic: if (i == R.id.rbtn_bmusic_open) { bgmusic = "开"; } else { bgmusic = "关"; } break; case R.id.rbtn_sound: if (i == R.id.rbtn_sound_open) { sound = "开"; } else { sound = "关"; } break; case R.id.rbtn_planetype: if (i == R.id.rbtn_planetype_one) { planetype = "机型一"; } else { planetype = "机型二"; } break; default: break; } } } class MyOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { StringBuilder sb = new StringBuilder(); sb.append("背景音乐" + bgmusic + ",游戏音效" + sound + ",玩家机型" + planetype); tv_comfirm.setText(sb); } }}
阅读全文
0 0
- android开发之RadioGroup的使用
- Android开发(20)--RadioGroup的使用
- Android开发笔记----radioGroup的使用
- android学习笔记之RadioGroup的使用
- android RadioGroup的使用
- Android开发之RadioGroup与RadioButton控件使用
- android 之RadioGroup 的换行
- Android RadioGroup控件的使用
- Android 多选一 RadioGroup的使用
- Android自学笔记之RadioGroup和RadioButton的使用
- Android控件系列之RadioButton与RadioGroup的基本使用
- Android开发之自动换行RadioGroup
- Android PopupWindow RadioGroup RadioButton 的使用
- Android—RadioGroup和RadioButton的使用
- Android的RadioGroup简单使用例子
- 20、Android之RadioGroup
- Android RadioGroup 之 RadioBUtton
- Android UI之RadioGroup
- 因子回测效果框架
- Centos7下,sed命令用法介绍
- NOIP2016滚粗记
- 很多人不知道的ASTGO三个命令解决服务器重启后各种问题
- linux文件系统测试工具
- android开发之RadioGroup的使用
- strdup与strcpy具体的区别
- banner图的开发
- CC2640R2F BLE5.0 安装BLE5-Stack 1.00.00
- TensorFlow学习资料:代码+笔记+教程
- SQL语句跨表查询
- Ubuntu16.04 只能以游客身份登录问题
- 表的主键与外键
- np.searchsorted