我正在使用两个按钮... 1很慢而其他更慢...当我点击其中一个按钮它工作正常...现在当我点击另一个按钮时,前面的功能是同时运行...我想要一个(已经在运行)的方法停止点击第二个......
slow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setVisibilityslow1();
}
});
slower.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setVisibilityslower1();
}
});
}
private void setVisibilityslow1() {
tv1.postDelayed(new Runnable() {
@Override
public void run() {
setVisibilityslow2();
}
}, 500);
}
private void setVisibilityslow2() {
tv1.postDelayed(new Runnable() {
@Override
public void run() {
setVisibilityslow1();
}
}, 500);
}
private void setVisibilityslower1() {
tv1.postDelayed(new Runnable() {
@Override
public void run() {
setVisibilityslower2();
}
}, 1000);
}
private void setVisibilityslower2() {
tv1.postDelayed(new Runnable() {
@Override
public void run() {
setVisibilityslower1();
}
}, 1000);
}
答案 0 :(得分:1)
通常,您希望避免关闭线程。有时候需要和必要。
而不是使用匿名类
query.where(schedule.get(Schedule_.assignedAgents)
.in(cb.parameter(List.class, "agentsToMatch" );
然后在它上面运行shutdown。
答案 1 :(得分:0)
您发布的代码不足以决定,但我想您要找的是Handler.removeCallbacks()
答案 2 :(得分:0)
在这里你可以实现你想要的使用下面的代码
首先将您的 onCreate
初始化private Handler visiablityHandler;
private Runnable mySlowRunnable;
然后用这个替换你的
visiablityHandler = new Handler();
slow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setVisibilityslowAndSlower(500);
}
});
slower.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setVisibilityslowAndSlower(1000);
}
});
}
private void setVisibilityslowAndSlower(final int delayMillis) {
if(mySlowRunnable != null)
visiablityHandler.removeCallbacks(mySlowRunnable);
mySlowRunnable = new Runnable() {
public void run() {
//here you can put what you want to do with your tv1
visiablityHandler.postDelayed(this , delayMillis);
}
};
visiablityHandler.post(mySlowRunnable);
}
@Override
protected void onStop() {
super.onStop();
if(mySlowRunnable != null)
visiablityHandler.removeCallbacks(mySlowRunnable);
}
希望这会对您有所帮助,如果它没有用,请告诉我
答案 3 :(得分:0)
您可以使用以下代码来停止您的主题:
tv1.removeCallbacksAndMessages(null); // where tv1 is your Handler
如果这对您没有用,那么您可以停止特定的线程,就像您运行这样的线程一样:
Handler handler= new Handler()
Runnable myRunnable = new Runnable() {
public void run() {
// do something
}
};
handler.postDelayed(myRunnable);
然后你就可以这样阻止它:
handler.removeCallbacks(myRunnable);