2011-09-30 3 views
0

Android에서 MotionEvent를 실행 한 UI 컨트롤을 식별하려고합니다. 나는몇 개의 UI 컨트롤에 대한 단일 DoubleTapDetector

private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener {  
    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
     String uiControlName = obtainUiControlName(e); 
     // Do something depends on uiControlName 
     return true; 
    }   

    private String obtainUiControlName(MotionEvent e) { 
     int deviceId = e.getDeviceId(); 
     switch (deviceId) { 
      case R.id.button1: return "Button1"; 
      case R.id.button2: return "Button2"; 
     } 
     return null; 
    }   
} 

이 두 버튼에 배치로

Button button1 = (Button) findViewById(R.id.button1); 
    outcomeButton.setOnTouchListener(new OnTouchListener(){ 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      doubleTapDetector.onTouchEvent(event); 
      return true; 
     } 
    });   

Button button2 = (Button) findViewById(R.id.button2); 
    outcomeButton.setOnTouchListener(new OnTouchListener(){ 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      doubleTapDetector.onTouchEvent(event); 
      return true; 
     } 
    }); 

문제는 DeviceID가 항상 0에 해당된다는 내가 버튼의 불을 식별 할 수없는 일 doubleTapDetector

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    doubleTapDetector = new GestureDetector(this, new DoubleTapDetector()); 
} 

선언이 더블 클릭 이벤트. 각 버튼마다 두 개의 서로 다른 doubleTapDetector를 구현하지 않고이를 수행 할 수있는 방법이 있습니까?

답변

0

실수. getDeviceId는 터치를 수신 한 곳에서 물리적 포인터 디바이스 ID를 반환합니다. Widget ID가 아닙니다.

유일한 방법은 버튼마다 고유 한 DoubleTapDetector를 만들고 DoubleTapDetector 클래스에 버튼의 ID를 저장하는 것입니다. 왜냐하면 onDoubleTap 메서드는 탭이 발생한 위젯에 대한 충분한 정보를 제공하지 않기 때문입니다. 이처럼

는 : - DoubleTap 후 ** 항상 ** 길게 누르가 해고 그것은 당신이 그런데 ... 맞아 보인다

private class DoubleTapDetector extends GestureDetector.SimpleOnGestureListener { 
    int id; 
    DoubleTapDetector(int id){ 
     this.id = id; 
    } 
... 
+0

, 나는 onLongPress과 onDoubleTap 사이에 간섭이있다! [보세요, plz!] (http://stackoverflow.com/questions/7606921/why-android-onlongpress-always-is-fire-after-ondoubletap) 이유는 무엇입니까? – isabsent