如何动态地更新用户界面

如何动态地更新用户界面

本文介绍了Android的:如何动态地更新用户界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,我需要动态改变UI:
例如:当用户presses一个按钮,我想改变当前活动的意见,并在某些情况下,有很多涉及到的意见,我需要显示 ProgressDialog

I have an Android app and I need to change UI dynamically:
ex: when user presses a button, I want to change the current activity's views and, as in some cases there are a lot of views involved, I need to display a ProgressDialog.

我一直在使用的AsyncTask ,但我认为这不是最好的解决方案,因为的AsyncTask 小号 doInBackground 运行在不同的线程,所以我不能从那里更新的用户界面 - 我必须使用 runOnUiThread ,其中运行code在UI线程,但冻结所显示的进度对话框。

I have been using an AsyncTask, but I think that it is not the best solution, because AsyncTask's doInBackground runs a different thread, so I can't update UI from there - I have to use runOnUiThread, which runs the code in the UI Thread, but freezes the progress dialog that is shown.

我想应该有更多的人有类似的问题,因为更新的用户界面,而显示 ProgressDialog 似乎有什么东西被应用程序定期进行。

I guess there should be more people with similar issues, as updating UI while displaying a ProgressDialog seems to be something done regularly by applications.

因此​​,没有人有一个解决办法?

So does anyone have a solution to this?

推荐答案

您可以使用这样的:

创建一个处理程序:

 final Handler handler = new Handler();
        final Runnable updateResults = new Runnable() {
            public void run() {
                updateResultsInUi();
            }
        };

和每当你想更新的用户界面,你可以使用任何你想要的

and whenever you want to update UI., you can use wherever you want

handler.post(updateResults);

和在updateResultsInUi方法,你可以做UI的东西:

and In updateResultsInUi method you can do the UI stuff:

 private void updateResultsInUi() {
  // do stuff
 }

希望它可以帮助

Hope it helps

这篇关于Android的:如何动态地更新用户界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:24