java 永久设置 DatePickerDialog 标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13123445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set DatePickerDialog title permanently
提问by John
I have a problem when I'm trying to set DatePickerDialog title permanently.
当我尝试永久设置 DatePickerDialog 标题时遇到问题。
DatePickerFragment.java
日期选择器片段.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
主活动.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?
当我单击按钮 DatePickerDialog 显示并且对话框标题是“设置日期”但是当我更改日期时,标题包含所选日期而不是“设置日期”。如何永久设置此对话框标题?
Tested on API 8-10.
在 API 8-10 上测试。
Thank you in advance and sorry for my English. Patrick
预先感谢您,并为我的英语感到抱歉。帕特里克
回答by sdabet
How about extending DatePickerDialog
and adding a setPermanentTitle
method to store a permanent title that will be forced when date gets changed ?
如何扩展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);
}
}
And then use the new setPermanentTitle
method:
然后使用新setPermanentTitle
方法:
MyDatePickerDialog dpd = new MyDatePickerDialog(this, null, 2012, 10, 10);
dpd.setPermanentTitle("set date");
回答by AsafK
Extend DatePickerDialog and override setTitle() method.
扩展 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>*);
}
}
回答by Kevin Crain
You could always use a AlertDialog
and call setView
passing an instance of DatePicker
.
您始终可以使用 aAlertDialog
并调用setView
传递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.
这将确保标题仅由您设置。
回答by fmnavarretem
Create a customized title as follows:
创建自定义标题如下:
TextView title = new TextView(getActivity());
title.setText(getResources().getString(R.string.calendar_start_title));
datePickerDialog.setCustomTitle(title);
回答by Prashant Borde
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:
并调用DatePickerDialog
如下:
MyDatePickerDialog datePickerDialog = new MyDatePickerDialog(this, null, 2012, 10, 10, "My Title");