单选框RadioGroup,单选按钮RadioButton的使用
来源:互联网 发布:java开发实例1200 pdf 编辑:程序博客网 时间:2024/05/14 09:40
RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法。
RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个Android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中。
使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响应按钮的事件;
下面是一个实例:
XML代码:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:layout_width="300dp"
- android:layout_height="300dp"
- android:id="@+id/iv_main_image"/>
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- >
- <RadioGroup
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:id="@+id/rg_main_group">
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="1号佳丽"
- android:ems="3"
- android:id="@+id/rb_main_one"/>
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="2号佳丽"
- android:ems="3"
- android:id="@+id/rb_main_two"/>
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="3号佳丽"
- android:ems="3"
- android:id="@+id/rb_main_three"/>
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="4号佳丽"
- android:ems="3"
- android:id="@+id/rb_main_four"/>
- <RadioButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="5号佳丽"
- android:ems="3"
- android:id="@+id/rb_main_five"/>
- </RadioGroup>
- </RelativeLayout>
- </LinearLayout>
这里RadioButton的Android:checked属性可以多个都选择为true,但运行之后只会选择最后一个checked属性作为初始状态。
很多初学者都会遇到一个问题:程序代码(包括xml文件)均无错误提示,但是在设备上运行时候却出错,其中一个原因就是布局或者组件没有指定layout_width和layout_height属性,导致运行出错!
Java代码
- public class MainActivity extends AppCompatActivity {
- private ImageView iv_main_image;
- private RadioGroup rg_main_group;
- int currentIndex=0;
- File files[];
- private Map<String, Bitmap> m;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //根据ID找到该图片控件
- iv_main_image = (ImageView) findViewById(R.id.iv_main_image);
- m = new HashMap<>();
- //手机是否有内存卡
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- //获取路径
- String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();
- File file=new File(sdCardPath+"/Images");
- files= file.listFiles();
- Toast.makeText(this,""+sdCardPath,Toast.LENGTH_LONG).show();
- Bitmap bm=BitmapFactory.decodeFile(files[0].getAbsolutePath());
- iv_main_image.setImageBitmap(bm);
- RadioButton rb_main_one= (RadioButton) findViewById(R.id.rb_main_one);
- rb_main_one.setChecked(true);
- }
- int i=1;
- for (File file : files) {
- m.put(i+"号佳丽",BitmapFactory.decodeFile(file.getAbsolutePath()));
- i++;
- }
- //根据ID找到RadioGroup实例
- rg_main_group = (RadioGroup) this.findViewById(R.id.rg_main_group);
- //绑定一个匿名监听器
- rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
- RadioButton rb= (RadioButton) findViewById(checkedId);
- String s=rb.getText().toString();
- iv_main_image.setImageBitmap(m.get(s));
- }
- });
- }
- }
阅读全文
0 0
- 单选框RadioGroup,单选按钮RadioButton的使用
- 单选框RadioGroup,单选按钮RadioButton的使用
- 单选按钮RadioButton&RadioGroup
- 安卓编程——单选框RadioGroup,单选按钮RadioButton的使用
- 单选按钮RadioGroup与RadioButton实例
- android 单选按钮 RadioGroup RadioButton
- RadioButton单选按钮实现RadioGroup.OnCheckedChangeListener
- 单选按钮RadioGroup和RadioButton
- 单选按钮RadioGroup的使用
- 使用RadioButton和RadioGroup实现多种多样的单选情况
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- Android 单选按钮RadioButton的使用
- 单选按钮 RadioButton 的使用
- Android 单选按钮RadioButton的使用
- 山寨开机选择四大系统-----------RadioGroup 、RadioButton单选按钮
- 安卓控件之单选按钮 (RadioButton和RadioGroup)
- 单选按钮RadioButton使用
- 安卓RadioButton,RadioGroup,Button按钮的使用
- java——类——final
- 权限列表
- 安卓高级控件3之RecyclerView的使用
- caffe-windows 训练mnist数据集
- 安卓之高级控件Listview的优化
- 单选框RadioGroup,单选按钮RadioButton的使用
- iOS 内购详解
- 多线程(4)控制线程
- 服务器维护过程中踩过的坑儿
- POJ分类很好很有层次感
- 卓常用控件3之图片框(ImageView)
- Android—activity
- 哈希,编码, 加密的区别!
- spring事务整理一