English 中文(简体)
如何在安乐施会时重读文字吗?
原标题:How to refresh a TextView while looping in Android?

我有:

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


    TextView debugView = (TextView)findViewById(R.id.debugView);

    for(int i=0;i<100;i++) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        debugView.setText(Integer.toString(i));
    }

}

I would expect it to update the text view each time through the loop so I would get: 1, 2, 3, ... 99

相反,这只是10秒钟,然后是产出: 99

我猜测,我需要书面意见在座席上复读。 我是新事物。 有些人能否告诉我,这是实现这一目标的最佳途径?

我最终的目标是能够处理音频样本,以建立一个gui子辅导员。 我试图直截了当地核实,看上看看看看看看看镜是回应外部声音的,我希望更新文字,以证明这一点。 请建议有更好的办法这样做。

最佳回答

你们的问题是,你正在休庭。 这是在你的活动展示之前,在“国际调查”的透镜上进行的。

Take a look at the Activity lifecycle in Android. It shows onCreate is just one of the stages in the Activity creation. What you ll want to do is run the loop on a background thread and post the notification to update the TextView onto the UI thread using runOnUiThread. Christian s answer shows how to do this.

然而,鉴于您的最终目标是,根据某些用户投入(如果是你的视听材料),更新文本意见,你一定要通过所有这些渠道跳跃工作。 最重要的事情是,你把更新《国际调查》的呼吁寄给你,否则你会例外。

问题回答

我有同样的问题,我用手来解决这个问题:

public class myClass extends Activity{

 private Handler mHandler;
 private TextView text;
 private int i;

    @Override
      public void onCreate(Bundle savedInstanceState) {
          text = (TextView) findViewById(R.id.textView01);
          i = 0;
          mHandler = new Handler();
          mHandler.post(mUpdate);

      }

private Runnable mUpdate = new Runnable() {
   public void run() {

       text.setText("My number: " + i);
       i++;
       mHandler.postDelayed(this, 1000);

    }
};}

<代码>mHandler.post(mUpdate), 您称:mUpdate<>code>, 该编码是可操作的,可作不同校对,并可更新国际交易日志。 在方法<代码>run()中,你将不产生与你同样的问题,而是确定你想要的辅助变量案文,加起来,并打电话mHandler.postDelayed(这一,更新时间)。 此时此刻,意见更新,你在屏幕上有案文,在你指定的时间(每秒)之后,将重新命名可操作的日志,并定下号。

我希望它能够提供帮助。

• 更好地使用时间:

    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                public void run() {
                    debugView.setText("something");
                }
            });
        }
    }, 0, 100);

使用<条码>的Im通知 既然你可以从另一个主线修改观点。

不能使用残疾者,而是可以直接通过,以观察所:

public class myClass extends Activity {

    private TextView text;
    private int i;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        text = (TextView) findViewById(R.id.textView01);
        text.postDelayed(mUpdate, 0);

    }

    private Runnable mUpdate = new Runnable() {
        public void run() {

            text.setText("My number: " + i);
            i++;
            text.postDelayed(this, 1000);

        }
    };
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签