重写DatePickerDialog 解决OnDateSetListener只有完成,没有取消回调问题

来源:互联网 发布:linux上装eclipse 编辑:程序博客网 时间:2024/06/07 09:33

今天写项目,发现这个DatePickerDialog给他设置了OnDateSetListener,但是我没有点击设置,它却回调了,判断不出我是否点击了设置,查看了网上一些方法:

方法一:自定义view的方法:http://www.2cto.com/kf/201501/367678.html

方法二:setButton方法:http://www.it165.net/pro/html/201503/36757.html

浏览了一下这两个方法,觉得方法一太麻烦,方法二的mDialog.getDatePicker();只有3.1以上才可以使用,两个方法都不好,后来继续浏览其它网页,发现一个比较牛逼的代码,深受启发。

http://blog.csdn.net/OnlyOneCoder/article/details/25481505

((ViewGroup) ((ViewGroup) this.getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); 

这方法是根据picker的view布局设置隐藏天的,那么我也可以试试找出picker的完成按钮对它设置回调,而不用自带那个!

首先,我需要一个工具找到picker的button的view或它的id,然后设置 setOnClickListener

不知道网上有没有,反正不需要多复杂,自己写一个:

public class ViewUtil {public static JSONObject getViewLayoutJson (View view) {if (view == null) {return new JSONObject();} else {JSONObject json = new JSONObject();json.put("classname", view.getClass().getName());json.put("width", view.getMeasuredWidth());json.put("height", view.getMeasuredHeight());json.put("top", view.getTop());json.put("visibility", view.getVisibility());json.put("id", view.getId());if (view instanceof ViewGroup) {JSONArray jsonarr = new JSONArray();ViewGroup vg = (ViewGroup)view;for (int i = 0; i < vg.getChildCount(); i++) {jsonarr.add(getViewLayoutJson(vg.getChildAt(i)));}json.put("subview", jsonarr);} else if (view instanceof TextView) {TextView tv = (TextView)view;json.put("text", tv.getText());}return json;}}}

获取dialog布局的json数据:

JSONObject json = ViewUtil.getViewLayoutJson(dataDialog.getWindow().getDecorView());

String layout = json.toJSONString();

其中,寻找设置按钮,在英文系统中就是 “Done”,找到结果如下:

{"classname" : "android.widget.Button","height" : 0,"id" : 16908313,"text" : "Done","top" : 0,"visibility" : 0,"width" : 0}

完成按钮对应的id是16908313,进入android.R.id类,找到这个id,

  // Field descriptor #8 I  public static final int button1 = 16908313;
这个id对应的名字是button1


知道这个id后,我们开始设计自定义dialog:

pimport android.app.DatePickerDialog;import android.content.Context;import android.view.View;import android.widget.DatePicker;public class AppPickerDialog extends DatePickerDialog {private int chooiceYear, chooiceMonth, chooiceDay;private DatePicker picker;private Context mContent;private OnDateSetListener mCallBack;public AppPickerDialog(final Context context,final OnDateSetListener callBack, int year, int monthOfYear,int dayOfMonth) {super(context, callBack, year, monthOfYear, dayOfMonth);mContent = context;}@Overridepublic void onDateChanged(DatePicker view, int year, int month, int day) {super.onDateChanged(view, year, month, day);picker = view;chooiceYear = year;chooiceMonth = month;chooiceDay = day;}/** * 完成事件 * @param callBack */public void setOnPositiveListener(OnDateSetListener callBack) {mCallBack = callBack;}@Overridepublic void show() {super.show();this.getWindow().getDecorView().findViewById(android.R.id.button1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {dismiss();if (mCallBack!=null) {mCallBack.onDateSet(picker, chooiceYear, chooiceMonth,chooiceDay);}}});}}

new的时候,不要传onDateSetListener事件(这个系统分法的,不好用),要另外set一个自己写的方法

最后使用方法:

dataDialog = new AppPickerDialog(this, null,year0, // 传入年份month0, // 传入月份day0// 传入天数);dataDialog.setOnPositiveListener(new OnDateSetListener() {@Overridepublic void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {calendar.setTimeInMillis(System.currentTimeMillis());calendar.set(year, month, dayOfMonth, 0, 0, 0);if (calendar.getTimeInMillis() > System.currentTimeMillis()) { ToastUtil.showMessage(getActivity(), "请输入正确的时间!");return;}year0 = year;month0 = month;day0 = dayOfMonth;mq.id(R.id.birthday).text(year + "-" + (month+1) + "-" + dayOfMonth);}});dataDialog.show();

大功告成,解决的简单粗暴。












((











0 0
原创粉丝点击