更新的TextView从功能的Andr​​oid

更新的TextView从功能的Andr​​oid

本文介绍了更新的TextView从功能的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我如何更新从功能的控制的TextView的Andr​​oid?我已经深深的搜索到互联网,看到许多人谁问同样的问题,我测试的线程,但无法正常工作,有人有这样做的一个简单的工作的例子?例如调用一个函数(运行几次循环)和函数在TextView中写道,但问题是,直到功能没有运行完毕,它让我的文字。

someone can tell me how to update a control Textview Android from a function? I have searched deep into the internet and see many people who ask the same question, I tested threads but could not work, someone has a simple working example of doing this? for example to call a function (which runs several times in a loop) and the function writes in the TextView, but the problem is that until the function is not finished running, it shows me the text.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    while(condition) //here freezes the UI and the text in textview only shows when the loop ends
    {
        HardWork();
    }

}

public void HardWork() {

    txtProgreso.append("Test Line" + "\n\n");

};

先谢谢了。

推荐答案

如果我理解正确结果
使用AsyncTask的在这里,所以你可以更新onProgressUpdate方法的TextView

If i understood you correctly
Use AsyncTask here, So you can update textview in onProgressUpdate method

private class SomeTask extends AsyncTask<String, String, String> {
    @Override
        protected String doInBackground(String... params) {
         while(condition) {
         //do more
         publishProgress("Some text value to you textview");
        }
         return null;
     }

     @Override
     protected void onProgressUpdate(String... values) {
         txtProgreso.append(values[0]);
     }


 }

这篇关于更新的TextView从功能的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:22