2010-07-26 70 views
2

我正在進行android自動測試,並嘗試在android中測試alarmclock應用程序。我想在TimePickerDialog彈出後獲取TimePicker,然後我們可以使用它調用方法的setCurrentHour(...)和setCurrentMinute()來以編程方式設置鬧鐘時間,而不是多次發送鍵盤事件。 謝謝。如何在TimePickerDialog彈出後獲取TimePicker

回答

0

onTimeChanged第一次觸發時,保存方法中傳遞的視圖。該視圖將作爲TimePicker的參考。

+0

對不起,我沒有清楚地說明問題。我們無法觸摸android中的鬧鐘應用程序的源代碼,但只是測試它... – user402068 2010-07-28 00:46:42

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()); 
    } 
} 
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; 
} 
相關問題