17

当我尝试永久设置 DatePickerDialog 标题时遇到问题。

DatePickerFragment.java

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
    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);

    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);
    dpd.setTitle("set date");
    return dpd;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    }
}

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getSupportFragmentManager(), "datePicker");

            }
        });
    }
}

当我单击按钮 DatePickerDialog 显示并且对话框标题是“设置日期”但是当我更改日期时,标题包含选定日期而不是“设置日期”。如何永久设置此对话框标题?

在 API 8-10 上测试。

提前谢谢你,对不起我的英语。帕特里克

4

5 回答 5

31

如何扩展DatePickerDialog和添加一种setPermanentTitle方法来存储在日期更改时将强制执行的永久标题?

public class MyDatePickerDialog extends DatePickerDialog {

    private CharSequence title;

    public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
    }

    public void setPermanentTitle(CharSequence title) {
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        setTitle(title);
    }
}

然后使用新setPermanentTitle方法:

    MyDatePickerDialog dpd = new MyDatePickerDialog(this, null, 2012, 10, 10);
    dpd.setPermanentTitle("set date");
于 2012-10-29T14:46:45.030 回答
4

扩展 DatePickerDialog 并覆盖 setTitle() 方法。

public class MyDatePickerDialog extends DatePickerDialog {

   public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
       super(context, callBack, year, monthOfYear, dayOfMonth);
   }

   public void setTitle(CharSequence title) {
       super.setTitle(*<whatever title you want>*);
   }
}
于 2013-12-25T12:00:31.027 回答
2

您始终可以使用 aAlertDialog并调用setView传递DatePicker.

请参见下面的示例代码:

DatePicker datePicker = new DatePicker(getActivity());
    datePicker.setCalendarViewShown(false);

new AlertDialog.Builder(getActivity())
    .setTitle("Your title")
    .setView(datePicker)
    .create().show();

这将确保标题仅由您设置。

于 2015-08-17T11:39:24.093 回答
2

创建自定义标题,如下所示:

TextView title = new TextView(getActivity());
title.setText(getResources().getString(R.string.calendar_start_title));
datePickerDialog.setCustomTitle(title);
于 2017-10-10T17:53:52.223 回答
0

考虑到小屏幕上的用户体验,我建议不要永久保留标题。这是对约翰代码的修改:

public class MyDatePickerDialog extends DatePickerDialog {

    private CharSequence title;

    public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth, CharSequence title) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        if (getDatePicker().getCalendarViewShown()) {
            setTitle(title);
        }
    }
}

并调用DatePickerDialog如下:

MyDatePickerDialog datePickerDialog = new MyDatePickerDialog(this, null, 2012, 10, 10, "My Title");
于 2015-01-16T15:26:04.660 回答