views:

11

answers:

1

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);
        }
    }
}
// ...
}
A: 

Only a very bad workaround so far...

ViewGroup v = (ViewGroup) timePicker.getChildAt(0);
ViewGroup numberPicker1 = (ViewGroup) v.getChildAt(0);
ViewGroup numberPicker2 = (ViewGroup) v.getChildAt(1);
String hours = ((EditText) numberPicker1.getChildAt(1)).getText().toString();
String mins = ((EditText) numberPicker2.getChildAt(1)).getText().toString();

selectedTime = hours+":"+mins;
cody