메인 UI 스레드를 멈추지 않고 2 초 이상 화면을 터치했는지 확인하는 최선의 전략은 어느 것입니까?화면이 2 초 이상 터치되었는지 확인하는 방법
일부 샘플 코드를 확인했지만이를 달성하는 데 가장 적합한 방법이 무엇인지 잘 모르겠습니다. 또한 기본 UI 스레드를 중지하지 않고도 수행해야합니다.
감사
메인 UI 스레드를 멈추지 않고 2 초 이상 화면을 터치했는지 확인하는 최선의 전략은 어느 것입니까?화면이 2 초 이상 터치되었는지 확인하는 방법
일부 샘플 코드를 확인했지만이를 달성하는 데 가장 적합한 방법이 무엇인지 잘 모르겠습니다. 또한 기본 UI 스레드를 중지하지 않고도 수행해야합니다.
감사
당신은이 같은 OnTouchListener
을 구현할 수 있습니다
public abstract class TouchTimer implements View.OnTouchListener {
private long touchStart = 0l;
private long touchEnd = 0l;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.touchStart = System.currentTimeMillis();
return true;
case MotionEvent.ACTION_UP:
this.touchEnd = System.currentTimeMillis();
long touchTime = this.touchEnd - this.touchStart;
onTouchEnded(touchTime);
return true;
case MotionEvent.ACTION_MOVE:
return true;
default:
return false;
}
}
protected abstract void onTouchEnded(long touchTimeInMillis);
}
당신은 다음과 같이 사용합니다 : 터치가 끝나면
view.setOnTouchListener(new TouchTimer() {
@Override
protected void onTouchEnded(long touchTimeInMillis) {
// touchTimeInMillis contains the time the touch lasted in milliseconds
}
});
방법 onTouchEnded()
가 호출됩니다.
Android 코드의 모든 제스처 감지 부분은 내부적으로 Handler 클래스와 postDelayed 메소드의 조합을 사용합니다. 임의의 시간 (예 : 2 초) 후에 실행할 코드 조각을 게시하는 데 사용할 수 있습니다.
2 초 대기를 제거 할 경우
Runnable runnable;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
runnable = new Runnable() {
@Override
public void run() {
//do something
}
};
getHandler().postDelayed(runnable, 2000);
} else if (ev.getAction() == MotionEvent.ACTION_UP
|| ev.getAction() == MotionEvent.ACTION_CANCEL) {
getHandler().removeCallbacks(runnable);
}
return super.dispatchTouchEvent(ev);
}
참조
를 실행하려면 , long)나는 RelativeLayout
에서 OnTouchListener
을 사용했습니다. 이게 당신을 도울 수 있기를 바랍니다.
RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
ll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration >= MIN_CLICK_DURATION) {
Toast.makeText(MainActivity.this, "TOUCHED FOR" + clickDuration + "MS", Toast.LENGTH_SHORT).show();
}
longClickActive = false;
break;
case MotionEvent.ACTION_DOWN:
if (longClickActive == false) {
longClickActive = true;
Toast.makeText(MainActivity.this, "touch!", Toast.LENGTH_SHORT).show();
startClickTime = Calendar.getInstance().getTimeInMillis();
}
break;
case MotionEvent.ACTION_MOVE:
if (longClickActive == true) {
longClickActive = false;
}
break;
}
return true;
}
});
이 솔루션은 실제로 그렇게 좋지 않습니다.당신은 항상 사실로 돌아갑니다. 실제로이 경우 작동 할 수도 있지만 여전히 똑똑하지는 않습니다. 실제로 관심있는 터치 이벤트 만 소비하기를 원하므로'MotionEvent'가'ACTION_UP','ACTION_DOWN' 또는'ACTION_MOVE' 일 때만 true를 리턴해야합니다. 다른 경우에는 false를 반환해야합니다. 자세한 내용은 내 대답을 참조하십시오. 또한이 간단한 사용 사례에서는 예제가 지나치게 복잡해 보입니다. –
그것은 단순히 ontouch listner입니다. 그것을 보완하지 못했습니다. 그것은 잘 작동 테스트 중입니다 –
그래, 잘 작동하지만 귀하의 예제 불필요한 검사 및 논리가 많이 필요하지 않습니다. 예를 들어'MIN_CLICK_DURATION' 또는'longClickActive'에 대한 검사. 이러한 기능을 구현할 수도 있지만 AndroidUser99에서 요구 한 기능이 아닌 것으로 알고 있습니다. 어쨌든 @ AndroidUser99는 그가 가장 좋아하는 답변을 결정합니다. –
안녕하세요, 코드가 멋지지만 그 onTouch (long timeInMillis) 메소드로 어떻게해야합니까? MainActivity 타입의 onTouch 추상 메서드는 추상 클래스에 의해서만 정의 될 수 있습니다 – NullPointerException
내 대답의 맨 아래에있는'OnTouchListener' 사용법을 보여줍니다. 새로운'TouchTimer' 인스턴스를 생성 할 때'onTouch' 메소드를 구현합니다. –
마지막 코드는 사용 방법을 보여줍니다. – FabianCook