您好,登錄后才能下訂單哦!
這是日期彈出框的第一種彈出方式:分開彈出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();
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。