2020-09-14

关于自定义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);
        }
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个 SQL 语句,用于向借阅表中插入数据。该表包含以下字段:借阅编号、读者编号、书籍编号、借阅日期、归还日期、借阅状态。每条数据表示一次借阅记录。其中借阅编号、读者编号、书籍编号、借阅日期和借阅状态是必填项,归还日期为可选项,如果借阅状态为“已还”则必须填写归还日期。 具体插入的数据如下: - 借阅编号:100001,读者编号:123413,书籍编号:0001,借阅日期:2020-11-05,归还日期:NULL,借阅状态:借阅 - 借阅编号:100002,读者编号:223411,书籍编号:0002,借阅日期:2020-9-28,归还日期:2020-10-13,借阅状态:已还 - 借阅编号:100003,读者编号:321123,书籍编号:1001,借阅日期:2020-7-01,归还日期:NULL,借阅状态:过期 - 借阅编号:100004,读者编号:321124,书籍编号:2001,借阅日期:2020-10-09,归还日期:2020-10-14,借阅状态:已还 - 借阅编号:100005,读者编号:321124,书籍编号:0001,借阅日期:2020-10-15,归还日期:NULL,借阅状态:借阅 - 借阅编号:100006,读者编号:223411,书籍编号:2001,借阅日期:2020-10-16,归还日期:NULL,借阅状态:借阅 - 借阅编号:100007,读者编号:411111,书籍编号:1002,借阅日期:2020-9-01,归还日期:2020-9-24,借阅状态:已还 - 借阅编号:100008,读者编号:411111,书籍编号:0001,借阅日期:2020-9-25,归还日期:NULL,借阅状态:借阅 - 借阅编号:100009,读者编号:411111,书籍编号:1001,借阅日期:2020-10-08,归还日期:NULL,借阅状态:借阅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值