这段代码在一个活动中正常运行但在片段中不正常,由于某种原因,当按钮关闭并且它进入runnable时,问题button.ispressed()返回false。我无法理解。
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.post(rotationRunnable);
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
progress.setProgress(0);
}
return false;
}
});
public Runnable rotationRunnable = new Runnable() {
int mProgressStatus = 0;
@Override
public void run() {
try {
if(button.isPressed()) {
if (mProgressStatus < 100) {
mProgressStatus++;
progress.setProgress(mProgressStatus);
button.postDelayed(rotationRunnable, 20);
} else {
sendMessageToService("");
mProgressStatus = 0;
Thread.currentThread().interrupt();
}
} else {
if (mProgressStatus > 0) {
mProgressStatus--;
progress.setProgress(mProgressStatus);
button.postDelayed(rotationRunnable, 20);
if (mProgressStatus == 0) {
Thread.currentThread().interrupt();
}
}
}
}catch(Exception e){
System.out.println(e)
}
}
};
答案 0 :(得分:0)
我找到了解决方案。似乎片段中的按钮有一个小的延迟,所以当我在runnable中询问按钮是否按下时返回false,所以如果我以延迟调用runnable则返回true。这有点奇怪,但有效...
arg0.postDelay(rotationRunnable, 200);