반응형
OnTouchListener의 Ontouch 이벤트가 Android에서 두 번 호출됩니다.
런타임에 주어진 두 지점 사이에 선이 생성되는 응용 프로그램을 만들고 있습니다.
내가 보는 문제는 onTouch()
시뮬레이터를 클릭 할 때마다 두 번 호출 된다는 것 입니다. 두 가지 동작 ( ACTION_DOWN
& ACTION_UP
)이 확인 되었음을 알고 있습니다. 하지만 내 앱이 onTouch()
한 번만 호출하기를 원합니다 . 제게 아이디어를주세요. 이것은 내가 사용한 코드입니다.
SurfaceView surfaceview = new SurfaceView(getContext());
SurfaceHolder h = surfaceview.getHolder();
int action = event.getActionMasked();
synchronized(h) {
if (action == MotionEvent.ACTION_DOWN && action!=MotionEvent.ACTION_CANCEL)// && flag==true)
{
Log.d("TouchView","ACTION_DOWN ");
Point pointer = new Point();
pointer.x = (int) event.getX();
pointer.y = (int) event.getY();
touchPoint.add(pointer);
view.invalidate();
Log.d("MotionEvent.ACTION_DOWN", "point: " + pointer);
action = MotionEvent.ACTION_CANCEL;
flag = false;
}
else if(action == MotionEvent.ACTION_UP && action!=MotionEvent.ACTION_CANCEL)// && flag==true)
{
Log.d("TouchView","ACTION_UP");
Point pointer = new Point();
pointer.x = (int) event.getX();
pointer.y = (int) event.getY();
touchPoint.add(pointer);
view.invalidate();
Log.d("MotionEvent.ACTION_UP", "point: " + pointer);
action = MotionEvent.ACTION_CANCEL;
flag = false;
}
else return false;
}
touchListener는 모든 요구 될 것이다 MotionEvent.ACTION_DOWN
, MotionEvent.ACTION_UP
그리고 MotionEvent.ACTION_MOVE
. 따라서 코드를 한 번만 실행하려는 경우, 즉 MotionEvent.ACTION_DOWN
내부
onTouch()
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//your code
}
또는 onClickListener를 사용하십시오.
myButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
//do what you gotta do
}
});
반응형
'code' 카테고리의 다른 글
-webkit-animation을 사용하여 맥박 효과를 만드는 방법-바깥 쪽 고리 (0) | 2020.12.03 |
---|---|
DataRowCollection을 IEnumerable로 변환 (0) | 2020.12.03 |
네임 스페이스 이름을 바꾼 후 Main 메서드에 지정된 'WindowsFormsApplication1.Program'을 찾을 수 없습니다. (0) | 2020.12.03 |
PHP : 중첩 루프 끊기 (0) | 2020.12.03 |
Jasmine의 toHaveBeenCalledWith 매처를 정규 표현식과 함께 사용할 수 있습니까? (0) | 2020.12.03 |