【发布时间】:2010-12-21 20:12:36
【问题描述】:
这个例子非常简单:我想让用户知道应用程序正在做什么,只显示一个文本 (canvas.drawText())。然后,出现了我的第一条消息,但没有出现其他消息。我的意思是,我有一个“setText”方法,但它没有更新。
onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(splash); // splash is the view class
loadResources();
splash.setText("this");
boundWebService();
splash.setText("that"):
etc();
splash.setText("so on");
}
视图的文本绘制通过在 onDraw(); 中仅执行 drawText 来工作,因此 setText 更改了文本但不显示它。
有人建议我用 SurfaceView 替换视图,但是仅仅进行几次更新就会很麻烦,所以......我怎么能在运行时动态地更新视图?
应该很简单,只显示一个文本 2 秒,然后主线程执行他的操作,然后更新文本...
谢谢!
更新:
我尝试实现 handler.onPost(),但又是同一个故事。让我把代码给你:
public class ThreadViewTestActivity extends Activity {
Thread t;
Splash splash;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
splash = new Splash(this);
t = new Thread(splash);
t.start();
splash.setTextow("OA");
try { Thread.sleep(4000); } catch (InterruptedException e) { }
splash.setTextow("LALA");
}
}
还有:
public class Splash implements Runnable {
Activity activity;
final Handler myHandler = new Handler();
public Splash(Activity activity) {
this.activity=activity;
}
@Override
public void run() {
// TODO Auto-generated method stub
}
public synchronized void setTextow(final String textow) {
// Wrap DownloadTask into another Runnable to track the statistics
myHandler.post(new Runnable() {
@Override
public void run() {
TextView t = (TextView)activity.findViewById(R.id.testo);
t.setText(textow);
t.invalidate();
}
});
}
}
虽然splash在其他线程,但我在主线程上设置了一个睡眠,我使用处理程序来管理UI和所有东西,它不会改变任何东西,它只显示最后一次更新。
【问题讨论】:
标签: java android user-interface view handler