我想延迟两秒钟。并且我每2秒钟就要更改一次文本,为此,我正在使用这样的处理程序,但是它没有工作它只显示你好。它根本没有改变它只显示我写的第二个。代码是这样的,
private Handler handler = new Handler();
int i=5;
private TextView textView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.hello);
textView.setText("Android Things !!");
hello_first.run();
}
private Runnable hello_first = new Runnable() {
@Override
public void run() {
textView.setText("Nice Work");
handler.postDelayed(this,5000);
textView.setText("Hello");
handler.postDelayed(this,2000);
i = i+1;
if(i==5)
handler.removeCallbacks(this);
}
};
答案 0 :(得分:1)
您错误地使用了postDelayed
。您希望它的工作方式与Thread.sleep
的工作方式相同。然而事实并非如此。
以下是您要实现的目标的正确实现:
private Handler handler = new Handler();
private TextView textView;
private int i = 0;
private boolean flip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.hello);
handler.post(hello_first);
}
private Runnable hello_first = new Runnable() {
@Override
public void run() {
if(++i == 5) {
handler.removeCallbacks(this);
return;
}
if(flip) {
textView.setText("Nice Work");
} else {
textView.setText("Hello");
}
flip = !flip;
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(2));
}
};
答案 1 :(得分:0)
它易于使用,如
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText("Hello");
},2000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
textView.setText("Nice Work");},5000);
答案 2 :(得分:0)
试试这段代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.hello);
textView.setText("Android Things !!");
handler.postDelayed(hello_first,5000);
handler.postDelayed(hello_second,2000);
}
private Runnable hello_first = new Runnable() {
@Override
public void run() {
textView.setText("Nice Work");
}
};
private Runnable hello_second = new Runnable() {
@Override
public void run() {
textView.setText("Hello");
}
};
答案 3 :(得分:0)
我希望这对你有用。
Handler handler = new Handler();
Runnable task = new Runnable() {
@Override
public void run() {
textView.setText("Nice Work");
handler.postDelayed(this,2000);
textView.setText("Hello");
handler.postDelayed(this,2000);
}
};
task.run();
停止任务
handler.removeCallbacks(task);
答案 4 :(得分:0)
Handler
允许您发送和处理Message和Runnable对象 与线程的MessageQueue相关联。
纠正你的postDelayed
方法。
使Runnable r添加到要运行的消息队列中 经过指定的时间后。
DEMO STRUCTURE
textView.setText("Nice Work");
final Handler handlerOBJ = new Handler();
handlerOBJ.postDelayed(new Runnable() {
@Override
public void run() {
// YOUR WORK
textView.setText("Hello");
}
}, 5000); // 5S delay
答案 5 :(得分:0)
您可以记录并查看实际发生的事情......
每次致电handler.postDelayed(this, 5000);
它将创建两个Runnable实例并将它们发送到处理程序。因此,队列中可运行的数量增长非常快。
您可以设置文本列表和索引,然后将runnable抛出到处理程序并将postDealyed抛出为2000毫秒。使用文本列表和索引查看应该为textview设置的文本。