欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义RadioGroup

程序员文章站 2022-03-24 23:13:22
关于自定义RadioGroup:作用:可以在设置选中状态前执行自定义相关的方法,类似拦截选中状态另做处理完整的代码类import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.RadioButton;imp....
关于自定义RadioGroup: 作用:可以在设置选中状态前执行自定义相关的方法,类似拦截选中状态另做处理 完整的代码类 import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.qinggan.app.vehicle.utils.LogActs;

public class ManualRadioGroup extends RadioGroup {
    /**
     * 手指落下的位置所在的子view
     */
    View v = null;
    /**
     * 同一个事件序列中,经历过ACTION_MOVE则为true
     */
    private boolean moved;

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            v = findViewByXY(ev.getX(), ev.getY());
            if (v != null && v instanceof RadioButton) {
                /**如果手指落下的位置刚好在一个RadioButton上,就直接丢到自己的{@link #onTouchEvent(MotionEvent)}方法处理*/
                return true;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                LogActs.d("onInterceptTouchEvent ACTION_DOWN");

                return true;
            case MotionEvent.ACTION_UP:
                LogActs.d("onInterceptTouchEvent ACTION_UP");
                if (listener != null) {
                    LogActs.d("onInterceptTouchEvent getCheckedRadioButtonId=" + getCheckedRadioButtonId() + " v.getId=" + v.getId());
                    if (getCheckedRadioButtonId() == v.getId()) {
                        LogActs.d("same button isChecked");
                        listener.onSameRadioButtonClick((RadioButton) v);
                    } else {
                        LogActs.d("another button isChecked");
                        listener.onAnotherRadioButtonClick(
                                (RadioButton) v,
                                (RadioButton) findViewById(getCheckedRadioButtonId()));
                    }
                }
                //没有移动过,消费事件
                return true;

            case MotionEvent.ACTION_MOVE:
                LogActs.d("onInterceptTouchEvent ACTION_MOVE");
                moved = true;
                //移动过,交给父类处理
                return super.onTouchEvent(event);
        }
        //其他事件,交给父类处理
        return super.onTouchEvent(event);
    }

    private View findViewByXY(float x, float y) {
        View v = null;
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            Rect rect = new Rect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
            if (!rect.contains((int) x, (int) y)) {
                continue;
            }
            v = child;
            break;
        }

        return v;
    }

    private OnChildRadioButtonClickedListener listener;

    public interface OnChildRadioButtonClickedListener {

        /**
         * 已选中的RadioButton被点击了
         *
         * @param button 被点击的按钮
         */
        void onSameRadioButtonClick(RadioButton button);

        /**
         * 非选中的RadioButton被点击了
         *
         * @param clickedRadioButton 被点击的按钮
         * @param checkedRadioButton 已经选中的按钮
         */
        void onAnotherRadioButtonClick(RadioButton clickedRadioButton, RadioButton checkedRadioButton);
    }

    public OnChildRadioButtonClickedListener getOnChildRadioButtonClickedListener() {
        return listener;
    }

    public void setOnChildRadioButtonClickedListener(OnChildRadioButtonClickedListener listener) {
        this.listener = listener;
    }
}

----------------------------------用法--------------------------

rg_ontime_charge.setOnChildRadioButtonClickedListener(new ManualRadioGroup.OnChildRadioButtonClickedListener() {
    @Override
    public void onSameRadioButtonClick(RadioButton button) {
        LogActs.d(" onRadioButtonCheckedClicked");
    }

    @Override
    public void onAnotherRadioButtonClick(RadioButton clickedRadioButton, RadioButton checkedRadioButton) {
        LogActs.d(" onRadioButtonDifferentFromCheckedClicked");
        if (isCharging) {
            clickedRadioButton.setChecked(false);
            QGToast.makeText(getContext(), "正在充电中,不可预约定时充电", Toast.LENGTH_LONG).show();
        }else{
            clickedRadioButton.setChecked(true);
        }
    }
});

本文地址:https://blog.csdn.net/qq_26522993/article/details/108576339