[英]How to setText from a separate Thread in Android
I want to setText from within a Thread. 我想从一个Thread中setText。
This is my code of the Thread: 这是我的线程代码:
private class GenerateThread implements Runnable {
public void run(){
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
textOut.setText("Initialising...");
});
}
}
in my main code, I use 在我的主要代码中,我使用
Thread t = new Thread(new GenerateThread());
t.start();
It does not allow me to setText from within the thread. 它不允许我从线程中setText。 Following some posts on internet, I have tried using a handler, but that gave me errors, I think I am double defining Runnable this way.
在互联网上的一些帖子之后,我尝试使用处理程序,但这给了我错误,我想我是这样双重定义Runnable。
Handler handler (before main) 处理程序处理程序(主要之前)
private class GenerateThread implements Runnable {
public void run(){
handler.post(new Runnable() {
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
textOut.setText("Initialising...");
});
}
}
How can I setText from within the Thread? 如何在线程中设置文本? Thanks!
谢谢!
besides runOnUiThread
there is also View#post(Runnable)
which I would prefer here because you don't need ugly looking references to the outer Activity ( MyActivity.this.runOnUiThread()
). 除了
runOnUiThread
,还有View#post(Runnable)
,我更喜欢这里,因为你不需要看外部Activity( MyActivity.this.runOnUiThread()
)的丑陋外观。
private class GenerateRunnable implements Runnable {
public void run() {
// this code is executed in a background thread.
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
textOut.post(new Runnable() {
public void run() {
// this code is executed on the UI thread.
textOut.setText("Initialising...");
}
});
}
}
@Override
protected void onResume() {
super.onResume();
new Thread(new GenerateRunnable()).start();
}
Also don't confuse Runnable
and Thread
. 也不要混淆
Runnable
和Thread
。 A Runnable
is just an ordinary class with a run()
method. Runnable
只是一个带有run()
方法的普通类。 It can be and often is executed on a new Thread
. 它可以并且通常在新
Thread
上执行。 If you want you can also make GenerateThread
a real Thread
like so: 如果你想要你也可以使
GenerateThread
成为一个真正的Thread
如下所示:
private class GenerateThread extends Thread {
@Override
public void run() {
// code here.
}
}
// start somewhere
new GenerateThread().start();
And besides using classic Thread
you could also think about using AsyncTask
since that is made exactly for tasks that do something long running and need to update the UI afterwards or when there is progress. 除了使用经典
Thread
之外,您还可以考虑使用AsyncTask
因为这完全适用于执行长时间运行并且需要在之后或有进度时更新UI的任务。
One can only update the UI from the UI thread. 只能从UI线程更新UI。 runOnUiThread will allow you to run an action on the UI thread the next time it executes.
runOnUiThread将允许您在下次执行时在UI线程上运行操作。 You can do something like:
你可以这样做:
runOnUiThread(new Runnable() {
public void run() {
textOut.setText("Initialising...");
}
});
EDIT: 编辑:
private class GenerateThread implements Runnable {
public void run(){
// generate the first music
music = generate(prevmusic, prevmusic.length);
prevmusic = music;
// write the midi
writeMidi(music, song);
// Update the UI
MyActivity.this.runOnUiThread(new Runnable() {
public void run() {
textOut.setText("Initialising...");
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.