2009-06-12 97 views
1

我正在創建視頻錄製應用程序,並使用標籤txtStatustxtTime覆蓋了視頻預覽。使用計時器更新GUI:爲什麼它不起作用?

相機按鈕啓動/停止,其週期性地調用UpdateGUI方法定時器。運行調試我可以看到定時器正在工作 - 它每秒調用updateGUI方法,但該方法不更新控件。

我真的很感激,如果我能得到關於如何解決此問題的任何暗示。

下面是代碼:

這是激活定時器的方法,包括:

private void startTimer() 
{ 
    updateTimer = new Timer("TimerUpdate"); 
    updateTimer.scheduleAtFixedRate(new TimerTask(){ 
     public void run(){ 
      settings.IncreaseRecordingTime(); 
      updateGUI(); 

     } 
    }, 0, 1000); 
} 

這是updateGUI方法:

private void updateGUI() 
{ 
    setStatusLabel(); 
    String strTime = settings.GetTimerString(); //strTime changes every second (it works as expected) 
    txtTimer.setText(strTime);//the text doesn't change! 
} 

這是被調用的方法當按鈕被按下:

private boolean onCaptureButton() 
{ 
    settings.CaptureAction(); 
    videoPreview.setFrameCallback(settings); 
    updateGUI();//here the function updateGUI() works as expected - it changes the txtStatus text from "Preview" to "Recording" 
    setTimer(); 
    return false; 
} 

我加了一些評論,以及(不知道爲什麼updateGUI()作品時,它被稱爲在onCaptureButton()方法和計時器方法中調用時不工作)。

回答

0

更新每個控制呼叫control.Refresh()form.Refresh()後 - 這會強制控件立即重繪。

你的情況:

txtTimer.setText(strTime); // the text doesn't change! 
txtTimer.Refresh(); 
+0

我試過使用txtTimer.refreshDrawableState();但它不起作用。 – 2009-06-12 13:52:57

3

計時器上的計時器線程上運行。您應該只從UI線程更新GUI。有幾種方法可以做到這一點。其中最簡單的是AsyncTasks或將事件發佈到UI處理程序。在nonGUI線程上調用時,某些函數可以工作,但沒有很好的文檔記錄。我從來沒有見過刷新功能。我一直使用invalidate(UI線程)或postInvalidate(後臺線程)。我認爲setText雖然在內部調用。

相關問題