java - 永久设置 DatePickerDialog 标题

标签 java android

我在尝试永久设置 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 上测试。

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

最佳答案

如何扩展 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");

关于java - 永久设置 DatePickerDialog 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13123445/

相关文章:

JavaFX : Remove the scene loaded in Center of BorderPane

java - Android 中的不安全类?

java - 从 Firestore 获取 ArrayList 和文档名称

android - 如何撤消对窗口装饰 View 的更改?

android - 当我上传到服务器时,相机图像旋转

java - 为什么 Java 的 String 不能从 Comparable 接口(interface)赋值

java - 使用简单的模拟对象

java - 在java中创建可配置对象存储库的最佳方法是什么?

android - 以编程方式更改 Gingerbread 中的 Actionbar 标题颜色

android - 如何在Android中计算两个TIMESTAMP之间的时间差