简体   繁体   中英

Set DatePickerDialog title permanently

I have a problem when I'm trying to set DatePickerDialog title permanently.

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

            }
        });
    }
}

When I click button DatePickerDialog shows and dialog title is "set date" but when I change date, title contains selected date rather than "set date". How can I set this dialog title permanently?

Tested on API 8-10.

Thank you in advance and sorry for my English. Patrick

How about extending DatePickerDialog and adding a setPermanentTitle method to store a permanent title that will be forced when date gets changed ?

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

And then use the new setPermanentTitle method:

    MyDatePickerDialog dpd = new MyDatePickerDialog(this, null, 2012, 10, 10);
    dpd.setPermanentTitle("set date");

Extend DatePickerDialog and override setTitle() method.

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

You could always use a AlertDialog and call setView passing an instance of DatePicker .

See example code below:

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

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

This will ensure title is only set by you.

Create a customized title as follows:

TextView title = new TextView(getActivity());
title.setText(getResources().getString(R.string.calendar_start_title));
datePickerDialog.setCustomTitle(title);

Considering user experience on smaller screens, I would suggest not to keep the title permanent. Here is modification to John's code:

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

And call the DatePickerDialog as follows:

MyDatePickerDialog datePickerDialog = new MyDatePickerDialog(this, null, 2012, 10, 10, "My Title");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM