我已经看过每一个讨论和我可以找到的关于让它工作的线程,但事实并非如此。我有一个更新文本视图的简单计时器(下例中的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);
}
};
}
根据一些建议,我尝试添加:
setContentView(mTimeLabel);
但是这会让人抱怨没有父母的观点。仅供参考,我确实有:
setContentView(R.layout.main);
拨打我的onCreate()
。
答案 0 :(得分:19)
替换
mHandler.postAtTime(this, 1000);
与
mHandler.postDelayed(this, 1000);
答案 1 :(得分:5)
您需要在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
// ...
}
答案 2 :(得分:4)
这可能更符合您的要求:
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);
}
};
我发现这是为视频添加时间的好方法
答案 3 :(得分:1)
视图只能在UI线程上显示。你必须使用ru
启动runnable答案 4 :(得分:0)
请确保R.id.timeTextFieldl是正确的ID。
答案 5 :(得分:0)
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);