postDelayed不工作计时器应用

postDelayed不工作计时器应用

本文介绍了更新的用户界面与Runnable接口和放大器; postDelayed不工作计时器应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过每次讨论和线程我能找到在得到这个工作,但事实并非如此。我有一个简单的定时器,更新(在下面的例子mTimeTextField)文本视图。该mUpdateTimeTask run方法被正确执行(每秒),但用户界面/文本字段不被更新。

I have looked at every discussion and thread I can find on getting this to work but it is not. I have a simple timer that updates a text view (mTimeTextField in the example below). The mUpdateTimeTask run method is being executed correctly (every second) but the UI/text field is not being updated.

我有一个基于信息code在这里找到:

I have code based on the info found here:

http://android-developers.blogspot.com/2007/11/stitch-in-time.htmlhttp://developer.android.com/resources/articles/timed-ui-updates.html

下面是code:

package com.something.handlertest;

import com.something.handlertest.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Test extends Activity {

    private Handler mHandler = new Handler();
    private int labelNo    = 0;
    private long currTime  = 0L;
    private long mStartTime = 0L;
    TextView mTimeTextField;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTimeTextField = (TextView)findViewById(R.id.timeTextFieldl);

        Button startButton = (Button)findViewById(R.id.start_button);
        startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                   if (mStartTime == 0L) {
                        mStartTime = System.currentTimeMillis();
                        mHandler.removeCallbacks(mUpdateTimeTask);
                        mHandler.postDelayed(mUpdateTimeTask, 100);
                   }
            }
        });
    }

    private Runnable mUpdateTimeTask = new Runnable() {
           public void run() {
               final long start = mStartTime;
               //long millis = SystemClock.uptimeMillis() - start;
               long millis = SystemClock.uptimeMillis();
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;

               //setContentView(mTimeTextField);  This will blow up if I use it

               if (seconds < 10) {
                   mTimeTextField.setText("" + minutes + ":0" + seconds);
               } else {
                   mTimeTextField.setText("" + minutes + ":" + seconds);
               }

               //mHandler.postAtTime(this,
                   //    start + (((minutes * 60) + seconds + 1) * 1000));

               mHandler.postAtTime(this, 1000);
           }
        };

}


每一点建议,我已经尝试添加:


Per some suggestions, I have tried adding:

setContentView(mTimeLabel);

但是,这会崩溃抱怨的观点没有父母。仅供参考,我有一个:

But this will crash complain about the view not having a parent. FYI, I do have a:

setContentView(R.layout.main);

在叫我的的onCreate()

推荐答案

替换

mHandler.postAtTime(this, 1000);

mHandler.postDelayed(this, 1000);

这篇关于更新的用户界面与Runnable接口和放大器; postDelayed不工作计时器应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:25