手动输入未保存在Android的DatePicker(Dialog)中 [英] Manual input not saved in Android's DatePicker(Dialog)

查看:64
本文介绍了手动输入未保存在Android的DatePicker(Dialog)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中实现 DatePicker DatePickerDialog 很容易。但是当涉及到数据存储时,我对这些类有疑问:

Implementing the DatePicker or DatePickerDialog in Android is easy. But when it comes to data storage, I have a problem with those classes:

如果您使用微调器(+或-按钮)更改日期,则一切正常。调用事件日期更改或日期设置,您可以获得用户输入的值。

If you use the spinners (+ or - button) to change the date, everything works fine. The event "Date changed" or "Date set" is called and you can get the values that the user entered.

但是当手动将年份输入到输入字段时(通过键盘),然后用户在对话框中单击保存,将不会调用任何事件,也不会获得手动输入的值。

But when the year is manually entered into the input field (via keyboard) and the user then clicks "Save" in the dialog, there won't be any event called and you won't get that manually entered value.

仅当用户在手动输入年份后再次使用滑块更改某些内容时,此功能才起作用。因为当您使用滑块时,会触发事件。

It only works when the user changes something with the sliders again after manually entering the year. Because when you use the sliders, the events are fired.

这是正常行为吗?我该如何实现所需的行为,即当用户手动输入某些内容然后单击保存时会触发一个事件?

Is this normal behaviour? How can I achieve the desired behaviour, namely that an event is fired when the user enteres something manually and then clicks "Save"?

请先感谢!

推荐答案

只需清除焦点,Android就会从手动输入中设置数字。

Just clear focus, and android will set the number from manual input.

例如:

DatePicker datePicker = findViewById(R.id.dp);

当保存 onClick()时,添加
datePicker.clearFocus();

这必须正常工作。

这篇关于手动输入未保存在Android的DatePicker(Dialog)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆