单选框RadioGroup,单选按钮RadioButton的使用

来源:互联网 发布:java开发实例1200 pdf 编辑:程序博客网 时间:2024/05/14 09:40

RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法。

RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个Android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中。

使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响应按钮的事件;


下面是一个实例:





XML代码:

[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <ImageView  
  7.         android:layout_width="300dp"  
  8.         android:layout_height="300dp"  
  9.         android:id="@+id/iv_main_image"/>  
  10.   
  11.   
  12.     <RelativeLayout  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.        >  
  16.   
  17.     <RadioGroup  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:orientation="horizontal"  
  21.         android:id="@+id/rg_main_group">  
  22.   
  23.         <RadioButton  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:text="1号佳丽"  
  27.             android:ems="3"  
  28.             android:id="@+id/rb_main_one"/>  
  29.         <RadioButton  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:text="2号佳丽"  
  33.             android:ems="3"  
  34.             android:id="@+id/rb_main_two"/>  
  35.         <RadioButton  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:text="3号佳丽"  
  39.             android:ems="3"  
  40.             android:id="@+id/rb_main_three"/>  
  41.         <RadioButton  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:text="4号佳丽"  
  45.             android:ems="3"  
  46.             android:id="@+id/rb_main_four"/>  
  47.         <RadioButton  
  48.             android:layout_width="wrap_content"  
  49.             android:layout_height="wrap_content"  
  50.             android:text="5号佳丽"  
  51.             android:ems="3"  
  52.             android:id="@+id/rb_main_five"/>  
  53.   
  54.     </RadioGroup>  
  55.     </RelativeLayout>  
  56.   
  57.   
  58. </LinearLayout>  

这里RadioButton的Android:checked属性可以多个都选择为true,但运行之后只会选择最后一个checked属性作为初始状态。

很多初学者都会遇到一个问题:程序代码(包括xml文件)均无错误提示,但是在设备上运行时候却出错,其中一个原因就是布局或者组件没有指定layout_width和layout_height属性,导致运行出错!


Java代码

[html] view plain copy
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.   
  4.   
  5.     private ImageView iv_main_image;  
  6.     private RadioGroup rg_main_group;  
  7.     int currentIndex=0;  
  8.     File files[];  
  9.     private Map<String, Bitmap> m;  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         //根据ID找到该图片控件  
  16.         iv_main_image = (ImageView) findViewById(R.id.iv_main_image);  
  17.         m = new HashMap<>();  
  18.         //手机是否有内存卡  
  19.         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
  20.             //获取路径  
  21.             String sdCardPath=Environment.getExternalStorageDirectory().getAbsolutePath();  
  22.             File file=new File(sdCardPath+"/Images");  
  23.             filesfile.listFiles();  
  24.             Toast.makeText(this,""+sdCardPath,Toast.LENGTH_LONG).show();  
  25.            Bitmap bm=BitmapFactory.decodeFile(files[0].getAbsolutePath());  
  26.            iv_main_image.setImageBitmap(bm);  
  27.             RadioButton rb_main_one= (RadioButton) findViewById(R.id.rb_main_one);  
  28.             rb_main_one.setChecked(true);  
  29.         }  
  30.         int i=1;  
  31.         for (File file : files) {  
  32.             m.put(i+"号佳丽",BitmapFactory.decodeFile(file.getAbsolutePath()));  
  33.             i++;  
  34.         }  
  35.         //根据ID找到RadioGroup实例  
  36.         rg_main_group = (RadioGroup) this.findViewById(R.id.rg_main_group);  
  37.         //绑定一个匿名监听器  
  38.         rg_main_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  39.             @Override  
  40.             public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {  
  41.                 RadioButton rb= (RadioButton) findViewById(checkedId);  
  42.                 String s=rb.getText().toString();  
  43.                 iv_main_image.setImageBitmap(m.get(s));  
  44.             }  
  45.         });  
  46.   
  47.   
  48.   
  49.     }  

原创粉丝点击