gpt4 book ai didi

android - 如何使用 DatePickerDialog 作为首选项

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

我有首选项工作,我正在使用 CheckBoxPreference 和 EditTextPreference 的组合。我想用 DatePickerDialog 替换其中一个。

当我的设置屏幕显示时,如果您单击其中一个首选项,我希望弹出日期选择器对话框供用户选择日期,并将选择的日期保存在首选项中。我在其他应用程序中看到过这项工作,但我不知道如何做到这一点。

我的日期选择器对话框在常规 View 中工作(根据教程),但我想根据偏好使用它。

最佳答案

感谢@commonsware。我关注了他的项目并创建了一个日期选择器首选项对话框。所以它会帮助某人。

按照步骤在首选项窗口中打开日期选择器。

1 .为日期选择器创建自定义对话框首选项。

package com.packagename;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;

public class DatePreference extends DialogPreference {
private int lastDate = 0;
private int lastMonth = 0;
private int lastYear = 0;
private String dateval;
private CharSequence mSummary;
private DatePicker picker = null;
public static int getYear(String dateval) {
String[] pieces = dateval.split("-");
return (Integer.parseInt(pieces[0]));
}

public static int getMonth(String dateval) {
String[] pieces = dateval.split("-");
return (Integer.parseInt(pieces[1]));
}

public static int getDate(String dateval) {
String[] pieces = dateval.split("-");
return (Integer.parseInt(pieces[2]));
}

public DatePreference(Context ctxt, AttributeSet attrs) {
super(ctxt, attrs);

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

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

// setCalendarViewShown(false) attribute is only available from API level 11
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
picker.setCalendarViewShown(false);
}

return (picker);
}

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

picker.updateDate(lastYear, lastMonth + 1, lastDate);
}

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

if (positiveResult) {
lastYear = picker.getYear();
lastMonth = picker.getMonth();
lastDate = picker.getDayOfMonth();

String dateval = String.valueOf(lastYear) + "-"
+ String.valueOf(lastMonth) + "-"
+ String.valueOf(lastDate);

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

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

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

if (restoreValue) {
if (defaultValue == null) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format1.format(cal.getTime());
dateval = getPersistedString(formatted);
} else {
dateval = getPersistedString(defaultValue.toString());
}
} else {
dateval = defaultValue.toString();
}
lastYear = getYear(dateval);
lastMonth = getMonth(dateval);
lastDate = getDate(dateval);
}

public void setText(String text) {
final boolean wasBlocking = shouldDisableDependents();

dateval = text;

persistString(text);

final boolean isBlocking = shouldDisableDependents();
if (isBlocking != wasBlocking) {
notifyDependencyChange(isBlocking);
}
}

public String getText() {
return dateval;
}

public CharSequence getSummary() {
return mSummary;
}

public void setSummary(CharSequence summary) {
if (summary == null && mSummary != null || summary != null
&& !summary.equals(mSummary)) {
mSummary = summary;
notifyChanged();
}
}
}

2 。在“res/xml/yourpreference.xml”中的偏好xml中添加以下代码

<com.packagename.DatePreference 
android:key="keyname"
android:title="Title of the preference"
android:defaultValue="2014-08-01"
android:summary="Summary"/>

注意:根据您的要求更改“keyname”、“Title of the preference”、“2014-08-01”、“summary”

3 .如果您想通过 Preference Activity 更改默认值,请使用以下代码。

package com.packagename;

import android.os.Bundle;
import com.packagename.DatePreference;

public class CustomPreference extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences);

final DatePreference dp= (DatePreference) findPreference("keyname");
dp.setText("2014-08-02");
dp.setSummary("2014-08-02");
dp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,Object newValue) {
//your code to change values.
dp.setSummary((String) newValue);
return true;
}
});

}
}

现在享受...

关于android - 如何使用 DatePickerDialog 作为首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4216082/

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