android DatePickerDialog配合edittext实现按日期查询

我们从网上一搜DatePickerDialog相关实现,大多都是默认的形式,也就是不带取消按钮。下边上我的代码:我将代码简单的封装到一个工具类里边

public static DatePickerDialog showDateDialog(Context ctx, final EditText editText)//editText是我们要显示选择日期值
    {
        
       Calendar c = Calendar.getInstance();//获取当前日期
       int year = c.get(Calendar.YEAR);
       int month = c.get(Calendar.MONTH);
       int day = c.get(Calendar.DAY_OF_MONTH);
        final DatePickerDialog datePickerDialog = new DatePickerDialog(ctx,null,year,month,day);

        datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "完成", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                DatePicker datePicker = datePickerDialog.getDatePicker();

                int year = datePicker.getYear();

                int month = datePicker.getMonth();

                int day = datePicker.getDayOfMonth();

                StringBuilder monthBuilder = new StringBuilder();
                if (month + 1 < 10) {
                    monthBuilder.append("0");
                }
                monthBuilder.append(month + 1);

                StringBuilder dayBuilder = new StringBuilder();
                if (day < 10) {
                    dayBuilder.append("0");
                }
                dayBuilder.append(day);

                editText.setText(year + "-" + monthBuilder.toString() + "-" + dayBuilder.toString());//点击完成将处理好的日期值写到edittext上
            }
        });
        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                datePickerDialog.cancel();
            }
        });
        datePickerDialog.getDatePicker().setCalendarViewShown(false);
        return datePickerDialog;
    }

我们通过setButton在dialog中加入取消按钮,并作简单的实现。

在我们选择完日期后,当我们再次打开toucher的时候,应该在日期控件中显示为选择后的时间:看代码

public static DatePickerDialog showDateDialog(Context ctx, final EditText editText)
    {
        int year = 0;
        int month = 0;
        int day = 0;

     //首次打开
if(editText.getText().toString().length()==0) { Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); }else{//有值的情况下 String currDate = editText.getText().toString(); year = Integer.valueOf(currDate.split("-")[0].toString()); month = Integer.valueOf(currDate.split("-")[1].toString())-1; day = Integer.valueOf(currDate.split("-")[2].toString()); } final DatePickerDialog datePickerDialog = new DatePickerDialog(ctx,null,year,month,day); datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "完成", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { DatePicker datePicker = datePickerDialog.getDatePicker(); int year = datePicker.getYear(); int month = datePicker.getMonth(); int day = datePicker.getDayOfMonth(); StringBuilder monthBuilder = new StringBuilder(); if (month + 1 < 10) { monthBuilder.append("0"); } monthBuilder.append(month + 1); StringBuilder dayBuilder = new StringBuilder(); if (day < 10) { dayBuilder.append("0"); } dayBuilder.append(day); editText.setText(year + "-" + monthBuilder.toString() + "-" + dayBuilder.toString()); } }); datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { datePickerDialog.cancel(); } }); datePickerDialog.getDatePicker().setCalendarViewShown(false); return datePickerDialog; }

调用方式示例:

 conditionEdit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(dialog!=null && dialog.isShowing()){
                    return true;
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN)
                {
                    conditionEdit.clearFocus();
                    conditionEdit.requestFocus();
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_UP)
                {
                    dialog  = Dialog.showDateDialog(ReservationActivity.this, conditionEdit);
                    dialog.show();
                }
                return false;
            }
        });

 

posted @ 2015-05-15 11:20  fullSunlight  阅读(679)  评论(0编辑  收藏  举报