這篇文章主要為大家展示了“android開發中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“android開發中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析”這篇文章吧。
成都創新互聯公司專注于站前企業網站建設,響應式網站建設,商城建設。站前網站建設公司,為站前等地區提供建站服務。全流程按需網站建設,專業設計,全程項目跟蹤,成都創新互聯公司專業和態度為您提供的服務
一、onTouch
onTouch是View中OnTouchListener接口中的方法,處理View及其子類被touch是的事件處理。當然,前提是touch時間能夠傳遞到指定的view。Q1:為什么會傳遞不到呢?
/** * Interface definition for a callback to be invoked when a touch event is * dispatched to this view. The callback will be invoked before the touch * event is given to the view. */ public interface OnTouchListener { /** * Called when a touch event is dispatched to a view. This allows listeners to * get a chance to respond before the target view. * * @param v The view the touch event has been dispatched to. * @param event The MotionEvent object containing full information about * the event. * @return True if the listener has consumed the event, false otherwise. */ boolean onTouch(View v, MotionEvent event); }
二、onTouchEvent
onTouchEvent同樣也是在view中定義的一個方法。處理傳遞到view 的手勢事件。手勢事件類型包括ACTION_DOWN,ACTION_MOVE,ACTION_UP,ACTION_CANCEL四種事件。
/** * Implement this method to handle touch screen motion events. * * @param event The motion event. * @return True if the event was handled, false otherwise. */ public boolean onTouchEvent(MotionEvent event) { …… …… }
一旦onTouchEvent方法被調用,并返回true則這個手勢事件就結束了,并不會向下傳遞到子控件。Q2:onTouchEvent什么時候被調用呢?
三、onInterceptTouchEvent
onInterceptTouchEvent是在ViewGroup里面定義的。Android中的layout布局類一般都是繼承此類的。onInterceptTouchEvent是用于攔截手勢事件的,每個手勢事件都會先調用onInterceptTouchEvent。
public boolean onInterceptTouchEvent(MotionEvent ev) { return false; }
此方法返回false,則手勢事件會向子控件傳遞;返回true,則調用onTouchEvent方法。
以上是“android開發中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯行業資訊頻道!
本文標題:android開發中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析
分享URL:http://vcdvsql.cn/article10/jhisgo.html
成都網站建設公司_創新互聯,為您提供建站公司、標簽優化、App開發、網站制作、小程序開發、軟件開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯