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

发布于 2024-09-24 13:16:28 字数 2936 浏览 1 评论 0原文

我已经查看了我能找到的所有讨论和线程,但事实并非如此。我有一个简单的计时器,可以更新文本视图(下面示例中的 mTimeTextField)。 mUpdateTimeTask run 方法正在正确执行(每秒),但 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);
           }
        };

}

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

setContentView(mTimeLabel);

但这会崩溃,抱怨视图没有父级。仅供参考,我确实有一个:

setContentView(R.layout.main);

在我的 onCreate() 中调用。

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.

I have code based on the info found here:

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

Here is the 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);

call in my onCreate().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一世旳自豪 2024-10-01 13:16:28

替换

mHandler.postAtTime(this, 1000);

mHandler.postDelayed(this, 1000);

Replace

mHandler.postAtTime(this, 1000);

with

mHandler.postDelayed(this, 1000);
殤城〤 2024-10-01 13:16:28
  1. 您需要在 UI 线程中进行 UI 更新。
  2. 需要在UI线程中创建一个Handler才能运行
    UI 线程中的任务。

    公共类 MainActivity 扩展 AppCompatActivity
        私有处理程序mHandler;
        @覆盖
        protected void onCreate(Bundle savingInstanceState) {
            mHandler = new Handler(); // 这是在 UI 线程中创建 Handler
            // ...
        }
    
  1. You need to do UI updates in the UI thread.
  2. You need to create a Handler in the UI thread to run
    tasks in the UI thread.

    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
            // ...
        }
    
衣神在巴黎 2024-10-01 13:16:28

这可能更符合您正在寻找的内容:

private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {

           final long start = mStartTime;
           long elapseTime = System.currentTimeMillis() - start;
           int seconds = (int) (elapseTime / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;

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

           // add a delay to adjust for computation time
           long delay = (1000 - (elapseTime%1000));

           mHandler.postDelayed(this, delay);
       }
    };

我发现这是为视频添加时间的好方法

This might be more along the lines of what you are looking for:

private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {

           final long start = mStartTime;
           long elapseTime = System.currentTimeMillis() - start;
           int seconds = (int) (elapseTime / 1000);
           int minutes = seconds / 60;
           seconds     = seconds % 60;

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

           // add a delay to adjust for computation time
           long delay = (1000 - (elapseTime%1000));

           mHandler.postDelayed(this, delay);
       }
    };

I found this to be a good way to add a time to video's

逐鹿 2024-10-01 13:16:28

view只能显示在UI线程上。你必须使用 ru 启动可运行程序

view can only be displayed on UI thread. you have to start the runnable using ru

守不住的情 2024-10-01 13:16:28

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

Please ensure R.id.timeTextFieldl is a correct id.

一口甜 2024-10-01 13:16:28
if (seconds < 10) {
    mTimeTextField.setText("" + minutes + ":0" + seconds);
} else {
    mTimeTextField.setText("" + minutes + ":" + seconds);            
}

更短的是:

mTimeTextField.setText(minutes + ":" + (seconds < 10 ? "0" : "") + seconds);

但更好的方法是使用 String.format^

mTimeTextField.setText(String.format("%d:%s%d", minutes, (seconds < 10 ? "0" : ""), seconds);
if (seconds < 10) {
    mTimeTextField.setText("" + minutes + ":0" + seconds);
} else {
    mTimeTextField.setText("" + minutes + ":" + seconds);            
}

shorter is:

mTimeTextField.setText(minutes + ":" + (seconds < 10 ? "0" : "") + seconds);

but better way is using String.format^

mTimeTextField.setText(String.format("%d:%s%d", minutes, (seconds < 10 ? "0" : ""), seconds);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文