使用 Runnable 和 Runnable 更新 UI postDelayed 不适用于计时器应用程序
我已经查看了我能找到的所有讨论和线程,但事实并非如此。我有一个简单的计时器,可以更新文本视图(下面示例中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
替换
为
Replace
with
需要在UI线程中创建一个Handler才能运行
UI 线程中的任务。
You need to create a Handler in the UI thread to run
tasks in the UI thread.
这可能更符合您正在寻找的内容:
我发现这是为视频添加时间的好方法
This might be more along the lines of what you are looking for:
I found this to be a good way to add a time to video's
view只能显示在UI线程上。你必须使用 ru 启动可运行程序
view can only be displayed on UI thread. you have to start the runnable using ru
请确保 R.id.timeTextFieldl 是正确的 id。
Please ensure R.id.timeTextFieldl is a correct id.
更短的是:
但更好的方法是使用 String.format^
shorter is:
but better way is using String.format^