Android雙擊處理
這次要繼續介紹Gesture類,這個類提供了一個事件處理
public boolean onDoubleTapEvent(MotionEvent e) {}
好的讓我們快速進入這個主題吧,方法很簡單,只需要將Activity的onTouch()事件交給Guesture來處理就可以了。
代碼:
public class MyGesture extends Activity implements OnTouchListener { private GestureDetector mGestureDetector; public MyGesture() { mGestureDetector = new GestureDetector(new MySimpleGesture()); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { Log.i("MyGesture", "MotionEvent.ACTION_UP"); } return mGestureDetector.onTouchEvent(event); } private class MySimpleGesture extends SimpleOnGestureListener { // 按兩下的第二下Touch down時觸發 public boolean onDoubleTap(MotionEvent e) { Log.i("MyGesture", "onDoubleTap"); return super.onDoubleTap(e); } // 按兩下的第二下Touch down和up都會觸發,可用e.getAction()區分 public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return super.onDoubleTapEvent(e); } } } |
沒有留言:
張貼留言