新聞中心
這篇文章主要為大家展示了“android開發(fā)中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“android開發(fā)中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析”這篇文章吧。
成都創(chuàng)新互聯(lián)公司專注于站前企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城建設(shè)。站前網(wǎng)站建設(shè)公司,為站前等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
一、onTouch
onTouch是View中OnTouchListener接口中的方法,處理View及其子類被touch是的事件處理。當(dāng)然,前提是touch時(shí)間能夠傳遞到指定的view。Q1:為什么會(huì)傳遞不到呢?
/** * 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中定義的一個(gè)方法。處理傳遞到view 的手勢(shì)事件。手勢(shì)事件類型包括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方法被調(diào)用,并返回true則這個(gè)手勢(shì)事件就結(jié)束了,并不會(huì)向下傳遞到子控件。Q2:onTouchEvent什么時(shí)候被調(diào)用呢?
三、onInterceptTouchEvent
onInterceptTouchEvent是在ViewGroup里面定義的。Android中的layout布局類一般都是繼承此類的。onInterceptTouchEvent是用于攔截手勢(shì)事件的,每個(gè)手勢(shì)事件都會(huì)先調(diào)用onInterceptTouchEvent。
public boolean onInterceptTouchEvent(MotionEvent ev) { return false; }
此方法返回false,則手勢(shì)事件會(huì)向子控件傳遞;返回true,則調(diào)用onTouchEvent方法。
以上是“android開發(fā)中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章題目:android開發(fā)中onInterceptTouchEvent、onTouchEvent與onTouch的示例分析
網(wǎng)頁網(wǎng)址:http://www.ef60e0e.cn/article/jhisgo.html