时间:2010-07-26 09:15:48

标签: android testing timepicker

3 个答案:

答案 0 :(得分:0)

首次触发onTimeChanged时,保存方法中传递的视图。该视图将是对TimePicker的引用。

答案 1 :(得分:0)

这可能会有所帮助

    Calendar mCalendar = Calendar.getInstance();

     mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
     mMinute = mCalendar.get(Calendar.MINUTE);


     TimePickerDialog timePickerDialog = new TimePickerDialog(mActivity, mTimeSetListener, mHour, mMinute, false);

     private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
         public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

             mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
         mCalendar.set(Calendar.MINUTE, mMinute);
         SimpleDateFormat mSDF = new SimpleDateFormat("hh:mm a");
         String time = mSDF.format(mCalendar.getTime());
     }
 }

答案 2 :(得分:0)

public class CustomTimePickerDialog extends TimePickerDialog {

    public CustomTimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) {

        super(context, listener, hourOfDay, minute, is24HourView);
        try {
            Class<?> superClass = getClass().getSuperclass();
            Field TimePickerField = superClass.getDeclaredField("mTimePicker");
            TimePickerField.setAccessible(true);
            TimePicker timePicker = (TimePicker) TimePickerField.get(this);
            timePicker.setOnTimeChangedListener(this);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public int getHour() {
        return currentHour;
    }

    public int getMinute() {
        return currentMinute;
    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

        super.onTimeChanged(view, hourOfDay, minute);
        currentHour = hourOfDay;
        currentMinute = minute;
    }

    private int currentHour;
    private int currentMinute;
}