2012-04-24 41 views
0

我正嘗試創建全屏時鐘。我設法將文本設置爲幾小時,幾分鐘和幾秒鐘。那麼,但是當我啓動應用程序,它顯示的時間,但用戶界面不更新......我不知道該怎麼做,我讀了這個教程,但我不明白它...任何人都可以解釋我如何consaline更新用戶界面?Android上創建時鐘未更新

public class Clock1Activity extends Activity { 
/** Called when the activity is first created. */ 
private Timer timer; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

final TextView txtHour = (TextView)findViewById(R.id.TxtHour); 
final TextView txtMinutes = (TextView)findViewById(R.id.TxtMinute); 
final TextView txtSeconds = (TextView)findViewById(R.id.TxtSeconds); 
final TextView txtMilliseconds = (TextView)findViewById(R.id.TxtMilliseconds); 

final Integer hora = new Integer(Calendar.HOUR_OF_DAY); 
final Integer minutos = new Integer(Calendar.MINUTE); 
final Integer segundos = new Integer(Calendar.SECOND); 
final Long milisegundos = new Long (System.currentTimeMillis()); 
    timer = new Timer("DigitalClock"); 
    Calendar calendar = Calendar.getInstance(); 
    // Get the Current Time 
    final Runnable updateTask = new Runnable() { 
     public void run() { 
/** txtHour.setText(hora.toString()); 
txtMinutes.setText(minutos.toString()); 
txtSeconds.setText(segundos.toString()); */ 
txtMilliseconds.setText(milisegundos.toString()); 
Toast toast1 = Toast.makeText(getApplicationContext(), milisegundos.toString(), Toast.LENGTH_SHORT); 
toast1.show(); 
     } 
    }; 

    timer.scheduleAtFixedRate(new TimerTask() { 
     @Override 
     public void run() { 
      runOnUiThread(updateTask); 
     } 
    }, 1, 1000); 

}

}

只要告訴我如何完成它更新UI,請...

+0

如果你想有人解釋... thenyou應該表現出你的代碼的某些部分也...使用此提出改進意見 – 5hssba 2012-04-24 15:50:02

回答

0

看到創建一個應用程序,以顯示Android的。而數字時代,你的情況使用

runOnUiThread爲Upadting時間UI.as

CurrentActivity.this.runOnUiThread(new Runnable() { 
    public void run() { 
     //UPDATE TIME HERE 
    } 
}); 

和你的代碼看起來像this例如:

public class Clock1Activity extends Activity { 
/** Called when the activity is first created. */ 
private Timer timer; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final TextView txtHour = (TextView)findViewById(R.id.TxtHour); 
    final TextView txtMinutes = (TextView)findViewById(R.id.TxtMinute); 
    final TextView txtSeconds = (TextView)findViewById(R.id.TxtSeconds); 
    final TextView txtMilliseconds = (TextView)findViewById(R.id.TxtMilliseconds); 


     timer = new Timer("DigitalClock"); 
     Calendar calendar = Calendar.getInstance(); 
     // Get the Current Time 
     final Runnable updateTask = new Runnable() { 
      public void run() { 
    final Integer hora = new Integer(Calendar.HOUR_OF_DAY); 
    final Integer minutos = new Integer(Calendar.MINUTE); 
    final Integer segundos = new Integer(Calendar.SECOND); 
    final Integer milisegundos = new Integer(Calendar.MILLISECOND); 
    txtHour.setText(hora.toString()); 
    txtMinutes.setText(minutos.toString()); 
    txtSeconds.setText(segundos.toString()); 
    txtMilliseconds.setText(milisegundos.toString()); 
      } 
     }; 

     timer.scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(updateTask); 
      } 
     }, 1, 1000); 
} 

} 
+0

,你的意思是做這個? CurrentActivity.this.runOnUiThread(新的Runnable(){ 公共無效的run(){ txtHour.setText(hora.toString()); \t txtMinutes.setText(minutos.toString()); \t txtSeconds.setText(segundos的ToString()); \t txtMilliseconds.setText(milisegundos.toString());} }); – BamsBamx 2012-04-24 16:13:09

+0

看到這個例子http://about-android.blogspot.in/2010/04/create-apps-to-show-digital-time-in_19.html你需要使用定時器1000延遲 – 2012-04-24 16:30:42

+0

看到我的編輯答案 – 2012-04-24 16:35:40

1

你沒有提到這教程你的工作,所以以防萬一,你可能會想要使用AsyncTask