ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 在Android中运行时更新View

2019-06-06 17:58:14  阅读:220  来源: 互联网

标签:java android user-interface view handler


这个例子非常简单:我想通过显示文本(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和一切,它不会改变一件事,它只显示最后一次更新.

解决方法:

我还没有尝试过,但我认为通常的模式是在后台线程中进行冗长的初始化,并使用Handler.post()来更新UI.有关不同但可能相关的示例,请参阅http://developer.android.com/reference/android/widget/ProgressBar.html.

另见this answer,尤其是第一段:

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.为什么不能在后台线程中加载资源?

标签:java,android,user-interface,view,handler
来源: https://codeday.me/bug/20190606/1189029.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有