下面列出了android.app.TimePickerDialog#setButton ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Dialog createTimeSetDialog(OBUtils.RunLambda completionBlock, final OBUtils.RunLambda cancelCompletionBlock)
{
timeSetCompletionBlock = completionBlock;
final Calendar calendar = Calendar.getInstance();
TimePickerDialog d = new TimePickerDialog(MainActivity.mainActivity, this, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(MainActivity.mainActivity));
d.setCancelable(false);
d.setCanceledOnTouchOutside(false);
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Back", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
OBUtils.runOnMainThread(cancelCompletionBlock);
}
});
d.setMessage("Please set the current time.\n");
return d;
}
void showPickTimeDialog (final TimePickerDialog.OnTimeSetListener listener)
{
final DatePickerDialog.OnDateSetListener dateListener = (DatePickerDialog.OnDateSetListener) listener;
final Calendar calendar = Calendar.getInstance();
TimePickerDialog d = new TimePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(MainActivity.mainActivity));
//
d.setCancelable(false);
d.setCanceledOnTouchOutside(false);
//
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Back", new DialogInterface.OnClickListener()
{
@Override
public void onClick (DialogInterface dialog, int which)
{
showPickDateDialog(dateListener, null);
}
});
//
LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCustomTitle(linearLayout);
//
d.show();
}
@Override
public void onClick(View view) {
final Calendar c = Calendar.getInstance();
if (date != null)
c.setTime(date);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
boolean is24HourFormat = DateFormat.is24HourFormat(getContext());
SimpleDateFormat twentyFourHourFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
SimpleDateFormat twelveHourFormat = new SimpleDateFormat("hh:mm a", Locale.getDefault());
TimePickerDialog dialog = new TimePickerDialog(getContext(), (timePicker, hourOfDay, minutes) -> {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minutes);
Date selectedDate = calendar.getTime();
String calendarTime;
if (is24HourFormat) {
calendarTime = twentyFourHourFormat.format(selectedDate);
editText.setText(calendarTime);
} else {
calendarTime = twelveHourFormat.format(selectedDate);
editText.setText(calendarTime);
}
listener.onDateSelected(selectedDate);
nextFocus(view);
}, hour, minute, is24HourFormat);
dialog.setTitle(label);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getContext().getString(R.string.date_dialog_clear), (timeDialog, which) -> {
editText.setText(null);
listener.onDateSelected(null);
});
dialog.show();
}
@Override
public void onClick(View view) {
activate();
final Calendar c = Calendar.getInstance();
if (date != null)
c.setTime(date);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
boolean is24HourFormat = DateFormat.is24HourFormat(getContext());
SimpleDateFormat twentyFourHourFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
SimpleDateFormat twelveHourFormat = new SimpleDateFormat("hh:mm a", Locale.getDefault());
TimePickerDialog dialog = new TimePickerDialog(getContext(), (timePicker, hourOfDay, minutes) -> {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minutes);
Date selectedDate = calendar.getTime();
String calendarTime;
if (is24HourFormat) {
calendarTime = twentyFourHourFormat.format(selectedDate);
editText.setText(calendarTime);
} else {
calendarTime = twelveHourFormat.format(selectedDate);
editText.setText(calendarTime);
}
listener.onDateSelected(selectedDate);
nextFocus(view);
date = null;
}, hour, minute, is24HourFormat);
dialog.setTitle(label);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getContext().getString(R.string.date_dialog_clear), (timeDialog, which) -> {
editText.setText(null);
listener.onDateSelected(null);
date=null;
});
dialog.show();
}
void showPickTimeDialog (final TimePickerDialog.OnTimeSetListener listener)
{
final DatePickerDialog.OnDateSetListener dateListener = (DatePickerDialog.OnDateSetListener) listener;
final Calendar calendar = Calendar.getInstance();
TimePickerDialog d = new TimePickerDialog(MainActivity.mainActivity, listener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), DateFormat.is24HourFormat(MainActivity.mainActivity));
//
d.setCancelable(false);
d.setCanceledOnTouchOutside(false);
//
d.setButton(DatePickerDialog.BUTTON_NEGATIVE, "Back", new DialogInterface.OnClickListener()
{
@Override
public void onClick (DialogInterface dialog, int which)
{
if (screenType == ScreenType.SET_DATE_SCREEN)
{
showPickDateDialog(dateListener, null);
}
else
{
MainActivity.log("OBSetupMenu:showPickTimeDialog:cancelled!");
}
}
});
//
LinearLayout linearLayout = new LinearLayout(MainActivity.mainActivity.getApplicationContext());
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setCustomTitle(linearLayout);
//
d.show();
}
private TimePickerDialog getDialog() {
TimePickerDialog picker = new TimePickerDialog(this.proxy.getActivity(),
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker selectedTime,
int selectedHour, int selectedMinute) {
// TODO Auto-generated method stub
hour = selectedHour;
minute = selectedMinute;
KrollDict data = new KrollDict();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
Date value = calendar.getTime();
data.put("value", value);
data.put("hour", hour);
data.put("minute", minute);
fireEvent("click", data);
}
}, hour, minute, DateFormat.is24HourFormat(this.proxy
.getActivity()));
picker.setCanceledOnTouchOutside(false);
picker.setButton(DialogInterface.BUTTON_POSITIVE, okButtonTitle, picker);
picker.setButton(DialogInterface.BUTTON_NEGATIVE, cancelButtonTitle,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
fireEvent("cancel", new KrollDict());
}
});
return picker;
}