編輯:關於android開發
在安卓四大組件中(Activity、Service、BroadCast、ContentProvider),最常用的當是Activity。因為Activity負責提供直觀的頁面並響應用戶操作。在Activity的布局文件中,通過最外層的ViewGroup(布局)一層層嵌套、布局直至View(控件),組成了豐富多彩的用戶頁面。如QQ、微信等等。在開發這些頁面過程中,難免會遇到一些事件沖突(說人話就是:你想點擊的某個布局或控件,發現響應的是另一個布局或控件)的問題。呵呵,怎麼辦呢?由此引入我們接下來討論的問題。
手機在響應用戶的點擊操作時,從Activity入口,遵循著一定的規則將對應的事件交由指定的ViewGroup或者View去響應(消費這個事件)。只有找出了各中規則,日後再次處理這類事件問題時,必定會得心應手。從程序的角度來看,Android提供三個方法處理事件問題dispatchTouchEvent(分發點擊事件)、onInterceptTouchEvent(攔截點擊事件),onTouchEvent(處理點擊事件)。每個方法返回true或false表示是否處理它對應的職責。比如說,如果我攔截了事件,就表示自身要處理該事件,不讓別的控件再能接收到事件信號。除了返回true或false之外,還能通過調用父類的方法執行父類的邏輯。簡而言之,Activity、ViewGroup、View三個類處理事件,相關的方法依次是dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent以及每個方法的返回可以是true、false和super。其中注意:Activity和View沒有攔截事件方法。
編寫一個測試demo,布局文件中用LinearLayout嵌套RelativeLayout,最後在RelativeLayout中包含一個Button按鈕。依次在每個事件處理方法中,輸出log日志。點擊按鈕測試,通過日志信息分析事件分發流程。下圖是布局效果。
為方便描述,定制命名規則:Activity簡寫A,LinearLayout簡寫L,RelativeLayout簡寫R,Button簡寫B,dispatchTouchEvent簡寫D,onInterceptTouchEvent簡寫I,onTouchEvent簡寫T,返回true簡寫T,返回false簡寫F,返回super簡寫S。如:ADS表示Activity中dispatchTouchEvent方法 return super。
測試方案一:ADS/ATS、LDS/LIS/LTS、RDS/RIS/RTS、BDS/BTS
日志如下:
04-01 12:44:44.402: D/Activity(1234): dispatchTouchEvent ACTION_DOWN 04-01 12:44:44.402: D/MyLinearLayout(1234): dispatchTouchEvent ACTION_DOWN 04-01 12:44:44.402: D/MyLinearLayout(1234): onInterceptTouchEvent ACTION_DOWN 04-01 12:44:44.402: D/MyRelativeLayout(1234): dispatchTouchEvent ACTION_DOWN 04-01 12:44:44.402: D/MyRelativeLayout(1234): onInterceptTouchEvent ACTION_DOWN 04-01 12:44:44.402: D/MyButton(1234): dispatchTouchEvent ACTION_DOWN 04-01 12:44:44.402: D/MyButton(1234): onTouchEvent ACTION_DOWN 04-01 12:44:44.506: D/Activity(1234): dispatchTouchEvent ACTION_UP 04-01 12:44:44.506: D/MyLinearLayout(1234): dispatchTouchEvent ACTION_UP 04-01 12:44:44.506: D/MyLinearLayout(1234): onInterceptTouchEvent ACTION_UP 04-01 12:44:44.506: D/MyRelativeLayout(1234): dispatchTouchEvent ACTION_UP 04-01 12:44:44.506: D/MyRelativeLayout(1234): onInterceptTouchEvent ACTION_UP 04-01 12:44:44.506: D/MyButton(1234): dispatchTouchEvent ACTION_UP 04-01 12:44:44.506: D/MyButton(1234): onTouchEvent ACTION_UP 04-01 12:44:44.506: D/Button(1234): 點擊
整理出如下示意圖:三色箭頭實線,表示對應方法返回一個值會跳轉到下一個方法。其中Button 返回true和返回super都會消費事件,只是返回true就不在就不再響應button的點擊事件。
測試方案二:ADS/ATS、LDS/LIS/LTS、RDS/RIS/RTS、BDS/BTF
04-01 14:00:54.970: D/Activity(1387): dispatchTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyLinearLayout(1387): dispatchTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyLinearLayout(1387): onInterceptTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyRelativeLayout(1387): dispatchTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyRelativeLayout(1387): onInterceptTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyButton(1387): dispatchTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyButton(1387): onTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyRelativeLayout(1387): onTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/MyLinearLayout(1387): onTouchEvent ACTION_DOWN 04-01 14:00:54.970: D/Activity(1387): onTouchEvent ACTION_DOWN 04-01 14:00:55.066: D/Activity(1387): dispatchTouchEvent ACTION_UP 04-01 14:00:55.066: D/Activity(1387): onTouchEvent ACTION_UP
測試方案三:ADS/ATS、LDS/LIS/LTS、RDS/RIS/RTS、BDT/BTF
04-01 14:12:47.918: D/Activity(1444): dispatchTouchEvent ACTION_DOWN 04-01 14:12:47.918: D/MyLinearLayout(1444): dispatchTouchEvent ACTION_DOWN 04-01 14:12:47.918: D/MyLinearLayout(1444): onInterceptTouchEvent ACTION_DOWN 04-01 14:12:47.918: D/MyRelativeLayout(1444): dispatchTouchEvent ACTION_DOWN 04-01 14:12:47.918: D/MyRelativeLayout(1444): onInterceptTouchEvent ACTION_DOWN 04-01 14:12:47.918: D/MyButton(1444): dispatchTouchEvent ACTION_DOWN 04-01 14:12:48.030: D/Activity(1444): dispatchTouchEvent ACTION_UP 04-01 14:12:48.030: D/MyLinearLayout(1444): dispatchTouchEvent ACTION_UP 04-01 14:12:48.030: D/MyLinearLayout(1444): onInterceptTouchEvent ACTION_UP 04-01 14:12:48.030: D/MyRelativeLayout(1444): dispatchTouchEvent ACTION_UP 04-01 14:12:48.030: D/MyRelativeLayout(1444): onInterceptTouchEvent ACTION_UP 04-01 14:12:48.030: D/MyButton(1444): dispatchTouchEvent ACTION_UP
說明:Button dispatchTouchEvent方法返回true,表示自身需要消費該事件。且不傳遞給onTouchEvent方法。
測試方案四:ADS/ATS、LDS/LIS/LTS、RDS/RIS/RTS、BDF/BTF
04-01 14:19:40.014: D/Activity(1502): dispatchTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyLinearLayout(1502): dispatchTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyLinearLayout(1502): onInterceptTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyRelativeLayout(1502): dispatchTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyRelativeLayout(1502): onInterceptTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyButton(1502): dispatchTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyRelativeLayout(1502): onTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/MyLinearLayout(1502): onTouchEvent ACTION_DOWN 04-01 14:19:40.014: D/Activity(1502): onTouchEvent ACTION_DOWN 04-01 14:19:40.022: D/Activity(1502): dispatchTouchEvent ACTION_UP 04-01 14:19:40.022: D/Activity(1502): onTouchEvent ACTION_UP
說明:Button dispatchTouchEvent方法返回false,事件傳遞給父控件onTouchEvent方法。
測試方案五:ADS/ATS、LDS/LIS/LTS、RDS/RIS/RTT、BDF/BTF
04-01 14:26:03.306: D/Activity(1561): dispatchTouchEvent ACTION_DOWN 04-01 14:26:03.306: D/MyLinearLayout(1561): dispatchTouchEvent ACTION_DOWN 04-01 14:26:03.306: D/MyLinearLayout(1561): onInterceptTouchEvent ACTION_DOWN 04-01 14:26:03.306: D/MyRelativeLayout(1561): dispatchTouchEvent ACTION_DOWN 04-01 14:26:03.306: D/MyRelativeLayout(1561): onInterceptTouchEvent ACTION_DOWN 04-01 14:26:03.306: D/MyButton(1561): dispatchTouchEvent ACTION_DOWN 04-01 14:26:03.306: D/MyRelativeLayout(1561): onTouchEvent ACTION_DOWN 04-01 14:26:03.366: D/Activity(1561): dispatchTouchEvent ACTION_UP 04-01 14:26:03.366: D/MyLinearLayout(1561): dispatchTouchEvent ACTION_UP 04-01 14:26:03.366: D/MyLinearLayout(1561): onInterceptTouchEvent ACTION_UP 04-01 14:26:03.366: D/MyRelativeLayout(1561): dispatchTouchEvent ACTION_UP 04-01 14:26:03.366: D/MyRelativeLayout(1561): onTouchEvent ACTION_UP
測試方案六:ADS/ATS、LDS/LIS/LTS、RDS/RIS/RTF、BDF/BTF
04-01 14:39:20.794: D/Activity(1644): dispatchTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyLinearLayout(1644): dispatchTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyLinearLayout(1644): onInterceptTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyRelativeLayout(1644): dispatchTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyRelativeLayout(1644): onInterceptTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyButton(1644): dispatchTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyRelativeLayout(1644): onTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/MyLinearLayout(1644): onTouchEvent ACTION_DOWN 04-01 14:39:20.798: D/Activity(1644): onTouchEvent ACTION_DOWN 04-01 14:39:20.874: D/Activity(1644): dispatchTouchEvent ACTION_UP 04-01 14:39:20.874: D/Activity(1644): onTouchEvent ACTION_UP
說明:RelativeLayout return false,就會將事件傳遞到上一層控件消費。其中,RelativeLayout onInterceptTouchEvent return false 表示事件也會分發到Button處理。
測試方案七:ADS/ATS、LDS/LIS/LTS、RDS/RIT/RTF、BDF/BTF
04-01 15:02:41.594: D/Activity(1739): dispatchTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/MyLinearLayout(1739): dispatchTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/MyLinearLayout(1739): onInterceptTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/MyRelativeLayout(1739): dispatchTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/MyRelativeLayout(1739): onInterceptTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/MyRelativeLayout(1739): onTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/MyLinearLayout(1739): onTouchEvent ACTION_DOWN 04-01 15:02:41.594: D/Activity(1739): onTouchEvent ACTION_DOWN 04-01 15:02:41.714: D/Activity(1739): dispatchTouchEvent ACTION_UP 04-01 15:02:41.714: D/Activity(1739): onTouchEvent ACTION_UP
測試方案八:ADS/ATS、LDS/LIS/LTS、RDT/RIT/RTF、BDF/BTF
04-01 15:27:17.002: D/Activity(1907): dispatchTouchEvent ACTION_DOWN 04-01 15:27:17.002: D/MyLinearLayout(1907): dispatchTouchEvent ACTION_DOWN 04-01 15:27:17.002: D/MyLinearLayout(1907): onInterceptTouchEvent ACTION_DOWN 04-01 15:27:17.002: D/MyRelativeLayout(1907): dispatchTouchEvent ACTION_DOWN 04-01 15:27:17.058: D/Activity(1907): dispatchTouchEvent ACTION_UP 04-01 15:27:17.058: D/MyLinearLayout(1907): dispatchTouchEvent ACTION_UP 04-01 15:27:17.058: D/MyLinearLayout(1907): onInterceptTouchEvent ACTION_UP 04-01 15:27:17.058: D/MyRelativeLayout(1907): dispatchTouchEvent ACTION_UP
測試方案九:ADS/ATS、LDS/LIS/LTS、RDF/RIT/RTF、BDF/BTF
04-01 15:32:12.138: D/Activity(1965): dispatchTouchEvent ACTION_DOWN 04-01 15:32:12.138: D/MyLinearLayout(1965): dispatchTouchEvent ACTION_DOWN 04-01 15:32:12.138: D/MyLinearLayout(1965): onInterceptTouchEvent ACTION_DOWN 04-01 15:32:12.138: D/MyRelativeLayout(1965): dispatchTouchEvent ACTION_DOWN 04-01 15:32:12.138: D/MyLinearLayout(1965): onTouchEvent ACTION_DOWN 04-01 15:32:12.138: D/Activity(1965): onTouchEvent ACTION_DOWN 04-01 15:32:12.250: D/Activity(1965): dispatchTouchEvent ACTION_UP 04-01 15:32:12.250: D/Activity(1965): onTouchEvent ACTION_UP
測試方案十:ADT/ATS、LDS/LIS/LTS、RDF/RIT/RTF、BDF/BTF
04-01 15:43:24.558: D/Activity(2023): dispatchTouchEvent ACTION_DOWN 04-01 15:43:24.686: D/Activity(2023): dispatchTouchEvent ACTION_UP
測試方案十一:ADS/ATS、LDS/LIS/LTS、RDF/RIT/RTF、BDF/BTF
04-01 15:56:51.486: D/Activity(2149): dispatchTouchEvent ACTION_DOWN 04-01 15:56:51.486: D/MyLinearLayout(2149): dispatchTouchEvent ACTION_DOWN 04-01 15:56:51.486: D/MyLinearLayout(2149): onInterceptTouchEvent ACTION_DOWN 04-01 15:56:51.486: D/MyRelativeLayout(2149): dispatchTouchEvent ACTION_DOWN 04-01 15:56:51.486: D/MyLinearLayout(2149): onTouchEvent ACTION_DOWN 04-01 15:56:51.486: D/Activity(2149): onTouchEvent ACTION_DOWN 04-01 15:56:51.490: D/Activity(2149): dispatchTouchEvent ACTION_UP 04-01 15:56:51.490: D/Activity(2149): onTouchEvent ACTION_UP
參照之前理解的思路,將整個示意圖完善如下:
總結:
1.Activity 的dispatchTouchEvent方法,執行父類dispatchTouchEvent方法時,會將事件分發至ViewGroup,否則自身消費事件。
2.當事件再次傳遞到Activity 的onTouchEvent方法,表示頁面中所有控件都不響應事件,由Activity處理。
3.ViewGroup和View 的dispatchTouchEvent方法,return true表示不分發事件,自身處理。return false就將事件傳遞給上層控件的onTouchEvent方法,並且不再響應上層控件傳遞過來的事件。
4.ViewGroup 的onInterceptTouchEvent方法return true表示攔截事件,將事件傳遞給自身的onTouchEvent方法處理。
5.View 的onTouchEvent 方法return false 表示不消費事件,將事件傳遞給上層控件的onTouchEvent方法。否則,消費事件。
6.ViewGroup的onTouchEvent 方法return true表示消費事件,否則將事件傳遞給上層控件或Activity的onTouchEvent方法處理。
7.當控件沒有處理事件時,就不再接收下一個事件消息,虛線所示。
最後,祝各位程序猿/媛、碼農、攻城獅們愚人節快樂。
Android 掃描條形碼(Zxing插件),androidzxing使用Android Studio 一、在build.gradle(Module:app)添加代碼 &
用Kotlin創建第一個Android項目(KAD 01),kotlinandroid原文標題:Create your first Android project usi
Android Studio系列-Activity單元測試 前言 上一篇講的是如何在Android Studio中進行Junit單元測試,這篇來簡單記錄一下如何針對A
Android學習第五篇——動畫效果 接下來我要介紹的是Android的一些動畫效果 一、AlphaAnimation透明動畫 這裡我用到了兩種不同的方法來實現透明動畫效