0

フルスクリーンの時計を作成しようとしています。時間、分、秒のテキストを設定することができました。でも、アプリを起動すると時間が表示されますが、UI が更新されません... 方法がわかりません。このチュートリアルを読みましたが、理解できません... 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を更新するためにそれを完了する方法を教えてください...

4

2 に答える 2

1

取り組んでいるチュートリアルについては言及していないので、念のため、おそらくAsyncTaskを使用することをお勧めします。

于 2012-04-24T15:45:05.563 に答える
0

Create a Apps to Show Digital Time in Androidについては、この例を参照してください。そして、あなたの場合は使用します

runOnUiThreadUI.as の更新時間

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

コードは次のようになります。

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);
 }

}
于 2012-04-24T15:47:28.653 に答える