Chinaunix

标题: 看代码学Android :如何处理Touch? [打印本页]

作者: zdwang    时间: 2011-12-23 02:09
标题: 看代码学Android :如何处理Touch?
  1. public class test2 extends Activity implements OnTouchListener, OnClickListener{
  2.     /** Called when the activity is first created. */
  3.     
  4.     GestureDetector mGestureDetector;
  5.     @Override
  6.     public void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.         
  10.         Button btn = (Button)this.findViewById(R.id.button1);
  11.         btn.setOnTouchListener(this);
  12.         btn.setOnClickListener(this);
  13.        
  14.         //创建一个手势dectector
  15.         mGestureDetector = new GestureDetector(this, new CalendarGestureListener());
  16.     }

  17.     @Override
  18.     public boolean onTouch(View v, MotionEvent event) {
  19.         if (mGestureDetector.onTouchEvent(event)) {
  20.             return true;
  21.         }
  22.         return super.onTouchEvent(event);
  23.     }
  24.     
  25.     
  26.      class CalendarGestureListener extends GestureDetector.SimpleOnGestureListener {

  27.         /* (non-Javadoc)
  28.          * @see android.view.GestureDetector.SimpleOnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
  29.          */
  30.         @Override
  31.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  32.                 float velocityY) {
  33.             Log.v("touch", "x=" + Float.toString(e1.getX()) + " x2=" + Float.toString(e2.getX()) );
  34.             // TODO Auto-generated method stub
  35.             return super.onFling(e1, e2, velocityX, velocityY);
  36.         }
  37.      }


  38.     @Override
  39.     public void onClick(View arg0) {
  40.         // TODO Auto-generated method stub
  41.         Log.v("touch", "Hello, Click Me" );

  42.     }
  43. }





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2