DatePickerDialog 的 setTitle 不起作用
setTitle for DatePickerDialog is not working
我一直在尝试制作 DatePickerDialog 和 TimePickerDialog 以在顶部显示标题,但到目前为止没有任何效果。在显示 date/time 选择器之前,我尝试使用 setTitle,还尝试了 setCustomTitle。
这是我的对话框 class,我用来显示基于 this answer.
的日期选择器和时间选择器
DateTimePickerDialog.java
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Calendar;
public class DateTimePicker extends DatePickerDialog {
@NonNull
private Calendar calendar = Calendar.getInstance();
@Nullable
private DatePickerDialog datePickerDialog;
@Nullable
private TimePickerDialog timePickerDialog;
private String pickerTitle;
private TextView customTitle;
public DateTimePicker(Context context, OnDateSetListener dateListener, int year, int monthOfYear, int dayOfMonth) {
super(context, dateListener, year, monthOfYear, dayOfMonth);
}
public setPickerTitle(String title, TextView view){
this.pickerTitle = title;
this.customTitle = view;
}
public void showDialog(@NonNull Context context, long time) {
calendar.setTimeInMillis(time);
closeDialogs();
showDatePicker(context);
}
private void closeDialogs() {
if (datePickerDialog != null) {
datePickerDialog.dismiss();
datePickerDialog = null;
}
if (timePickerDialog != null) {
timePickerDialog.dismiss();
timePickerDialog = null;
}
}
private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
timePicker(view.getContext());
dateListener.onDateSet(view, year, month, dayOfMonth);
}
};
private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
timeListener.onTimeSet(view, hourOfDay, minute);
}
};
private void showDatePicker(@NonNull Context context) {
datePickerDialog = new DatePickerDialog(context,
dateSetListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setTitle(this.pickerTitle);
datePickerDialog.show();
}
private void timePicker(@NonNull Context context) {
timePickerDialog = new TimePickerDialog(context,
timeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
true);
timePickerDialog.setCustomTitle(this.customTitle);
timePickerDialog.show();
}
}
这就是我在 Fragment 中使用它的方式:
private void onDateTimeClick(){
DateTimePicker dateTimePicker = new DateTimePicker(getContext(), onDateSetListener, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);
TextView view = new TextView(getContext());
view.setText("Custom title");
dateTimePicker.setPickerTitle("My title", view);
dateTimePicker.showDialog(Objects.requireNonNull(getContext()), Calendar.getInstance().getTimeInMillis());
}
我尝试重写 here 中提到的 setTitle 方法,单独使用和组合使用 setTitle 和 setCustomTitle,我遍历了在 SO 上可以找到的所有内容,但到目前为止没有任何效果。当我调试代码时,我注意到调用 setTitle 时,光标跳转到 AlertDialog.java 上的 setTitle,然后跳转到 PhoneWindow.java 的下面方法,其中 mViewTitle 为空。我想这可能是问题所在,但不知道如何解决。
public void setTitle(CharSequence title, boolean updateAccessibilityTitle) {
if (mTitleView != null) {
mTitleView.setText(title);
} else if (mDecorContentParent != null) {
mDecorContentParent.setWindowTitle(title);
}
mTitle = title;
if (updateAccessibilityTitle) {
WindowManager.LayoutParams params = getAttributes();
if (!TextUtils.equals(title, params.accessibilityTitle)) {
params.accessibilityTitle = TextUtils.stringOrSpannedString(title);
if (mDecor != null) {
// ViewRootImpl will make sure the change propagates to WindowManagerService
ViewRootImpl vr = mDecor.getViewRootImpl();
if (vr != null) {
vr.onWindowTitleChanged();
}
}
dispatchWindowAttributesChanged(getAttributes());
}
}
}
感谢您的帮助
创建DatePickerDialog
时需要设置主题,标题才会显示。
new DatePickerDialog(
context,
android.R.style.Theme_Material_Light_Dialog,
...
))
我一直在尝试制作 DatePickerDialog 和 TimePickerDialog 以在顶部显示标题,但到目前为止没有任何效果。在显示 date/time 选择器之前,我尝试使用 setTitle,还尝试了 setCustomTitle。 这是我的对话框 class,我用来显示基于 this answer.
的日期选择器和时间选择器DateTimePickerDialog.java
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Calendar;
public class DateTimePicker extends DatePickerDialog {
@NonNull
private Calendar calendar = Calendar.getInstance();
@Nullable
private DatePickerDialog datePickerDialog;
@Nullable
private TimePickerDialog timePickerDialog;
private String pickerTitle;
private TextView customTitle;
public DateTimePicker(Context context, OnDateSetListener dateListener, int year, int monthOfYear, int dayOfMonth) {
super(context, dateListener, year, monthOfYear, dayOfMonth);
}
public setPickerTitle(String title, TextView view){
this.pickerTitle = title;
this.customTitle = view;
}
public void showDialog(@NonNull Context context, long time) {
calendar.setTimeInMillis(time);
closeDialogs();
showDatePicker(context);
}
private void closeDialogs() {
if (datePickerDialog != null) {
datePickerDialog.dismiss();
datePickerDialog = null;
}
if (timePickerDialog != null) {
timePickerDialog.dismiss();
timePickerDialog = null;
}
}
private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
timePicker(view.getContext());
dateListener.onDateSet(view, year, month, dayOfMonth);
}
};
private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
timeListener.onTimeSet(view, hourOfDay, minute);
}
};
private void showDatePicker(@NonNull Context context) {
datePickerDialog = new DatePickerDialog(context,
dateSetListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setTitle(this.pickerTitle);
datePickerDialog.show();
}
private void timePicker(@NonNull Context context) {
timePickerDialog = new TimePickerDialog(context,
timeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
true);
timePickerDialog.setCustomTitle(this.customTitle);
timePickerDialog.show();
}
}
这就是我在 Fragment 中使用它的方式:
private void onDateTimeClick(){
DateTimePicker dateTimePicker = new DateTimePicker(getContext(), onDateSetListener, Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH);
TextView view = new TextView(getContext());
view.setText("Custom title");
dateTimePicker.setPickerTitle("My title", view);
dateTimePicker.showDialog(Objects.requireNonNull(getContext()), Calendar.getInstance().getTimeInMillis());
}
我尝试重写 here 中提到的 setTitle 方法,单独使用和组合使用 setTitle 和 setCustomTitle,我遍历了在 SO 上可以找到的所有内容,但到目前为止没有任何效果。当我调试代码时,我注意到调用 setTitle 时,光标跳转到 AlertDialog.java 上的 setTitle,然后跳转到 PhoneWindow.java 的下面方法,其中 mViewTitle 为空。我想这可能是问题所在,但不知道如何解决。
public void setTitle(CharSequence title, boolean updateAccessibilityTitle) {
if (mTitleView != null) {
mTitleView.setText(title);
} else if (mDecorContentParent != null) {
mDecorContentParent.setWindowTitle(title);
}
mTitle = title;
if (updateAccessibilityTitle) {
WindowManager.LayoutParams params = getAttributes();
if (!TextUtils.equals(title, params.accessibilityTitle)) {
params.accessibilityTitle = TextUtils.stringOrSpannedString(title);
if (mDecor != null) {
// ViewRootImpl will make sure the change propagates to WindowManagerService
ViewRootImpl vr = mDecor.getViewRootImpl();
if (vr != null) {
vr.onWindowTitleChanged();
}
}
dispatchWindowAttributesChanged(getAttributes());
}
}
}
感谢您的帮助
创建DatePickerDialog
时需要设置主题,标题才会显示。
new DatePickerDialog(
context,
android.R.style.Theme_Material_Light_Dialog,
...
))