2

我正在做android自动测试,并尝试在android中测试闹钟应用程序。我想在 TimePickerDialog 弹出后获取 TimePicker,然后我们可以使用它来调用方法 'setCurrentHour(...)' 和 'setCurrentMinute()' 以编程方式设置闹钟时间,而不是多次发送按键事件. 谢谢你。

4

3 回答 3

1

这可能会有所帮助

    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());
     }
 }
于 2013-09-04T06:36:26.670 回答
1
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;
}
于 2017-04-27T15:55:48.480 回答
0

第一次onTimeChanged触发时,保存方法中传递的视图。该视图将作为 TimePicker 的参考。

于 2010-07-27T11:21:31.473 回答