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));
- }
- });
- }
- }