当前位置:   article > 正文

android 重写RadioButton

android 重写radio

转载是一种美德 http://hi.baidu.com/545057627/blog/item/bd154f0bfef4e5181c9583d6.html

package com.cloud.test;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.RadioButton;

public class MyRadioButton extends RadioButton{

public MyRadioButton(Context context) {
super(context);
}

public MyRadioButton(Context context,AttributeSet attrs) {
super(context,attrs);
}


@Override
public boolean isChecked() {
return super.isChecked();
}

@Override
public void setButtonDrawable(int resid) {
super.setButtonDrawable(resid);
}

@Override
protected void onDraw(Canvas canvas) {
if(isChecked()){
super.setButtonDrawable(R.drawable.circle_green); //为选中RadioButton的图片
}else{
super.setButtonDrawable(R.drawable.circle_grey); //未选中RadioButton的图片
}
super.onDraw(canvas);
}

}

package com.cloud.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

public class RadioButtonTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
RadioGroup rg=new RadioGroup(this);
MyRadioButton mBtn=new MyRadioButton(getApplicationContext());
mBtn.setWidth(100);
mBtn.setText("RadioBtnOne");
mBtn.setHeight(60);

MyRadioButton mBtn2=new MyRadioButton(getApplicationContext());
mBtn2.setWidth(100);
mBtn2.setText("RadioBtnTwo");

mBtn2.setHeight(60);

rg.addView(mBtn);
rg.addView(mBtn2);
layout.addView(rg);

}
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/733757
推荐阅读
相关标签
  

闽ICP备14008679号