栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android自定义DataTimePicker实例代码(日期选择器)

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap

Android自定义DataTimePicker实例代码(日期选择器)

笔者有一段时间没有发表关于Android的文章了,关于Android自定义组件笔者有好几篇想跟大家分享的,后期会记录在博客中。本篇博客给大家分享的是自定义一个日期选择器,可以让用户同时选择年月日和当前时间。

先看看效果:

实现的效果就是在同一个布局上显示日期选择和时间选择。

自定义一个类:

/DateTimePicker/src/com/wwj/datetimepicker/DateTimePickDialogUtil.java

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.widget.DatePicker; 
import android.widget.DatePicker.OnDateChangedListener; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TimePicker; 
import android.widget.TimePicker.OnTimeChangedListener; 
 
 
public class DateTimePickDialogUtil implements OnDateChangedListener, 
    onTimeChangedListener { 
  private DatePicker datePicker; 
  private TimePicker timePicker; 
  private alertDialog ad; 
  private String dateTime; 
  private String initDateTime; 
  private Activity activity; 
 
   
  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)); 
  } 
 
   
  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); 
  } 
 
   
  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"); // 时间 
 
    String yearStr = spliteString(date, "年", "index", "front"); // 年份 
    String monthAndDay = spliteString(date, "年", "index", "back"); // 月日 
 
    String monthStr = spliteString(monthAndDay, "月", "index", "front"); // 月 
    String dayStr = spliteString(monthAndDay, "月", "index", "back"); // 日 
 
    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; 
  } 
 
   
  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; 
  } 
 
} 

/DateTimePicker/src/com/wwj/datetimepicker/DateTimePickerActivity.java

package com.wwj.datetimepicker; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
 
 
public class DateTimePickerActivity extends Activity { 
   
  private EditText startDateTime; 
  private EditText endDateTime; 
 
  private String initStartDateTime = "2013年9月3日 14:44"; // 初始化开始时间 
  private String initEndDateTime = "2014年8月23日 17:44"; // 初始化结束时间 
 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
 
    // 两个输入框 
    startDateTime = (EditText) findViewById(R.id.inputDate); 
    endDateTime = (EditText) findViewById(R.id.inputDate2); 
 
    startDateTime.setText(initStartDateTime); 
    endDateTime.setText(initEndDateTime); 
 
    startDateTime.setonClickListener(new onClickListener() { 
      public void onClick(View v) { 
 
 DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil( 
     DateTimePickerActivity.this, initEndDateTime); 
 dateTimePicKDialog.dateTimePicKDialog(startDateTime); 
 
      } 
    }); 
 
    endDateTime.setonClickListener(new onClickListener() { 
 
      public void onClick(View v) { 
 DateTimePickDialogUtil dateTimePicKDialog = new DateTimePickDialogUtil( 
     DateTimePickerActivity.this, initEndDateTime); 
 dateTimePicKDialog.dateTimePicKDialog(endDateTime); 
      } 
    }); 
  } 
} 

自定义界面:

/DateTimePicker/res/layout/common_datetime.xml

 
 
 
   
 
   
 
 

源码下载:demo

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/160138.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号