Android上创建的时钟未更新

时间:2012-04-24 15:42:46

标签: android user-interface time clock

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

}

}

请告诉我如何完成更新用户界面,请...

2 个答案:

答案 0 :(得分:1)

您没有提及您正在处理哪个教程,以防万一,您可能想要使用AsyncTask

答案 1 :(得分:0)

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

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

}