免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 702 | 回复: 0

看代码学Android :如何处理Touch? [复制链接]

论坛徽章:
0
发表于 2011-12-23 02:09 |显示全部楼层
  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. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP