正如您在下面的代码中看到的,我尝试添加一个设置query_date
的中性按钮并取消对话框。不幸的是,下面的内容不起作用:根本不显示中性按钮。
我想要一个名为“今天”的中间按钮并执行该操作。
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setNeutralButton("Today", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
query_date = now_date;
dialog.dismiss();
}
});
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMinDate(c.getTimeInMillis() );
return dialog;
}
@SuppressLint("SimpleDateFormat")
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
sdfSource.setTimeZone(tz);
Date date = new GregorianCalendar(year,month,day).getTime();
SimpleDateFormat sdfDestination = new SimpleDateFormat("EEE', 'd LLL");
query_date = sdfSource.format(date);
change_date_button.setText(sdfDestination.format(date));
load_more_bar.setVisibility(View.VISIBLE);
new get_events().execute();
}
}
我从onClick按钮监听器调用DialogFragment的方式如下:
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getFragmentManager(), "datePicker");
是否可以在DatePickerDialog
片段中设置中性按钮?如果是这样的话?
答案 0 :(得分:1)
您正在实例化两个对话框。一个AlertDialog和另一个DatePickerDialog。你应该只使用一个:
@Override public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMinDate(c.getTimeInMillis() );
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Today", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
query_date = now_date;
dialog.dismiss();
}
});
return dialog;
}