Android的:如何从TimePicker获取时间当键入 [英] Android: How to get the time from a TimePicker when it is typed in

查看:290
本文介绍了Android的:如何从TimePicker获取时间当键入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框preference它实现了一个简单的TimePicker.OnTimeChangedListener(见下文)。设置时间通过点击+/-按钮的伟大工程。但我不知道如何保存timepicker的状态,当用户键入时直接进入文本框。这可能是足以访问当前文本框的值,所以我能坚持它onDialogClosed。但timePicker.getCurrentHour()不会做。请帮助...

 公共类时preference扩展对话框preference工具
        TimePicker.OnTimeChangedListener {
// ...
@覆盖
公共无效onTimeChanged(TimePicker观点,诠释小时,INT分钟){
    selectedHours =小时;
    selectedMinutes =分钟;
}

@覆盖
公共无效onDialogClosed(布尔positiveResult){
    如果(positiveResult){
        串TimeString以= selectedHours +:+ selectedMinutes;
        如果(isPersistent()){
            persistString(TimeString以);
        }
    }
}
// ...
}
 

解决方案

我没有注意到这个问题,直到我偶然发现了这个问题。 有一个简单的解决方案: 当用户完成更改日期和时间,在我的应用程序,我简单地调用完成()和的onPause()或的onStop()或的onDestroy()我这样做:

  //强制timepicker松动重点和类型值可用!
timePicker.clearFocus();
//重新读取的值,在我的情况下,我把它们放在一个Time对象。
time.hour = timePicker.getCurrentHour();
time.minute = timePicker.getCurrentMinute();
 

在此我保存在我的表的适当列time.toMillis(假)值。

我不知道,如果timepicker还在你onDialogClosed(布尔positiveResult)访问。如果没有,找到另一种回调使用时,它仍然是。

希望这有助于。

I've got a DialogPreference which implements a simple TimePicker.OnTimeChangedListener (see below). Setting the time by clicking the +/- buttons works great. But I don't know how to save the state of the timepicker when the user typed in the time directly into the textfield. It could be sufficient to access to the current textfield value, so I'd be able to persist it in onDialogClosed. But timePicker.getCurrentHour() won't do it. Please help...

public class TimePreference extends DialogPreference implements
        TimePicker.OnTimeChangedListener {
// ...
@Override
public void onTimeChanged(TimePicker view, int hours, int minutes) {
    selectedHours = hours;
    selectedMinutes = minutes;
}

@Override
public void onDialogClosed(boolean positiveResult) {
    if(positiveResult) {
        String timeString = selectedHours + ":" + selectedMinutes;
        if(isPersistent()) {
            persistString(timeString);
        }
    }
}
// ...
}

解决方案

I hadn't noticed this problem until i stumbled upon this question. There is a simple solution: When the user is done changing the date and time in my app, i simply call finish() and in the onPause() or onStop() or onDestroy() I do this:

// force the timepicker to loose focus and the typed value is available !
timePicker.clearFocus();
// re-read the values, in my case i put them in a Time object.
time.hour   = timePicker.getCurrentHour();
time.minute = timePicker.getCurrentMinute();

After this i store the time.toMillis(false) value in the appropriate column of my table.

I don't know if the timepicker is still accessible in your onDialogClosed(boolean positiveResult). If not, find another callback to use when it still is.

Hope this helps.

这篇关于Android的:如何从TimePicker获取时间当键入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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