自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談onInterceptTouchEvent、onTouchEvent與onTouch

移動開發(fā) Android
本文為Android開發(fā)詳解onInterceptTouchEvent函數(shù)實(shí)現(xiàn)分派各項(xiàng)功能的調(diào)用以實(shí)現(xiàn)各項(xiàng)任務(wù)。

一、onTouch

onTouch是View中OnTouchListener接口中的方法,處理View及其子類被touch是的事件處理。當(dāng)然,前提是touch時間能夠傳遞到指定的view。Q1:為什么會傳遞不到呢?

  1.   /**     
  2.     * Interface definition for a callback to be invoked when a touch event is    
  3.      * dispatched to this view. The callback will be invoked before the touch     
  4.       * event is given to the view.     
  5.       */     
  6.      public interface OnTouchListener {     
  7.          /**     
  8.          * Called when a touch event is dispatched to a view. This allows listeners to    
  9.          * get a chance to respond before the target view.    
  10.          *    
  11.          * @param v The view the touch event has been dispatched to.    
  12.          * @param event The MotionEvent object containing full information about    
  13.          *        the event.    
  14.           * @return True if the listener has consumed the event, false otherwise.    
  15.          */    
  16.          boolean onTouch(View v, MotionEvent event);    
  17.     }  

二、onTouchEvent

onTouchEvent同樣也是在view中定義的一個方法。處理傳遞到view 的手勢事件。手勢事件類型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四種事件。

  1. /**     
  2. * Implement this method to handle touch screen motion events.     
  3.  *     
  4.  * @param event The motion event.     
  5.  * @return True if the event was handled, false otherwise.     
  6.  */     
  7. public boolean onTouchEvent(MotionEvent event) {     
  8.  ……     
  9.  ……    

一旦onTouchEvent方法被調(diào)用,并返回true則這個手勢事件就結(jié)束了,并不會向下傳遞到子控件。Q2:onTouchEvent什么時候被調(diào)用呢?

三、onInterceptTouchEvent

onInterceptTouchEvent是在ViewGroup里面定義的。Android中的layout布局類一般都是繼承此類的。onInterceptTouchEvent是用于攔截手勢事件的,每個手勢事件都會先調(diào)用onInterceptTouchEvent。

  1.  
  2. public boolean onInterceptTouchEvent(MotionEvent ev) {     
  3.         return false;     
  4.  } 

此方法返回false,則手勢事件會向子控件傳遞;返回true,則調(diào)用onTouchEvent方法。

 原文鏈接:http://www.cnblogs.com/halzhang/archive/2010/08/18/1802611.html

【編輯推薦】

  1. 在Ubuntu下建立Eclipse的Android開發(fā)環(huán)境
  2. Intent,Android應(yīng)用核心探秘
  3. Adobe AIR即將登陸Android平臺
責(zé)任編輯:張攀 來源: cnblogs
相關(guān)推薦

2012-12-21 10:15:35

2012-12-21 09:48:06

JavaJavaSE異常

2009-07-14 13:49:29

原型

2012-02-14 09:43:08

2011-02-16 09:48:04

Hashtable

2013-11-12 14:11:10

2018-02-23 14:44:41

負(fù)載均衡技術(shù)分類

2009-03-11 15:30:05

evalwithJavascript

2011-04-18 00:00:00

SQLPLSQL

2021-07-14 13:12:51

2009-09-21 17:30:25

組件復(fù)用服務(wù)復(fù)用

2009-07-10 13:09:53

.Net與Java S

2009-06-26 16:09:53

2010-09-13 14:34:55

2013-01-08 15:11:19

OpenStackKVM

2021-08-29 23:25:36

前端開發(fā)工具

2011-06-30 17:48:42

SEOSEM

2009-07-09 15:40:54

Java ServleCGI程序

2011-09-06 09:27:15

項(xiàng)目設(shè)計

2009-06-24 17:05:10

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號