Android自定义RadioGroup
- 作者: 香气CG
- 来源: 51数据库
- 2021-09-21
关于自定义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); } } });
本文地址:http://www.51sjk.com/Upload/Articles/1/0/251/251877_20210626002006813.jpg
推荐阅读
热点文章
android中Bitmap用法(显示,保存,缩放,旋转)实例分析
12
android 仿微信聊天气泡效果实现思路
1
Android的尺度,drawable-xxxxxxx
2
Codeforces Round #656 (Div. 3) (C、D题)
1
Android之handler异步消息处理机制解析
6
GridView中图片显示出现上下间距过大,左右图片显示类似瀑布流的问题
0
AsyncTask的简单使用
5
两个简单Fragment之间的通信(三种方式)
18
uboot修改设置boot参数命令
41
android中实现从相册中一次性获取多张图片与拍照,并将选中的图片显示出来
2