【发布时间】:2017-01-31 01:49:14
【问题描述】:
我有一个 for 循环,我想在其中调用 setText 方法。除了在脚本完成运行之前文本不会更改的事实之外,这可以正常工作。我想要它做的是每次调用setText 方法时更改代码。我在下面附上了我的代码,它应该非常困难。
代码:
package com.example.zoe.andone;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startLoop(View view) {
String string = getString(R.string.hello);
((TextView)findViewById(R.id.hello)).setText("starting...");
for (int i = 0; i < 10; i++) {
String updated = Integer.toString(i);
((TextView)findViewById(R.id.hello)).setText(updated);
try {
Thread.sleep(250);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
((TextView)findViewById(R.id.hello)).setText("done!");
}
}
【问题讨论】:
-
使用
Handler而不是在主线程上调用Thread.sleep(250); -
我不确定我是否关注。
标签: java android for-loop settext