本文介绍了永久设置DatePickerDialog称号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,当我试图永久设置DatePickerDialog冠军的问题。

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

DatePickerFragment.java

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

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节目和对话框的标题是设置日期,但是当我改变日期,标题中包含选定的日期,而不是​​设置日。我怎样才能永久地设置此对话框的标题?

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?

在测试了API 8-10。

Tested on API 8-10.

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

Thank you in advance and sorry for my English. Patrick

推荐答案

如何延长 DatePickerDialog 并添加 setPermanentTitle 方法来存储时的日期得到改变将被迫永久封号?

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

然后用新的 setPermanentTitle 方法:

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

这篇关于永久设置DatePickerDialog称号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-09 23:05