gpt4 book ai didi

android - 当前时间选择器

转载 作者:搜寻专家 更新时间:2023-11-01 08:14:37 25 4
gpt4 key购买 nike

大家好,我正在努力改变时间选择器对话框中显示的时间。

这是 Android SDK 引用中关于onPrepareDialog 方法的引用

Provides an opportunity to prepare a managed dialog before it is being shown. The default implementation calls through to onPrepareDialog(int, Dialog) for compatibility. Override this if you need to update a managed dialog based on the state of the application each time it is shown. For example, a time picker dialog might want to be updated with the current time. You should call through to the superclass's implementation. The default implementation will set this Activity as the owner activity on the Dialog

.

onCreateDialog 方法将创建的对话框作为其参数传递给 onPrepareDialog 但我如何实际更改此对话框中的小时和分钟?

谢谢

好吧,伙计们,我找到了答案,这是我在 PreferenceActivity 中的部分代码:

protected void onPrepareDialog(int id, Dialog dialog) {

if (id == DIALOG_MAX_AGE) {
SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int time = myPref.getInt(getString(R.string.pr_maxtmagekey), 15);
int h = time / 100;
int m = time - h * 100;

((TimePickerDialog)dialog).updateTime(h, m);

}
}

updateTime 做这件事。

最佳答案

你可以尝试这样的事情:

在您的 onCreate() 或您需要的任何地方,使用典型的 Java 代码获取当前时间:

Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);

然后,当您设置时间选择器时,您需要:

... new DatePickerDialog(this, aDateListener, year, month, day);

然后,当您打开时间纠察队时,它将始终显示当前时间。

关于android - 当前时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074511/

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