如何在另一个线程设置的文本视图文本 [英] How to set text of text view in another thread

查看:106
本文介绍了如何在另一个线程设置的文本视图文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特林的setText在另一个线程,也就是子线程。但以下code,它给错误

  

只有创建视图层次可以触摸其观点原来的线程。

 

 

 公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    IMG =(ImageView的)findViewById(R.id.img);
    PB =(进度)findViewById(R.id.pb);
    this.tv =(TextView中)findViewById(R.id.perc);
    tv.setText(30%);
    pb.setProgress(30);
    pb.setMax(100);
}

公共无效集(INT P)
{
    tv.setText的(p +%);
}

保护无效的OnStart()
{
    super.onStart();
    pb.setProgress(20);

    线程t =新主题(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
            尝试 {
                INT I = pb.getProgress();
                而(ⅰ&小于100)
                {
                    而(pb.getProgress()&小于100)
                    {
                        pb.incrementProgressBy(5);
                        视频下载(1000);
                    }
                I + = 10;
                pb.setProgress(ⅰ);
                Thread.interrupted();
                集(一);
                }
            }
            赶上(InterruptedException的E)
            {
                e.printStackTrace();
            }
        }
    });
    t.start();
}
 

解决方案

您需要参考该TextView的,然后做:

  textView.post(新的Runnable(){
    公共无效的run(){
        textView.setText(yourText);
    }
}
 

I am tring to setText in another thread, that is, child thread. But for the following code, it is giving the error

Only the original thread that created a view hierarchy can touch its views.

 

 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    img = (ImageView)findViewById(R.id.img);
    pb = (ProgressBar)findViewById(R.id.pb);
    this.tv = (TextView)findViewById(R.id.perc);
    tv.setText("30 %");
    pb.setProgress(30);
    pb.setMax(100);
}

public void set(int p)
{
    tv.setText(p + " %");
}

protected void onStart()
{
    super.onStart();
    pb.setProgress(20);

    Thread t = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try {
                int i = pb.getProgress();
                while(i <100)
                {
                    while(pb.getProgress()<100)
                    {
                        pb.incrementProgressBy(5);
                        Thread.sleep(1000);
                    }
                i+=10;
                pb.setProgress(i);
                Thread.interrupted();
                set(i);
                }
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    });
    t.start();
}

解决方案

You need the reference to that textview and then do:

textView.post(new Runnable() {
    public void run() {
        textView.setText(yourText);
    } 
}

这篇关于如何在另一个线程设置的文本视图文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆