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


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值