这是日期弹出框的第一种弹出方式:分开弹出datepicterdialog和timepicterdialog
final DatePickerDialog datePickerDialog = new DatePickerDialog(
TableChart.this, null, mYear, mMonth,
mDay);
datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
"完成", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
DateTimeUtils.showDatePositive(datePickerDialog,starttime);
}
});
datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
"取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
DateTimeUtils.showDateNegative(starttime);
}
});
datePickerDialog.setCanceledOnTouchOutside(false);
datePickerDialog.show();
public static void showDatePositive(DatePickerDialog datePickerDialog,
EditText yearEdit) {
DatePicker datePicker = datePickerDialog.getDatePicker();//获取DatePicter对象
int year = datePicker.getYear();
int month = datePicker.getMonth() + 1;
int day = datePicker.getDayOfMonth();
yearEdit.setText(year+"-"month+"-"+day);
yearEdit.clearFocus();
}
public static void showDateNegative(EditText yearEdit) {
yearEdit.clearFocus();
}
由于timepicterDialog无法获取到timepicter,所以重写:MyTimePickerDialog.java
package com.rts.dzdc.utils;
import com.rts.dzdc.activity.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
/**
* 基本上把源码套用了一下
* @author Lee
*
*/
public class MyTimePickerDialog extends AlertDialog
implements OnClickListener, OnTimeChangedListener {
public interface OnTimeSetListener {
void onTimeSet(TimePicker view, int hourOfDay, int minute);
}
private static final String HOUR = "hour";
private static final String MINUTE = "minute";
private static final String IS_24_HOUR = "is24hour";
private final TimePicker mTimePicker;
private final OnTimeSetListener mCallback;
int mInitialHourOfDay;
int mInitialMinute;
boolean mIs24HourView;
public MyTimePickerDialog(Context context,
OnTimeSetListener callBack,
int hourOfDay, int minute, boolean is24HourView) {
this(context, 0, callBack, hourOfDay, minute, is24HourView);
}
public MyTimePickerDialog(Context context,
int theme,
OnTimeSetListener callBack,
int hourOfDay, int minute, boolean is24HourView) {
super(context, theme);
mCallback = callBack;
mInitialHourOfDay = hourOfDay;
mInitialMinute = minute;
mIs24HourView = is24HourView;
setIcon(0);
setTitle("设置时间");
Context themeContext = getContext();
setButton(BUTTON_POSITIVE, "完成", this);
LayoutInflater inflater =
(LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.time_picker_dialog, null);
setView(view);
mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
// initialize state
mTimePicker.setIs24HourView(mIs24HourView);
mTimePicker.setCurrentHour(mInitialHourOfDay);
mTimePicker.setCurrentMinute(mInitialMinute);
mTimePicker.setOnTimeChangedListener(this);
}
public void onClick(DialogInterface dialog, int which) {
tryNotifyTimeSet();
}
public void updateTime(int hourOfDay, int minutOfHour) {
mTimePicker.setCurrentHour(hourOfDay);
mTimePicker.setCurrentMinute(minutOfHour);
}
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
/* do nothing */
}
private void tryNotifyTimeSet() {
if (mCallback != null) {
mTimePicker.clearFocus();
mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute());
}
}
@Override
protected void onStop() {
tryNotifyTimeSet();
super.onStop();
}
@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
state.putInt(HOUR, mTimePicker.getCurrentHour());
state.putInt(MINUTE, mTimePicker.getCurrentMinute());
state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
return state;
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
int hour = savedInstanceState.getInt(HOUR);
int minute = savedInstanceState.getInt(MINUTE);
mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
mTimePicker.setCurrentHour(hour);
mTimePicker.setCurrentMinute(minute);
}
/**
* 自己加的获取TimePicker方法
* @return
*/
public TimePicker getTimePicker()
{
return mTimePicker;
}
}
使用方法:
final MyTimePickerDialog timePickerDialog = new MyTimePickerDialog(
TableSlideZaiHaiActivity.this, null,
mhour, mmintue, true);
timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
"完成", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
DateTimeUtils.showTimePositive(timePickerDialog,hour);
}
});
timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
"取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
DateTimeUtils.showTimeNegative(slidedown_huadongtimehour);
}
});
timePickerDialog.setCanceledOnTouchOutside(false);
timePickerDialog.show();
public static void showTimePositive(MyTimePickerDialog timePickerDialog,EditText hourEdit) {
TimePicker timePicker = timePickerDialog.getTimePicker();
int hour = timePicker.getCurrentHour();
int mintue = timePicker.getCurrentMinute();
hourEdit.setText(hour+":"+mintue);
hourEdit.clearFocus();
}
public static void showTimeNegative(EditText hourEdit, EditText ) {
hourEdit.clearFocus();
}