需求描述
在一個RadioGroup中,在切換選中項之前,需要用戶確認是否切換,用戶確認之后,切換之后再進行切換。
問題分析
在常規(guī)的RadioGroup中,當一個RadioButton被點擊時是會立即被選中的,無法先攔截點到點擊事件。
解決辦法
這里我自定義一個ManualRadioGroup,繼承RadioGroup,修改它的事件分發(fā),以達到攔截點擊事件,手動控制選中切換的目的。
關鍵點:
1.手指落在RadioButton上攔截事件,自定義處理,手指落在其他位置,由父類處理
2.手指抬起時,判斷手指是否移動過,沒有移動過則認為點擊了RadioButton
直接上代碼:
/**
* 攔截了RadioButton點擊事件的RadioGroup
* 如果要選中RadioButton,請使用{@link #setOnChildRadioButtonClickedListener(OnChildRadioButtonClickedListener)},手動選中
*/
public class ManualRadioGroup extends RadioGroup {
/**
* 手指落下的位置所在的子view
*/
View v = null;
/**
* 同一個事件序列中,經(jīng)歷過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:
moved = false;
//消費事件
return true;
case MotionEvent.ACTION_UP:
if (!moved) {
if (listener != null) {
if (getCheckedRadioButtonId() == -1) {
listener.onRadioButtonClickedWhenCheckedNone((RadioButton) v);
} else if (getCheckedRadioButtonId() == v.getId()) {
listener.onRadioButtonCheckedClicked((RadioButton) v);
} else {
listener.onRadioButtonDifferentFromCheckedClicked(
(RadioButton) v,
(RadioButton) findViewById(getCheckedRadioButtonId()));
}
}
//沒有移動過,消費事件
return true;
} else {
//移動過,交給父類處理
return super.onTouchEvent(event);
}
case MotionEvent.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 {
/**
* 沒有按鈕被選中時,被點擊了
*
* @param button 被點擊的按鈕
*/
void onRadioButtonClickedWhenCheckedNone(RadioButton button);
/**
* 已選中的RadioButton被點擊了
*
* @param button 被點擊的按鈕
*/
void onRadioButtonCheckedClicked(RadioButton button);
/**
* 非選中的RadioButton被點擊了
*
* @param clickedRadioButton 被點擊的按鈕
* @param checkedRadioButton 已經(jīng)選中的按鈕
*/
void onRadioButtonDifferentFromCheckedClicked(RadioButton clickedRadioButton, RadioButton checkedRadioButton);
}
public OnChildRadioButtonClickedListener getOnChildRadioButtonClickedListener() {
return listener;
}
public void setOnChildRadioButtonClickedListener(OnChildRadioButtonClickedListener listener) {
this.listener = listener;
}
}
ManualRadioGroup的使用
ManualRadioGroup和正常的RadioGroup一樣使用,只是RadioButton不會在被點擊的時候自動切換了,而是需要手動的去切換,請看下方的示例代碼
ManualRadioGroup rg = findViewById(R.id.rg);
rg.setOnChildRadioButtonClickedListener(new ManualRadioGroup.OnChildRadioButtonClickedListener() {
@Override
public void onRadioButtonClickedWhenCheckedNone(RadioButton button) {
button.setChecked(true);
Toast.makeText(MainActivity.this, "第一次選中", Toast.LENGTH_SHORT).show();
}
@Override
public void onRadioButtonCheckedClicked(RadioButton button) {
Toast.makeText(MainActivity.this, "重復選中", Toast.LENGTH_SHORT).show();
}
@Override
public void onRadioButtonDifferentFromCheckedClicked(final RadioButton clickedRadioButton, final RadioButton checkedRadioButton) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("重選提示")
.setMessage("確定要改變選中嗎?")
.setCancelable(false)
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
clickedRadioButton.setChecked(true);
dialog.dismiss();
}
})
.setNeutralButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create()
.show();
}
});