Android的RadioGroup简单使用例子
来源:互联网 发布:淘宝详情页制作思路 编辑:程序博客网 时间:2024/06/05 22:42
今天给大家简单介绍一下RadioGroup的使用
我这边做的是一个根据点击按钮出现相应图片的例子
RadioGroup最重要的就是在xml文件中:
首先我用的是线性布局,在布局的上面是一张图片,下面则是一些按钮
<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=".MainActivity" > <ImageView android:id="@+id/iv_main_iv1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scaleType="fitXY" android:src="@drawable/s1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioGroup android:id="@+id/rg_main_rg1" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:id="@+id/rb_main_rb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="杨幂" /> <RadioButton android:id="@+id/rb_main_rb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="雪泫" /> <RadioButton android:id="@+id/rb_main_rb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="雪莉" /> <RadioButton android:id="@+id/rb_main_rb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="四号佳丽" /> <RadioButton android:id="@+id/rb_main_rb5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="五号佳丽" /> <RadioButton android:id="@+id/rb_main_rb6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="六号佳丽" /> </RadioGroup> </LinearLayout></LinearLayout>
定义一个图片的数组,把图片存放进去
在点击的时候给按钮加一个selectIndex,这个selectIndex就是数组图片所在的下标
package com.example.a07_h01;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.ImageView;import android.widget.RadioButton;import android.widget.RadioGroup;public class MainActivity extends Activity implements View.OnClickListener{ private ImageView iv_main_iv1; private RadioGroup rg_main_rg1; private RadioButton rb_main_rb1; private RadioButton rb_main_rb2; private RadioButton rb_main_rb3; private RadioButton rb_main_rb4; private RadioButton rb_main_rb5; private RadioButton rb_main_rb6; private int imagIds[]={R.drawable.ym,R.drawable.xx,R.drawable.xl, R.drawable.s4,R.drawable.s5,R.drawable.s6,}; private int selectIndex=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); iv_main_iv1.setImageResource(imagIds[selectIndex]); rb_main_rb1.setOnClickListener(this); rb_main_rb2.setOnClickListener(this); rb_main_rb3.setOnClickListener(this); rb_main_rb4.setOnClickListener(this); rb_main_rb5.setOnClickListener(this); rb_main_rb6.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.rb_main_rb1: selectIndex=0; iv_main_iv1.setImageResource(imagIds[selectIndex]); break; case R.id.rb_main_rb2: selectIndex=1; iv_main_iv1.setImageResource(imagIds[selectIndex]); break; case R.id.rb_main_rb3: selectIndex=2; iv_main_iv1.setImageResource(imagIds[selectIndex]); break; case R.id.rb_main_rb4: selectIndex=3; iv_main_iv1.setImageResource(imagIds[selectIndex]); break; case R.id.rb_main_rb5: selectIndex=4; iv_main_iv1.setImageResource(imagIds[selectIndex]); break; case R.id.rb_main_rb6: selectIndex=5; iv_main_iv1.setImageResource(imagIds[selectIndex]); break; } } private void findViews(){ iv_main_iv1=(ImageView)findViewById(R.id.iv_main_iv1); rg_main_rg1=(RadioGroup)findViewById(R.id.rg_main_rg1); rb_main_rb1=(RadioButton)findViewById(R.id.rb_main_rb1); rb_main_rb2=(RadioButton)findViewById(R.id.rb_main_rb2); rb_main_rb3=(RadioButton)findViewById(R.id.rb_main_rb3); rb_main_rb4=(RadioButton)findViewById(R.id.rb_main_rb4); rb_main_rb5=(RadioButton)findViewById(R.id.rb_main_rb5); rb_main_rb6=(RadioButton)findViewById(R.id.rb_main_rb6); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
阅读全文
0 0
- Android的RadioGroup简单使用例子
- android RadioGroup的使用
- Android RadioGroup控件的使用
- Android 多选一 RadioGroup的使用
- RadioGroup和RadioButton的简单使用
- Android-->FlowRadioGroup(流式布局RadioGroup, 自定义View的简单使用)
- Android PopupWindow RadioGroup RadioButton 的使用
- android学习笔记之RadioGroup的使用
- Android开发(20)--RadioGroup的使用
- Android—RadioGroup和RadioButton的使用
- Android开发笔记----radioGroup的使用
- android开发之RadioGroup的使用
- android中使用HttpClient的简单例子
- android framelayout的简单使用例子
- Android使用SQLite数据库的简单例子
- 简单的android Listview使用例子
- RadioGroup的简单封装
- android的RadioGroup讲解
- HashCode和equals的面试问题
- 我安装archlinux的过程总结
- Ubuntu16.04 和Windows远程桌面
- 最大子列和(算法复杂度优化)
- SQL中execute操作返回false的问题
- Android的RadioGroup简单使用例子
- Nodejs + azure +webrtc 实现android 和web视频聊天步骤
- android studio gradle:download http://jcenter 卡住解决办法
- 数字三角形问题(简单动态规划)-算法设计与分析
- spring整合struts2模型驱动访问action失败
- 1-2 安装beanstalkd与pheanstalk类
- Construct 2游戏制作教程之连线消除
- python常用库安装
- 十六:列表、元组操作