无法在androidx上使用DialogPreference

时间:2019-06-29 03:55:58

标签: android androidx android-timepicker

我是android studio的新手,正在尝试制作一个简单的todolist应用。我已经使用“警报管理器”设置了每日警报,但是我希望用户能够在设置页面中更改该每日警报关闭的时间。因此,我在偏好屏幕中需要一个时间选择器,并且我了解到您可以使用自定义DialogPreferences进行此操作。

我尝试按照本教程http://www.infiniteimprob.com/blog/custom-preferences/进行操作,但它显示了此异常com.example.todolist.TimePickerPreference cannot be cast to androidx.preference.Preference,我感觉这是由于兼容性问题所致,因为我将androidx库用于其他导入。我认为如果我导入androidx.preference.DialogPreference而不是android.preference.DialogPreference;可以解决此问题,有人可以告诉我如何进行此更改吗?我已经读过这个答案Difference between DialogPreference before and after AndroidX,但我仍然不太了解如何执行它。

这是我在网站上遵循的timepickerpreference类:

import android.content.res.TypedArray;
import android.os.Build;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimePickerPreference extends DialogPreference {

    private static final int DEFAULT_TIME = 0;
    private TimePicker mTimePicker;
    private int currentTime;

    public TimePickerPreference(Context context, AttributeSet attrs){
        super(context, attrs);
        setDialogLayoutResource(R.layout.timepickerpreferencelayout);
        setPositiveButtonText("ok");
        setNegativeButtonText("cancel");
    }

    private static int getTime(int hour, int minute){
        return hour*60*60*1000 + minute*60*1000;
    }

    private static int getHour(int value){
        return (int)(value / (60*60*1000));
    }

    private static int getMinute(int value){
        return (int)(value % (60*60*1000));
    }

    @Override
    protected View onCreateDialogView() {
        View view = super.onCreateDialogView();
        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
        mTimePicker.setIs24HourView(true);
        if (Build.VERSION.SDK_INT >= 23)
        {
            mTimePicker.setHour(getHour(currentTime));
            mTimePicker.setMinute(getMinute(currentTime));
        }
        else
        {
            mTimePicker.setCurrentHour(getHour(currentTime));
            mTimePicker.setCurrentMinute(getMinute(currentTime));
        }

        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener()
        {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute)
            {
                TimePickerPreference.this.currentTime = getTime(hour, minute);
            }
        });

        TextView messageTextView = (TextView) view.findViewById(R.id.messageTextView);
        if (getDialogMessage() == null || getDialogMessage().toString().trim().length() == 0)
            messageTextView.setVisibility(View.GONE);
        messageTextView.setText(getDialogMessage());

        return view;
    }

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

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue)
    {
        if(restorePersistedValue)
        {
            currentTime = this.getPersistedInt((int) (defaultValue == null ? DEFAULT_TIME : defaultValue));
        }
        else
        {
            currentTime = (int) defaultValue;
            if (shouldPersist())
                persistInt(currentTime);
        }
    }

    @Override
    protected void onDialogClosed(boolean positiveResult)
    {
        if (positiveResult)
            persistInt(currentTime);
    }
}

0 个答案:

没有答案