Android TimePicker 在 PreferenceScreen ->读取值

发布于 2024-10-19 10:09:59 字数 1357 浏览 4 评论 0原文

我有一个自定义的 DialogPreference。该对话框是从 PreferenceScreen 调用的。 一切正常,对话框启动并显示 TimePicker。

但如何获得选定的值呢? 首先,我尝试在偏好的摘要中写下所选的时间。 (因此 var xxx :) 稍后,我想将这些值保存在 SharedPreferences 中。

这就是我现在所拥有的:

public class Calendar extends DialogPreference implements
    TimePicker.OnTimeChangedListener {

TimePicker tp;
int xxx;

public Calendar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

public Calendar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
}

private void initialize() {
    setPersistent(true);
}

@Override
protected View onCreateDialogView() {
    tp = new TimePicker(getContext());
    tp.setIs24HourView(true);

    return tp;
}

@Override
public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
}

@Override
public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        // getEditor().
        setTitle(getTitle());
        setSummary(Integer.toString(xxx));

    }
}
private TimePicker.OnTimeChangedListener mTimeSetListener =
    new TimePicker.OnTimeChangedListener() {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            xxx=hourOfDay;

        }
    };
}

非常感谢并致以最诚挚的问候

I have a custom DialogPreference. The Dialog is called from a PreferenceScreen.
All works fine, the Dialog starts and shows the TimePicker.

But how do I get the selected values?
First of all, I tried to write the selected hours in the summary of the Preference. (therefore the var xxx :)
Later on, I want to save the values in SharedPreferences.

This is what I have for now:

public class Calendar extends DialogPreference implements
    TimePicker.OnTimeChangedListener {

TimePicker tp;
int xxx;

public Calendar(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
}

public Calendar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
}

private void initialize() {
    setPersistent(true);
}

@Override
protected View onCreateDialogView() {
    tp = new TimePicker(getContext());
    tp.setIs24HourView(true);

    return tp;
}

@Override
public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
}

@Override
public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        // getEditor().
        setTitle(getTitle());
        setSummary(Integer.toString(xxx));

    }
}
private TimePicker.OnTimeChangedListener mTimeSetListener =
    new TimePicker.OnTimeChangedListener() {

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            xxx=hourOfDay;

        }
    };
}

Thanks a lot and best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2024-10-26 10:09:59

感谢您提出这个问题,它为我提供了有关如何创建 DialogPreference 的重要答案。

我希望我也能给你一个答案。我稍微修改了您的代码,现在可以存储从对话框中选择的时间:

@Override
protected View onCreateDialogView() {
    this.tp = new TimePicker(getContext());
    this.tp.setIs24HourView(true);
    final String storedValue = getPersistedString("07:00");
    final String[] split = storedValue.split(":");
    this.tp.setCurrentHour(Integer.parseInt(split[0]));
    this.tp.setCurrentMinute(Integer.parseInt(split[1]));
    return this.tp;
}

@Override
public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        final String result = this.tp.getCurrentHour() + ":" + this.tp.getCurrentMinute();
        persistString(result);
    }
}

显示对话框时,我检索存储的值并简单地设置 currentHourcurrentMinute TimePicker 的字段。当对话框关闭时,反之亦然。由于我控制传入和传出的格式,因此不应该存在非法值的问题。

这是您要找的吗?

Thanks for asking this question, it provided me with an important answer on how to create a DialogPreference.

I hope I might also have an answer for you. I modified your code a little bit and I can now store the time selected from the Dialog:

@Override
protected View onCreateDialogView() {
    this.tp = new TimePicker(getContext());
    this.tp.setIs24HourView(true);
    final String storedValue = getPersistedString("07:00");
    final String[] split = storedValue.split(":");
    this.tp.setCurrentHour(Integer.parseInt(split[0]));
    this.tp.setCurrentMinute(Integer.parseInt(split[1]));
    return this.tp;
}

@Override
public void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);
    if (positiveResult) {
        final String result = this.tp.getCurrentHour() + ":" + this.tp.getCurrentMinute();
        persistString(result);
    }
}

When the dialog is shown I retrieve the stored value and simply set the currentHour and currentMinute fields of the TimePicker. The other way round when the dialog is closed. Since I control both the format on the way in as well as on the way out there should not be a problem with illegal values.

Was this what you were looking for?

☆獨立☆ 2024-10-26 10:09:59

要将值存储在共享首选项中,请在首选项更改侦听器上实现。
请注意,首选项应该是

preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();

onprefchange 内的默认共享首选项:

if (preference.getKey().equals(getString(R.string.reminder_end_time_key))){

    editor.putString("End_Date", String.valueOf(newValue));
    editor.apply();
    endTimePickerPreference.setSummary((CharSequence)newValue);
}

To store the value in shared pref, implement on preference Change Listener.
note that preference should be default Shared preference

preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();

inside onprefchange:

if (preference.getKey().equals(getString(R.string.reminder_end_time_key))){

    editor.putString("End_Date", String.valueOf(newValue));
    editor.apply();
    endTimePickerPreference.setSummary((CharSequence)newValue);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文