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("很遗憾,答错了!");
                 }  
              
            }
        });

 

posted @ 2014-07-02 10:56  伟大的臭猪猪  阅读(7478)  评论(1编辑  收藏  举报