如果使用键盘,如何捕获 TimePicker 中的时间变化?

发布于 2024-09-12 11:38:32 字数 718 浏览 3 评论 0原文

我有一个小部件,它是一个 TimePicker,用于检索数据库字段中保存的时间。

问题是,当用户更改小部件中的时间值时,这不会保存在数据库中。

因此,如果您仅在小部件中使用加号和减号,我遇到了 setOnTimeChangedListener 方法,它的作用就像一个魅力。如果您使用关键字,它不会捕获更改。

这是我的代码:

pickedTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
            System.out.println("What time is it? "
                    + String.valueOf(arg0.getCurrentHour()) + ":"
                    + String.valueOf(arg0.getCurrentMinute()));
        }

    });

我也尝试过但不成功:

pickedTime.setOnFocusChangeListenerpickedTime.setOnKeyListener 方法。

I have a widget which is a TimePicker that retrieves the time saved in a field in a database.

Thing is that when the user changes the time value in the widget, this is not being saved in the database.

So I came across the setOnTimeChangedListener method that works like a charm, if you are only using the plus and minus signs in the widget. It does not capture the change if you are using the keyword.

Here is my code:

pickedTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker arg0, int arg1, int arg2) {
            System.out.println("What time is it? "
                    + String.valueOf(arg0.getCurrentHour()) + ":"
                    + String.valueOf(arg0.getCurrentMinute()));
        }

    });

I've also tried unsuccessfully:

pickedTime.setOnFocusChangeListener and pickedTime.setOnKeyListener methods.

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

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

发布评论

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

评论(3

最舍不得你 2024-09-19 11:38:32

在处理对话框和使用 TimePicker 时,它对我不起作用。

我在某处读到它与焦点有关,因此如果您在关闭对话框之前执行clearFocus,它将起作用。

像这样的东西:

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

    if (positiveResult) {
        picker.clearFocus();
        lastHour=picker.getCurrentHour();            
        lastMinute=picker.getCurrentMinute();

        String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

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

It didn't work for me when dealing with a dialog and using the TimePicker.

I read somewhere that it's related to the focus, so if u do a clearFocus right before dismissing the dialog it will work.

Something like:

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

    if (positiveResult) {
        picker.clearFocus();
        lastHour=picker.getCurrentHour();            
        lastMinute=picker.getCurrentMinute();

        String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

        if (callChangeListener(time)) {
            persistString(time);
        }
    }
}
本王不退位尔等都是臣 2024-09-19 11:38:32

正如 mon3t 发现的那样,选项是在 XML 文件中设置 android:AddStatesFromChildren="true" ,如下所示:

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true" />
<TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true" />

或者您可以通过代码设置:

    datePicker.setAddStatesFromChildren(true);
    timePicker.setAddStatesFromChildren(true);

我真的无法理解默认值的意义是什么。

As mon3t found out, the option is to set android:AddStatesFromChildren="true" in the XML file, as:

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true" />
<TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:addStatesFromChildren="true" />

Or you can set that by code:

    datePicker.setAddStatesFromChildren(true);
    timePicker.setAddStatesFromChildren(true);

I really cannot understand what is the point of the default being the way it is.

↙温凉少女 2024-09-19 11:38:32

Sikora 的答案实际上为这个常见问题增加了很多价值。

如果您不管理更改事件并从另一段代码(例如标准按钮的 onClick 处理程序)查找时间选择器的值,则需要明确的焦点才能获取时间选择器的实际值当用户使用键盘手动输入小时或分钟时,而不是使用向上或向下箭头或滚动条,具体取决于它们位于蜂窝或更高版本上。

谢谢西科拉。

Sikora's answer is actually adding a lot of value to this common issue.

If you are not managing the change event and look up the value of your time picker from a different piece of code (an onClick handler for a standard button for instance), then the clear focus is needed to get the real value of the time picker when the user manually enters the hours or minutes with the keyboard as opposed to using the up or down arrows or scroller depending on them being on honeycomb or a later version.

Thanks Sikora.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文