0

我正在尝试创建一个全屏时钟。我设法将文本设置为小时、分钟和秒。好吧,但是当我启动应用程序时,它会显示时间但 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

请参阅示例以创建应用程序以在 Android 中显示数字时间。在您的情况下使用

runOnUiThread在 UI.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 回答