문제의 일부는 해당 클릭과 관련된 동작이 아직 완료되지 않은 경우에도 UI 버튼을 클릭하면 즉시 반환되거나 대기열에 대기합니다. 이 응답의 범위를 벗어나는 이유 때문에 "일을하는 동안"버튼을 비활성화하는 것이 효과가 없다는 점에 유의해야합니다. 이런 종류의 문제에 대한 몇 가지 해결책이 있습니다 : 하나는 기본 "작업"이 완료된 후에 만 설정되는 부울 플래그를 사용하는 것입니다. 그런 다음 단추 작업 처리기 내에서 플래그를 다시 설정하기 전에 발생한 단추 클릭을 무시합니다.
/**
* Button presses are ignored unless idle.
*/
private void onMyButtonClicked() {
if(idle) {
doWork();
}
}
/**
* Does some work and then restores idle state when finished.
*/
private void doWork() {
idle = false;
// maybe you spin off a worker thread or something else.
// the important thing is that either in that thread's run() or maybe just in the body of
// this doWork() method, you:
idle = true;
}
또 다른 일반적인 옵션은 시간을 사용하여 필터링하는 것입니다. 즉.
/**
* Determines whether or not a button press should be acted upon. Note that this method
* can be used within any interactive widget's onAction method, not just buttons. This kind of
* filtering is necessary due to the way that Android caches button clicks before processing them.
* See http://code.google.com/p/android/issues/detail?id=20073
* @param timestamp timestamp of the button press in question
* @return True if the timing of this button press falls within the specified threshold
*/
public static synchronized boolean validateButtonPress(long timestamp) {
long delta = timestamp - lastButtonPress;
lastButtonPress = timestamp;
return delta > BUTTON_PRESS_THRESHOLD_MS;
}
그런 다음이 같은 것을 할 거라고 : 버튼 누름의 최대 주파수는 1Hz의 어디에 있는지 한계를 설정
private void onMyButtonClicked() {
if(validateButtonPress(System.currentTimeMillis())) {
doWork();
}
}
이 마지막 솔루션으로 인정 하듯이 비 결정적입니다,하지만 당신은 것을 고려하는 경우 사용자는 모바일 장치에서 1 초에 1-2 번 이상 버튼을 의도적으로 클릭하지 않습니다.
충돌에 대한 LogCat에서 코드 및 스택 추적을 게시해야합니다. – kcoppock