當前位置:首頁 > IT技術

時間選擇器之 DateTimePickerDialog

時間:2019-10-03 17:13:16來源:IT技術作者:seo實驗室小編閱讀:88次「手機版」
 

東學西讀

效果圖

package com.example.liuqidemo.view;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.simpledateformat;

import java.util.calendar;

import java.util.Date;

import com.example.vehiclemandcsyh.R;

import Android.content.context;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.TimePicker;

public class DateTimePickerDialog {

private DatePicker dp;

private TimePicker tp;

private Context context;

private MyDialog dateDialog;

private TextView picktitle;

private String dateString="";

private EditText editText;

private String formStr="";// 輸出格式

private int type;// 操作類型

private int count=1;

public DateTimePickerDialog(Context context){

this.context=context;

}

/**

* 設置日期和時間(無初始化 日期時間參數),typ=1:時間選擇;type=2:日期選擇;type=3:日期時間選擇

* @param editText

*/

public void getDateTimePicker(EditText editText,int type){

this.type=type;

int year, month, day;

this.editText=editText;

Calendar calendar=Calendar.getinstance();

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH);

day=calendar.get(Calendar.DAY_OF_MONTH);

dateDialog= new MyDialog(context, R.layout.zsg_datetime_pick);

picktitle=(TextView)dateDialog.findViewById(R.id.tv_datetime_pick_title);

dp=(DatePicker)dateDialog.findViewById(R.id.datepicker);

tp=(TimePicker)dateDialog.findViewById(R.id.timepicker);

switch (this.type) {

case 1:

tp.setVisibility(View.VISIBLE);

dp.setVisibility(View.GONE);

picktitle.setText("請選擇時間");

formStr="HH:mm";

break;

case 2:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd";

picktitle.setText("請選擇日期");

break;

default:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd HH:mm";

picktitle.setText("請選擇日期");

break;

}

dp.setCalendarViewShown(false);

dp.init(year, month, day, new DatePicker.OnDateChangedListener() {

public void onDateChanged(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

}

});

int a=tp.getCurrentHour()+0;

tp.setIs24HourView(true);

tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

}

});

tp.setCurrentHour(a);

Button btn_sure=(Button)dateDialog.findViewById(R.id.btn_datepicker_sure);

btn_sure.setOnClickListener(sureListener);

Button btn_cancel=(Button)dateDialog.findViewById(R.id.btn_datepicker_cancel);

btn_cancel.setOnClickListener(cancelListener);

}

private OnClickListener sureListener = new OnClickListener() {

@Override

public void onClick(View v) {

Calendar calendar = Calendar.getInstance();

if(type==1 || type==2){

calendar.set(dp.getYear(), dp.getmonth(),

dp.getDayOfMonth(), tp.getCurrentHour(),

tp.getCurrentMinute());

SimpleDateFormat sdf = new SimpleDateFormat(formStr);

dateString= sdf.format(calendar.getTime());

editText.setText(dateString);

Log.i("onTouch", dateString+"--");

dateDialog.cancel();

}else {

if(count<2){

count++;

tp.setVisibility(View.VISIBLE);

dp.setVisibility(View.GONE);

picktitle.setText("請選擇時間");

}else{

count=1;

calendar.set(dp.getYear(), dp.getMonth(),

dp.getDayOfMonth(), tp.getCurrentHour(),

tp.getCurrentMinute());

SimpleDateFormat sdf = new SimpleDateFormat(formStr);

dateString= sdf.format(calendar.getTime());

editText.setText(dateString);

Log.i("onTouch", dateString+"--");

dateDialog.cancel();

}

}

}

};

private OnClickListener cancelListener = new OnClickListener() {

@Override

public void onClick(View v) {

dateDialog.cancel();

}

};

/**

* 設置日期和時間(無初始化 日期時間參數),typ=1:時間選擇;type=2:日期選擇;type=3:日期時間(無秒)選擇,type=4 日期時間(含秒)選擇

* minDate,maxDate 設置 日期選擇范圍,值為0則不設置(使用默認)

* @param editText

*/

public void getDateTimePicker(EditText editText,int type,long minDate,long maxDate, final int minHour,final int maxHour){

this.type=type;

int year, month, day;

this.editText=editText;

Calendar calendar=Calendar.getInstance();

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH);

day=calendar.get(Calendar.DAY_OF_MONTH);

dateDialog= new MyDialog(context, R.layout.zsg_datetime_pick);

picktitle=(TextView)dateDialog.findViewById(R.id.tv_datetime_pick_title);

dp=(DatePicker)dateDialog.findViewById(R.id.datepicker);

tp=(TimePicker)dateDialog.findViewById(R.id.timepicker);

if (minDate!=0) {

dp.setMinDate(minDate);

}

if (maxDate!=0) {

dp.setMaxDate(maxDate);

}

switch (this.type) {

case 1:

tp.setVisibility(View.VISIBLE);

dp.setVisibility(View.GONE);

picktitle.setText("請選擇時間");

formStr="HH:mm";

break;

case 2:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd";

picktitle.setText("請選擇日期");

break;

case 3:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd HH:mm";

picktitle.setText("請選擇日期");

break;

default:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd HH:mm:ss";

picktitle.setText("請選擇日期");

}

dp.setCalendarViewShown(false);

dp.init(year, month, day, new DatePicker.OnDateChangedListener() {

public void onDateChanged(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

}

});

int a=tp.getCurrentHour()+0;

tp.setIs24HourView(true);

tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

if (hourOfDay>maxHour) {

view.setCurrentHour(maxHour);

}

if (hourOfDay<minHour) {

view.setCurrentHour(minHour);

}

}

});

tp.setCurrentHour(a);

Button btn_sure=(Button)dateDialog.findViewById(R.id.btn_datepicker_sure);

btn_sure.setOnClickListener(sureListener);

Button btn_cancel=(Button)dateDialog.findViewById(R.id.btn_datepicker_cancel);

btn_cancel.setOnClickListener(cancelListener);

}

/**

* 設置日期和時間(有初始化 日期時間參數(從 editText 中獲?。?typ=1:時間選擇;type=2:日期選擇;type=3:日期時間(無秒)選擇,type=4 日期時間(含秒)選擇

* minDate,maxDate 設置 日期選擇范圍,值為0則不設置(使用默認); strForm: 時間格式

* @param editText

*/

public void getDateTimePicker(EditText editText,int type,long minDate,long maxDate, final int minHour,final int maxHour , boolean isDefault){

this.type=type;

int year, month, day;

this.editText=editText;

Calendar calendar=Calendar.getInstance();

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH);

day=calendar.get(Calendar.DAY_OF_MONTH);

dateDialog= new MyDialog(context, R.layout.zsg_datetime_pick);

picktitle=(TextView)dateDialog.findViewById(R.id.tv_datetime_pick_title);

dp=(DatePicker)dateDialog.findViewById(R.id.datepicker);

tp=(TimePicker)dateDialog.findViewById(R.id.timepicker);

if (minDate!=0) {

dp.setMinDate(minDate);

}

if (maxDate!=0) {

dp.setMaxDate(maxDate);

}

switch (this.type) {

case 1:

tp.setVisibility(View.VISIBLE);

dp.setVisibility(View.GONE);

picktitle.setText("請選擇時間");

formStr="HH:mm";

break;

case 2:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd";

picktitle.setText("請選擇日期");

break;

case 3:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd HH:mm";

picktitle.setText("請選擇日期");

break;

default:

tp.setVisibility(View.GONE);

dp.setVisibility(View.VISIBLE);

formStr="yyyy-MM-dd HH:mm:ss";

picktitle.setText("請選擇日期");

}

if (isDefault) {

DateFormat df = new SimpleDateFormat(formStr);

try {

Date d = df.parse(editText.getText().toString());

calendar.setTime(d);

year=calendar.get(Calendar.YEAR);

month=calendar.get(Calendar.MONTH);

day=calendar.get(Calendar.DAY_OF_MONTH);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printstacktrace();

}?

}

dp.setCalendarViewShown(false);

dp.init(year, month, day, new DatePicker.OnDateChangedListener() {

public void onDateChanged(DatePicker view, int year, int monthOfYear,

int dayOfMonth) {

}

});

int a=tp.getCurrentHour()+0;

tp.setIs24HourView(true);

tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

if (hourOfDay>maxHour) {

view.setCurrentHour(maxHour);

}

if (hourOfDay<minHour) {

view.setCurrentHour(minHour);

}

}

});

tp.setCurrentHour(a);

if (isDefault) {

tp.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));

tp.setCurrentMinute(calendar.get(Calendar.MINUTE));

}

Log.i("onTouch", calendar.get(Calendar.HOUR_OF_DAY)+"--"+calendar.get(Calendar.MINUTE)+"~"+ calendar.get(Calendar.SECOND));

Button btn_sure=(Button)dateDialog.findViewById(R.id.btn_datepicker_sure);

btn_sure.setOnClickListener(sureListener);

Button btn_cancel=(Button)dateDialog.findViewById(R.id.btn_datepicker_cancel);

btn_cancel.setOnClickListener(cancelListener);

}

}

相關閱讀

什么是時間復雜度和空間復雜度

先簡要介紹一下:算法復雜度分為時間復雜度和空間復雜度。其作用:時間復雜度是指執行這個算法所需要的計算工作量;而空間復雜度是指執

920全球雙十一備貨節入口在哪里?活動時間多久?

920備貨節終于來了,還有不知道的小伙伴嗎?今年920大促的主題是“全球雙十一備貨節”,大家可別小瞧了920備貨節哦,它的銷售情況可以影

通勤時間的好去處

為大家推薦如下

雙12淘寶最晚發貨時間是什么時候?交易流程有什么調整?

雙十一剛剛進入尾聲,人們都進入了等待快遞的過程,物流公司日以繼夜的在忙著,轉眼再過一個月就是雙十二了,很多人擔心自己的快遞不能按

java秒數轉換成標準的北京時間

//服務器返回的值往往是秒,但是計算的時候要求毫秒,需要*1000L才能得到正確的日期結果。SimpleDateFormat sdf = new SimpleDateFor

分享到:

欄目導航

推薦閱讀

熱門閱讀

超市配送赚钱吗