gpt4 book ai didi

android - PreferenceScreen 中的 TimePicker

转载 作者:IT老高 更新时间:2023-10-28 13:11:49 27 4
gpt4 key购买 nike

我想创建一个名为 Interval 的首选项字段,并且我希望能够弹出一个 TimePicker 并设置一个 mm:ss 具有最小值 00:30 和步长 30 秒的格式化值。

PreferenceScreen中是否可以使用TimePicker

最佳答案

Android 中没有内置 TimePreference。但是,创建自己的相当容易。 Here's one I did :

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 lastHour=0;
private int lastMinute=0;
private TimePicker picker=null;

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 ctxt, AttributeSet attrs) {
super(ctxt, attrs);

setPositiveButtonText("Set");
setNegativeButtonText("Cancel");
}

@Override
protected View onCreateDialogView() {
picker=new TimePicker(getContext());

return(picker);
}

@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);

picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}

@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);

if (positiveResult) {
lastHour=picker.getCurrentHour();
lastMinute=picker.getCurrentMinute();

String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

if (callChangeListener(time)) {
persistString(time);
}
}
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return(a.getString(index));
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String time=null;

if (restoreValue) {
if (defaultValue==null) {
time=getPersistedString("00:00");
}
else {
time=getPersistedString(defaultValue.toString());
}
}
else {
time=defaultValue.toString();
}

lastHour=getHour(time);
lastMinute=getMinute(time);
}
}

关于android - PreferenceScreen 中的 TimePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5533078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com