刚学习Android中的Threads(我对Threads时期也有点新意)。我正在查看其他各种帖子,但仍需要一点澄清。我用HelloWorld保持简单。我想要的是显示消息:“Hello World这是一个线程”,每个单词以1秒的间隔显示。想到使用带有消息的字符串数组。然后在线程中使用for循环来遍历每个元素。我很确定我理解我的问题的逻辑 - 因为整个循环在延迟时间帧内执行。任何人都可以告诉我如何解决这个问题,这样我就能以1秒的间隔显示每个元素吗?这是我的代码:
public class HelloWorld extends Activity {
Handler m_handler;
Runnable m_handlerTask ;
private TextView hello;
private String[] HelloWorld = {
"Hello",
"World",
"This",
"Is",
"A",
"Thread",
};
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world);
hello = (TextView) findViewById(R.id.hello);
m_handler = new Handler();
m_handlerTask = new Runnable()
{
@Override
public void run() {
if(i<HelloWorld.length-1)
{
hello.append(HelloWorld[i]);
hello.setText("\n");
i++;
}
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
m_handler.removeCallbacks(m_handlerTask);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world, menu);
return true;
}
}
答案 0 :(得分:1)
使用线程
Thread thread;
private String[] HelloWorld = {
"Hello",
"World",
"This",
"Is",
"A",
"Thread",
};
int i=0;
在你的onCreate()
中 thread = new Thread(new Runnable(){
@Override
public void run(){
for( i = 0; i < HelloWorld.length; i++){
runOnUiThread(new Runnable(){
@Override
public void run(){
hello.append(HelloWorld[i]);
hell0.append(" ");
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
thread.start();
我建议你使用Handler
Handler m_handler;
Runnable m_handlerTask ;
private String[] HelloWorld = {
"Hello",
"World",
"This",
"Is",
"A",
"Thread",
};
int i=0;
在你的onCreate()
中 m_handlerTask = new Runnable()
{
@Override
public void run() {
if(i<HelloWorld.length)
{
hello.append(HelloWorld[i]);
hello.append(" ");
i++;
}
else
{
m_handler.removeCallbacks(m_handlerTask);
}
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
如果不需要,请停止运行
m_handler.removeCallbacks(m_handlerTask);
答案 1 :(得分:1)
对于初学者来说,你没有在这里使用任何线程,如果你想使用一个线程就会是这样的。
Thread t = new Thread(new Runnable(){
@Override
public void run(){
for(int i = 0; i < HelloWorld.length; i++){
hello.append(HelloWorld[i]);
Thread.sleep(1000); //wait one second
}
}
});
t.start();
但是,在Android中,您无法从后台线程更新UI。因此,您需要在UI线程上实际运行更新,Activity
中有一个名为runOnUiThread
的辅助方法
Thread t = new Thread(new Runnable(){
@Override
public void run(){
for(int i = 0; i < HelloWorld.length; i++){
runOnUiThread(new Runnable(){
@Override
public void run(){
hello.append(HelloWorld[i]);
}
});
Thread.sleep(1000); //wait one second
}
}
});
t.start();
答案 2 :(得分:1)
虽然使用普通Java Threads
是好的,但是当您必须修改UI时,使用AsyncTasks
您可以这样写AsyncTask
:
public class Task extends AsyncTask<String, String, Void> {
private final TextView textView;
public Task(TextView textView) {
this.textView = textView;
}
protected void onProgressUpdate(String... update) {
textView.append(update[0]);
}
protected Void doInBackground(String... params) {
for (String param : params) {
publishProgress(param);
try {
Thread.sleep(1000)
} catch (InterruptedException e) {
}
}
return null;
}
}
你在Activity
中使用它就像这样:
Task task = new Task(hello);
task.execute("Hello", "world", "this", "is", "a", "thread");