RightRadioButton.java 1.6 KB
package com.sw.laryngoscope.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.CompoundButton;

import com.sw.laryngoscope.R;

public class RightRadioButton extends androidx.appcompat.widget.AppCompatRadioButton
        implements CompoundButton.OnCheckedChangeListener {

    public RightRadioButton(Context context) {
        this(context, null);
        setOnCheckedChangeListener(this);
        setCompoundDrawables(null, null, null, null);
    }

    public RightRadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.radioButtonStyle);
        setOnCheckedChangeListener(this);
        setCompoundDrawables(null, null, null, null);
    }

    public RightRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnCheckedChangeListener(this);
        setCompoundDrawables(null, null, null, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if ( isChecked ) {
            Drawable drawable = getResources().getDrawable(R.mipmap.img_enter);
            /// 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            setCompoundDrawables(null, null, drawable, null);
        } else {
            setCompoundDrawables(null, null, null, null);
        }
    }

}