我使用显示DialogFragment
的{{1}}。如何显示“取消”'对话框上的按钮?
答案 0 :(得分:3)
尝试使用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
}
}
});
答案 1 :(得分:0)
使用此代码并根据您的方便修改UI。
/**
* 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);
}