java - androidx.preference.DialogPreference 的 TimePicker 自定义首选项
问题描述
我正在尝试将扩展自的 TimePicker 首选项对话框转换为android.support.v7.preference.DialogPreference
相同但扩展自androidx.preference.DialogPreference
.
这是原始自定义首选项的代码:
package com.example.example;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
public class TimePreference extends DialogPreference {
private int mHour = 0;
private int mMinute = 0;
private TimePicker picker = null;
private final String DEFAULT_VALUE = "00:00";
public static int getHour(String time) {
String[] pieces = time.split(":");
return Integer.parseInt(pieces[0]);
}
public static int getMinute(String time) {
String[] pieces = time.split(":");
return Integer.parseInt(pieces[1]);
}
public TimePreference(Context context) {
this(context, null);
}
public TimePreference(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TimePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}
public void setTime(int hour, int minute) {
mHour = hour;
mMinute = minute;
String time = toTime(mHour, mMinute);
persistString(time);
notifyDependencyChange(shouldDisableDependents());
notifyChanged();
}
public String toTime(int hour, int minute) {
return hour + ":" + minute;
}
public void updateSummary() {
String time = mHour + ":" + mMinute;
setSummary(time24to12(time));
}
@Override
protected View onCreateDialogView() {
picker = new TimePicker(getContext());
return picker;
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
picker.setCurrentHour(mHour);
picker.setCurrentMinute(mMinute);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
int currHour = picker.getCurrentHour();
int currMinute = picker.getCurrentMinute();
if (!callChangeListener(toTime(currHour, currMinute))) {
return;
}
// persist
setTime(currHour, currMinute);
updateSummary();
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
String time = null;
if (restorePersistedValue) {
if (defaultValue == null) {
time = getPersistedString(DEFAULT_VALUE);
}
else {
time = getPersistedString(DEFAULT_VALUE);
}
}
else {
time = defaultValue.toString();
}
int currHour = getHour(time);
int currMinute = getMinute(time);
// need to persist here for default value to work
setTime(currHour, currMinute);
updateSummary();
}
public static Date toDate(String inTime) {
try {
DateFormat inTimeFormat = new SimpleDateFormat("HH:mm", Locale.US);
return inTimeFormat.parse(inTime);
} catch(ParseException e) {
return null;
}
}
public static String time24to12(String inTime) {
Date inDate = toDate(inTime);
if(inDate != null) {
DateFormat outTimeFormat = new SimpleDateFormat("hh:mm a", Locale.US);
return outTimeFormat.format(inDate);
} else {
return inTime;
}
}
}
如果我尝试直接在我的偏好片段中使用它,应用程序会因以下错误而崩溃:
android.view.InflateException: Binary XML file line #27: Error inflating class com.example.example.TimePreference
...
Caused by: java.lang.ClassCastException: com.example.example.TimePreference cannot be cast to androidx.preference.Preference
这很正常,考虑到我的自定义偏好不是androidx.preference
我还没有找到任何关于如何将旧的转换android.support.v7.preference
为新的信息androidx.preference
。有谁知道该怎么做?
非常感谢 !
解决方案
推荐阅读
- javascript - Firebase 和 Chart.js - 从数据库中提取到数组中的数据未在图表中显示
- java - 在 Android 中处理菜单点击事件
- python - 用 cv2 检测 2048 年的平铺方块
- fortran - 使用 FFTW3 库评估 FORTRAN 中高斯函数的快速傅里叶变换
- reactjs - Ant Design - 使菜单不可折叠/不可折叠
- java - 类似的代码,但一个版本在应该返回字符串时却不返回
- html - Web Scraping:如何从动态页面获取信息?
- objective-c - 如何从实例方法返回 Swift 中的实例类型
- google-cloud-platform - 我知道目前在 BeamSQL 中不存在 order by 是否有任何解决方法
- sql - 如何在pgsql中将json转换为json数组