radiobutton设置样式


单选题里会用到radiobutton,如果不想使用系统提供的圆圈样式,可以自定义样式,想要做成的效果就是,

使用自定义的图片替换圆圈,然后选择有4个选项的其中一个,图片上有个对勾标记,

然后如果正确选项是帅哥,则选择A后,图片替换成

所以首先在xml属性里,将自定义的图片替换圆圈:关键就是

 android:button="@drawable/c" 这一句:
  <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="丑男"
            android:button="@drawable/c" />

然后在代码中设置监听事件,当选中了正确答案,

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                 result = (RadioButton)findViewById(checkedId);
                 if (result.getText().toString().equals("帅哥"))
                     {
                     result.setButtonDrawable(R.drawable.b1);//选对后替换打钩的图片
                     
                     resultDetail.setVisibility(View.VISIBLE); 
                     resultDetail.setText("恭喜你,答对了!");
                     
                 }else {
                     resultDetail.setVisibility(View.VISIBLE); 
                     resultDetail.setText("很遗憾,答错了!");
                 }  
              
            }
        });

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM