1
我使用的DialogFragment
顯示DatePicker
。如何顯示對話框上的「取消」按鈕?如何在DatePicker上顯示取消按鈕?
我使用的DialogFragment
顯示DatePicker
。如何顯示對話框上的「取消」按鈕?如何在DatePicker上顯示取消按鈕?
試試這個代碼,因爲我從THIS
DatePickerDialog dialog = new DatePickerDialog(this,
mDateSetListener,
year, month, day);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
// Do Stuff
}
}
});
使用此代碼中找到並修改用戶界面,按您的方便。
/**
* Prepares & shows the Dialog for selecting date.
*/
private void prepareDateDialog()
{
final Dialog dialog = new Dialog(this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
dialog.setContentView(R.layout.dialog_date_picker);
datePicker = (DatePicker) dialog.findViewById(R.id.dialog_date_picker_date);
// initialize DatePicker with the previously initialized values.
datePicker.init(year, month - 1, dayOfMonth, null);
TextView tvDone = (TextView) dialog.findViewById(R.id.dialog_date_picker_tv_done);
TextView tvCancel = (TextView) dialog.findViewById(R.id.dialog_date_picker_tv_cancel);
tvCancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
}
});
tvDone.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
setDate();
}
});
dialog.show();
}
的setDate方法..
/**
* Sets the date selected by the user.
*/
private void setDate()
{
dayOfMonth = datePicker.getDayOfMonth();
month = (datePicker.getMonth()) + 1;
year = datePicker.getYear();
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, dayOfMonth);
c.set(Calendar.MONTH, month);
c.set(Calendar.YEAR, year);
String monthCount = "" + month;
String day = dayOfMonth + "";
if (dayOfMonth < 9)
day = "0" + dayOfMonth;
if (month < 9)
monthCount = "0" + month;
selectedDate = day + "-" + monthCount + "-" + year;
tvDate.setText(selectedDate);
tvBirthday.setText(selectedDate);
}
你有沒有嘗試使用:DialogInterface.BUTTON_NEGATIVE? –
我有你的同樣的問題,我用這個解決:http://stackoverflow.com/questions/14092093/is-there-a-way-to-use-cancel-in-android-jellybean-timepickerdialog/14097934# 14097934 – Manza