2012-04-24 6 views
0

- 私の更新されていません。私は数時間、分、秒のテキストを設定することができました。さて、私はアプリを起動すると、時間が表示されますが、UIは更新されていません...私はそれを行う方法はわかりません、私はこのチュートリアルを読むが、私はそれを理解していない...いずれか1つは、 UIを更新しますか?作成されたクロックは、フルスクリーンクロックを作成しようとして

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

説明するには...コードの一部を表示する必要があります...改善を提案するには – 5hssba

答えて

0

UI.as

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

とあなたのコードのように見える上Upadting時間のためにあなたのケースを使用

runOnUiThreadにAndroidの.ANDにおけるデジタルの時間を表示するアプリを作成するために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を(){ ます。public void実行(){ txtHour.setText(hora.toString()); \t txtMinutes.setText(minutos.toString()); \t txtSeconds.setText(segundos .toString()); \t txtMilliseconds.setText(milisegundos.toString());} })。 – BamsBamx

+0

この例を参照してくださいあなたは1000年遅れでタイマーを使用する必要がhttp://about-android.blogspot.in/2010/04/create-apps-to-show-digital-time-in_19.html –

+0

私の編集答えを参照してください –

1

を私にしてください教えて場合によっては、AsyncTaskを使用することをお勧めします。

関連する問題