• 大小: 0.17M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-23
  • 语言: Java
  • 标签: Android  日期  时间  控件  d  

资源简介

package com.wwj.datetimepicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

/**
 * 日期时间选择控件 使用方法: private EditText inputDate;//需要设置的日期时间文本编辑框 private String
 * initDateTime="2012年9月3日 14:44",//初始日期时间值 在点击事件中使用:
 * inputDate.setOnClickListener(new OnClickListener() {
 *
 * @Override public void onClick(View v) { DateTimePickDialogUtil
 *           dateTimePicKDialog=new
 *           DateTimePickDialogUtil(SinvestigateActivity.this,initDateTime);
 *           dateTimePicKDialog.dateTimePicKDialog(inputDate);
 *
 *           } });
 *
 * @author
 */
public class DateTimePickDialogUtil implements OnDateChangedListener,
        OnTimeChangedListener {
    private DatePicker datePicker;
    private TimePicker timePicker;
    private AlertDialog ad;
    private String dateTime;
    private String initDateTime;
    private Activity activity;

    /**
     * 日期时间弹出选择框构造函数
     *
     * @param activity
     *            :调用的父activity
     * @param initDateTime
     *            初始日期时间值,作为弹出窗口的标题和日期时间初始值
     */
    public DateTimePickDialogUtil(Activity activity, String initDateTime) {
        this.activity = activity;
        this.initDateTime = initDateTime;

    }

    public void init(DatePicker datePicker, TimePicker timePicker) {
        Calendar calendar = Calendar.getInstance();
        if (!(null == initDateTime || "".equals(initDateTime))) {
            calendar = this.getCalendarByInintData(initDateTime);
        } else {
            initDateTime = calendar.get(Calendar.YEAR) "年"
                     calendar.get(Calendar.MONTH) "月"
                     calendar.get(Calendar.DAY_OF_MONTH) "日 "
                     calendar.get(Calendar.HOUR_OF_DAY) ":"
                     calendar.get(Calendar.MINUTE);
        }

        datePicker.init(calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), this);
        timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
    }

    /**
     * 弹出日期时间选择框方法
     *
     * @param inputDate
     *            :为需要设置的日期时间文本编辑框
     * @return
     */
    public AlertDialog dateTimePicKDialog(final EditText inputDate) {
        LinearLayout dateTimeLayout = (LinearLayout) activity
                .getLayoutInflater().inflate(R.layout.common_datetime, null);
        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
        timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);
        init(datePicker, timePicker);
        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);

        ad = new AlertDialog.Builder(activity)
                .setTitle(initDateTime)
                .setView(dateTimeLayout)
                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        inputDate.setText(dateTime);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        inputDate.setText("");
                    }
                }).show();

        onDateChanged(null, 0, 0, 0);
        return ad;
    }

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        onDateChanged(null, 0, 0, 0);
    }

    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // 获得日历实例
        Calendar calendar = Calendar.getInstance();

        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
                timePicker.getCurrentMinute());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

        dateTime = sdf.format(calendar.getTime());
        ad.setTitle(dateTime);
    }

    /**
     * 实现将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒,并赋值给calendar
     *
     * @param initDateTime
     *            初始日期时间值 字符串型
     * @return Calendar
     */
    private Calendar getCalendarByInintData(String initDateTime) {
        Calendar calendar = Calendar.getInstance();

        // 将初始日期时间2012年07月02日 16:45 拆分成年 月 日 时 分 秒
        String date = spliteString(initDateTime, "日", "index", "front"); // 日期
        String time = spliteString(initDateTime, "日", "index", "back"); // 时间
        Log.i("liqiang", "liqiang----0---" date);
        String yearStr = spliteString(date, "年", "index", "front"); // 年份
        Log.i("liqiang", "liqiang----1---" yearStr);
        String monthAndDay = spliteString(date, "年", "index", "back"); // 月日
        Log.i("liqiang", "liqiang----2---" monthAndDay);
        String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月
        Log.i("liqiang", "liqiang----3---" monthStr);
        String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日
        Log.i("liqiang", "liqiang----4---" dayStr);
        String hourStr = spliteString(time, ":", "index", "front"); // 时
        String minuteStr = spliteString(time, ":", "index", "back"); // 分

        int currentYear = Integer.valueOf(yearStr.trim()).intValue();
        int currentMonth = Integer.valueOf(monthStr.trim()).intValue() - 1;
        int currentDay = Integer.valueOf(dayStr.trim()).intValue();
        int currentHour = Integer.valueOf(hourStr.trim()).intValue();
        int currentMinute = Integer.valueOf(minuteStr.trim()).intValue();

        calendar.set(currentYear, currentMonth, currentDay, currentHour,
                currentMinute);
        return calendar;
    }

    /**
     * 截取子串
     *
     * @param srcStr
     *            源串
     * @param pattern
     *            匹配模式
     * @param indexOrLast
     * @param frontOrBack
     * @return
     */
    public static String spliteString(String srcStr, String pattern,
            String indexOrLast, String frontOrBack) {
        String result = "";
        int loc = -1;
        if (indexOrLast.equalsIgnoreCase("index")) {
            loc = srcStr.indexOf(pattern); // 取得字符串第一次出现的位置
        } else {
            loc = srcStr.lastIndexOf(pattern); // 最后一个匹配串的位置
        }
        if (frontOrBack.equalsIgnoreCase("front")) {
            if (loc != -1)
                result = srcStr.substring(0, loc); // 截取子串
        } else {
            if (loc != -1)
                result = srcStr.substring(loc 1, srcStr.length()); // 截取子串
        }
        return result;
    }

}


资源截图

代码片段和文件信息

/** Automatically generated file. DO NOT MODIFY */
package com.wwj.datetimepicker;

public final class BuildConfig {
    public final static boolean DEBUG = true;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        475  2016-01-07 16:31  DateTimePicker\.classpath

     文件        850  2013-03-12 18:29  DateTimePicker\.project

     文件         55  2014-08-23 15:13  DateTimePicker\.settings\org.eclipse.core.resources.prefs

     文件        598  2013-03-12 18:58  DateTimePicker\.settings\org.eclipse.jdt.core.prefs

     文件       1107  2016-01-08 11:57  DateTimePicker\AndroidManifest.xml

     文件       1107  2016-01-22 09:32  DateTimePicker\bin\AndroidManifest.xml

     文件        353  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\BuildConfig.class

     文件       1303  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1$1.class

     文件       1258  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1$2.class

     文件       1099  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1$onclickListenerImpl.class

     文件       4023  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeDemo1.class

     文件       1259  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickDialogUtil$1.class

     文件       1169  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickDialogUtil$2.class

     文件       6772  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickDialogUtil.class

     文件       1338  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickerActivity$1.class

     文件       1338  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickerActivity$2.class

     文件       1770  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimePickerActivity.class

     文件       1017  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$1.class

     文件        889  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$2.class

     文件        924  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$3$1.class

     文件       1279  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$3.class

     文件        900  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$4$1.class

     文件       1271  2016-01-22 09:32  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe$4.class

     文件       2246  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\DateTimeToMe.class

     文件        352  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\R$attr.class

     文件        419  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\R$drawable.class

     文件        741  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\R$id.class

     文件        511  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\R$layout.class

     文件        482  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\R$string.class

     文件        559  2016-01-26 10:48  DateTimePicker\bin\classes\com\wwj\datetimepicker\R.class

............此处省略58个文件信息

评论

共有 条评论