gpt4 book ai didi

java - 如何将日期选择器对话框 fragment 添加到首选项

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:10 24 4
gpt4 key购买 nike

我有一个带有时间选择器对话框的 Android 项目,我希望将日期选择器添加到首选项

我尝试了几个代码,但没有一个对我有用

pref_dialog_time.xml


<!-- Layout for the TimePreference Dialog -->
<TimePicker
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="18dp"
android:paddingTop="18dp"
android:theme="@style/AppAlertDialogContent"/>

TimePreference.java


public class TimePreference extends DialogPreference {

private int mTime;
private int mDialogLayoutResId = R.layout.pref_dialog_time;

public TimePreference(Context context) {
// Delegate to other constructor
this(context, null);
}

public TimePreference(Context context, AttributeSet attrs) {
// Delegate to other constructor
// Use the preferenceStyle as the default style
this(context, attrs, R.attr.preferenceStyle);
}

public TimePreference(Context context, AttributeSet attrs, int defStyleAttr) {
// Delegate to other constructor
this(context, attrs, defStyleAttr, defStyleAttr);
}

public TimePreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public int getTime() {
return mTime;
}
public void setTime(int time) {
mTime = time;

// Save to SharedPreference
persistInt(time);
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
// The type of this preference is Int, so we read the default value from the attributes
// as Int. Fallback value is set to 0.
return a.getInt(index, 0);
}
@Override
public int getDialogLayoutResource() {
return mDialogLayoutResId;
}

@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
// If the value can be restored, do it. If not, use the default value.
setTime(restorePersistedValue ?
getPersistedInt(mTime) : (int) defaultValue);
}

}

TimePreferenceDialogFragmentCompat.java


public class TimePreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat {

private TimePicker mTimePicker;
public static TimePreferenceDialogFragmentCompat newInstance(String key) {
final TimePreferenceDialogFragmentCompat
fragment = new TimePreferenceDialogFragmentCompat();
final Bundle b = new Bundle(1);
b.putString(ARG_KEY, key);
fragment.setArguments(b);

return fragment;
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);

mTimePicker = (TimePicker) view.findViewById(R.id.edit);

// Exception: There is no TimePicker with the id 'edit' in the dialog.
if (mTimePicker == null) {
throw new IllegalStateException("Dialog view must contain a TimePicker with id 'edit'");
}

// Get the time from the related Preference
Integer minutesAfterMidnight = null;
DialogPreference preference = getPreference();
if (preference instanceof TimePreference) {
minutesAfterMidnight = ((TimePreference) preference).getTime();
}

// Set the time to the TimePicker
if (minutesAfterMidnight != null) {
int hours = minutesAfterMidnight / 60;
int minutes = minutesAfterMidnight % 60;
boolean is24hour = DateFormat.is24HourFormat(getContext());

mTimePicker.setIs24HourView(is24hour);
mTimePicker.setCurrentHour(hours);
mTimePicker.setCurrentMinute(minutes);
}
}
@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
// Get the current values from the TimePicker
int hours;
int minutes;
if (Build.VERSION.SDK_INT >= 23) {
hours = mTimePicker.getHour();
minutes = mTimePicker.getMinute();
} else {
hours = mTimePicker.getCurrentHour();
minutes = mTimePicker.getCurrentMinute();
}

// Generate value to save
int minutesAfterMidnight = (hours * 60) + minutes;

// Save the value
DialogPreference preference = getPreference();
if (preference instanceof TimePreference) {
TimePreference timePreference = ((TimePreference) preference);
// This allows the client to ignore the user value.
if (timePreference.callChangeListener(minutesAfterMidnight)) {
// Save the value
timePreference.setTime(minutesAfterMidnight);
}
}
}
}
}

PreferenceFragmentCustom.java

public class PreferenceFragmentCustom extends PreferenceFragmentCompat {

/**
* {@inheritDoc}
*/
@Override
public void onCreatePreferences(Bundle bundle, String s) {
// Load the Preferences from the XML file
addPreferencesFromResource(R.xml.app_preferences);
}

/**
* {@inheritDoc}
*/
@Override
public void onDisplayPreferenceDialog(Preference preference) {

// Try if the preference is one of our custom Preferences
DialogFragment dialogFragment = null;
if (preference instanceof TimePreference) {
// Create a new instance of TimePreferenceDialogFragment with the key of the related
// Preference
dialogFragment = TimePreferenceDialogFragmentCompat.newInstance(preference.getKey());
}


if (dialogFragment != null) {
// The dialog was created (it was one of our custom Preferences), show the dialog for it
dialogFragment.setTargetFragment(this, 0);
dialogFragment.show(this.getFragmentManager(), "android.support.v7.preference" +
".PreferenceFragment.DIALOG");
} else {
// Dialog creation could not be handled here. Try with the super method.
super.onDisplayPreferenceDialog(preference);
}

}
}

app_prefereces.xml 中时间选择器的代码:

        <de.jakobulbrich.preferences.TimePreference
style="@style/AppPreference.DialogPreference"
android:key="@string/pref_pref4"
android:summary="@string/preferences_time_summ"
android:title="@string/preferences_time_title"/>

那么添加简单日期选择器的最佳方法是什么

最佳答案

我使用的简单方法;

dependencies {
implementation 'com.github.psinetron:slycalendarview:0.0.9'
}

选择选择器按钮*

new SlyCalendarDialog()
.setSingle(false)
.setCallback(listener)
.show(getSupportFragmentManager(), "TAG_SLYCALENDAR");

接口(interface)实现

public class MainActivity extends AppCompatActivity implements SlyCalendarDialog.Callback

onDataSelected时,你可以做同样的事情。

@Override
public void onCancelled() {
//Nothing
}

@Override
public void onDataSelected(Calendar firstDate, Calendar secondDate, int hours, int minutes) {
if (firstDate != null) {
if (secondDate == null) {
firstDate.set(Calendar.HOUR_OF_DAY, hours);
firstDate.set(Calendar.MINUTE, minutes);
Toast.makeText(
this,
new SimpleDateFormat(getString(R.string.timeFormat), Locale.getDefault()).format(firstDate.getTime()),
Toast.LENGTH_LONG

).show();
} else {
Toast.makeText(
this,
getString(
R.string.period,
new SimpleDateFormat(getString(R.string.dateFormat), Locale.getDefault()).format(firstDate.getTime()),
new SimpleDateFormat(getString(R.string.timeFormat), Locale.getDefault()).format(secondDate.getTime())
),
Toast.LENGTH_LONG

).show();
}
}
}

简单的 SharedPreferences 保存和获取首选项;

private void savePreferences(context context, String date){
private SharedPreferences mPreferences;
private SharedPreferences.Editor mEditor;
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
mEditor = mPreferences.edit();

mEditor.putString("save.date.pref", date);
mEditor.commit();
}

private String getPreferences(Context context){
private SharedPreferences mPreferences;
private SharedPreferences.Editor mEditor;
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
mEditor = mPreferences.edit();

mPreferences.getString("save.date.pref", "");
}

关于java - 如何将日期选择器对话框 fragment 添加到首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56715860/

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