在运行时的Andr​​oid更新视图视图、Andr、oid

2023-09-07 16:53:54 作者:患得患失像神经!

这个例子是pretty的简单:我希望让用户知道哪些应用程序正在做通过只是显示一个文本(canvas.drawText())。然后,出现我的第一个消息,而不是其他的。我的意思是,我有一个的setText的方法,但它没有更新。

The example is pretty straightforward: i want to let the user know about what the app is doing by just showing a text (canvas.drawText()). Then, my first message appears, but not the other ones. I mean, i have a "setText" method but it doesn't updates.

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");
}

视图的文本绘制的工作原理是这样做只是在的drawText的OnDraw();,这样的setText更改文本,但并没有表现出来。

The view's text drawing works by doing just a drawText in onDraw();, so setText changes the text but doesn't show it.

有人建议我更换观点与SurfaceView,但它是很多的麻烦,只是一对夫妇的更新,所以...如何赫克我可以dinamically更新视图在运行时?

Someone recommended me replacing the view with a SurfaceView, but it would be alot of trouble for just a couple of updates, SO... how the heck can i update the view dinamically at runtime?

这应该是很简单的,只是显示一个文本说,2秒钟,然后主线程做他的东西,然后更新文本...

It should be quite simple, just showing a text for say 2 seconds and then the main thread doing his stuff and then updating the text...

谢谢!

更新:

我想实现handler.onPost(),但相同的故事一遍。让我把你的code:

I tried implementing handler.onPost(), but is the same story all over again. Let me put you the code:

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(); 
        }                   
    });
}
}

尽管飞溅在其他线程,我把睡眠在主线程中,我使用的处理程序来管理用户界面和一切,这不改变的事情,那只能说明上一次更新。

Although splash is in other thread, i put a sleep on the main thread, i use the handler to manage UI and everything, it doesn't changes a thing, it only shows the last update.

推荐答案

我还没有打这个,但我认为通常的模式是做冗长的初始化在后台线程,并使用处理程序。员额()更新UI。请参见 http://developer.android.com/reference/android/widget/ProgressBar。 HTML 对于不同的,但可能是相关的,例如。

I haven't hit this yet, but I think the usual pattern is to do lengthy initialization in a background thread, and use Handler.post() to update the UI. See http://developer.android.com/reference/android/widget/ProgressBar.html for a different, but possibly related, example.

另请参见这个答案,尤其是第一款:

Also see this answer, especially the first paragraph:

现在的问题是最有可能的是你   正在运行的启动画面(有些   排序对话框,如ProgressDialog   我假设)在同一个线程所有   正在开展的工作。这将保持   闪屏的从视图   被更新,从而可以防止其   甚至得到显示在屏幕上。   你需要显示启动画面,   揭开序幕的AsyncTask的实例   去下载所有数据,然后隐藏   闪屏一旦任务   完成了。

The problem is most likely that you are running the splash screen (some sort of Dialog such as ProgressDialog I assume) in the same thread as all the work being done. This will keep the view of the splash screen from being updated, which can keep it from even getting displayed to the screen. You need to display the splash screen, kick off an instance of AsyncTask to go download all your data, then hide the splash screen once the task is complete.

更新(根据您的更新和您的评论):你是不是应该更新UI,除了一个在其中创建活动的任何线程。为什么无法为您加载资源,在后台线程?

Update (based on your update and your comment): You are not supposed to update the UI in any thread except the one where your Activity is created. Why is it impossible for you to load your resources in a background thread?