gpt4 book ai didi

android - 在实现 DatePickerDialog 的 DialogFragment 中添加中性按钮

转载 作者:行者123 更新时间:2023-11-30 03:07:34 25 4
gpt4 key购买 nike

正如您在下面的代码中看到的,我尝试添加一个中性按钮来设置 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 fragment 中使用中性按钮?如果是怎么办?

最佳答案

您正在实例化两个对话框。一个 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;

关于android - 在实现 DatePickerDialog 的 DialogFragment 中添加中性按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21541653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com