需求
每次点击radiobutton前需要弹框提示用户,点确定则切换radiobutton,点取消什么也不做。
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MyRadioGroup extends RadioGroup {
private final String TAG = "laocuo";
private int checkId;
private AlertDialog dialog;
public MyRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
dialog = new AlertDialog.Builder(getContext())
.setTitle("title")
.setMessage("message")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface anInterface, int i) {
Log.d(TAG, "setPositiveButton");
check(checkId);
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface anInterface, int i) {
Log.d(TAG, "setNegativeButton");
}
})
.create();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int count = getChildCount();
for (int i=0;i<count;i++) {
RadioButton child = (RadioButton) getChildAt(i);
int x = (int) ev.getX();
int y = (int) ev.getY();
int left = child.getLeft();
int right = child.getRight();
int top = child.getTop();
int bottom = child.getBottom();
Log.d(TAG, "x="+x+"|x="+x);
Log.d(TAG, "left="+left+"|right="+right);
Log.d(TAG, "top="+top+"|bottom="+bottom);
if (x < right && x > left && y > top && y < bottom) {
checkId = child.getId();
dialog.setMessage("确定要跳转到"+child.getText()+"?");
break;
}
}
if (getCheckedRadioButtonId() != checkId) {
dialog.show();
}
return true;
}
}