如果使用键盘,如何捕获 TimePicker 中的时间变化?
我有一个小部件,它是一个 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.setOnFocusChangeListener 和 pickedTime.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在处理对话框和使用 TimePicker 时,它对我不起作用。
我在某处读到它与焦点有关,因此如果您在关闭对话框之前执行clearFocus,它将起作用。
像这样的东西:
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:
正如 mon3t 发现的那样,选项是在 XML 文件中设置 android:AddStatesFromChildren="true" ,如下所示:
或者您可以通过代码设置:
我真的无法理解默认值的意义是什么。
As mon3t found out, the option is to set android:AddStatesFromChildren="true" in the XML file, as:
Or you can set that by code:
I really cannot understand what is the point of the default being the way it is.
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.