使用Runnable和postDelayed更新UI不使用计时器应用程序

bde*_*ott 22 multithreading android timer handler postdelayed

我已经看过每一个讨论和我可以找到的关于让它工作的线程,但事实并非如此.我有一个更新文本视图的简单计时器(下例中的mTimeTextField).正在正确执行mUpdateTimeTask运行方法(每秒),但UI /文本字段未更新.

我有基于以下信息的代码:

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

这是代码:

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

}
Run Code Online (Sandbox Code Playgroud)

根据一些建议,我尝试添加:

setContentView(mTimeLabel);
Run Code Online (Sandbox Code Playgroud)

但是这会让人抱怨没有父母的观点.仅供参考,我确实有:

setContentView(R.layout.main);
Run Code Online (Sandbox Code Playgroud)

打电话给我onCreate().

Fed*_*dor 19

更换

mHandler.postAtTime(this, 1000);
Run Code Online (Sandbox Code Playgroud)

mHandler.postDelayed(this, 1000);
Run Code Online (Sandbox Code Playgroud)


dav*_*lee 5

  1. 您需要在UI线程中进行UI更新.
  2. 您需要在UI线程中创建一个Handler来在UI线程中运行任务.

    public class MainActivity extends AppCompatActivity
        private Handler mHandler;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            mHandler = new Handler(); // This to create the Handler in the UI thread
            // ...
        }
    
    Run Code Online (Sandbox Code Playgroud)


Fed*_*dor 0

请确保 R.id.timeTextFieldl 是正确的 id。