Android的:如何从TimePicker获取时间当键入时间、Android、TimePicker

由网友(相关励志坚强的>>)分享简介:我有一个对话框preference它实现了一个简单的TimePicker.OnTimeChangedListener(见下文)。设置时间通过点击+/-按钮的伟大工程。但我不知道如何保存timepicker的状态,当用户键入时直接进入文本框。这可能是足以访问当前文本框的值,所以我能坚持它onDialogClosed。但t...

我有一个对话框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)访问。如果没有,找到另一种回调使用时,它仍然是。

希望这有助于。

Android如何运用TimePicker控件

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.

阅读全文

相关推荐

最新文章