gpt4 book ai didi

java - 为什么我的处理程序没有执行?

转载 作者:行者123 更新时间:2023-12-01 12:05:29 25 4
gpt4 key购买 nike

我有以下代码:

   int x=0; 
private void startTimerThread() {
System.out.println("enter");
System.out.println("percentage"+percentage);
System.out.println("x"+x);

final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
for (x = 0; x>= percentage; x++ ) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable(){
public void run() {
textpercentage.animate(x, x++);
System.out.println("enter"+x);
}
});
}
}
};
new Thread(runnable).start();
}

我正在尝试使用及时的 TextView 对 TextView 上的数字进行动画处理,但是当我从 onCreate 之外的代码中调用 startTimerThread 时,我既不会显示要显示的 TextView ,也不会执行 system.out。我在这里想念什么?

最佳答案

尝试这样

Handler handler = new Handler();
int delay=1000;
Runnable rann=new Runnable() {
@Override
public void run() {
//Write Your logic here which you want to perform periodically
System.out.println("Handler is running : ");
//to call the same thread repeatedly calling handler again
handler.postDelayed(rann, delay);
}
};


private void startHandler() {
//here the handler will executes the rannable after that particulary delay milli seconds
handler.postDelayed(rann, delay);
}

private void stopHandler() {
handler.removeCallbacks(rann);
}

关于java - 为什么我的处理程序没有执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com