{"id":995,"date":"2015-09-08T16:42:40","date_gmt":"2015-09-08T08:42:40","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=995"},"modified":"2016-08-19T11:35:16","modified_gmt":"2016-08-19T03:35:16","slug":"android%e4%ba%8b%e4%bb%b6%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=995","title":{"rendered":"Android\u4e8b\u4ef6\u5904\u7406\u673a\u5236"},"content":{"rendered":"<p><span style=\"color: #000000;\">Android\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u66f4\u51c6\u786e\u7684\u8bf4\u5e94\u8be5\u53eb &#8220;Android\u4e8b\u4ef6\u5206\u53d1\u3001\u62e6\u622a\u3001\u5904\u7406\u673a\u5236&#8221;\u3002\u8fd9\u91cc\u4e3b\u8981\u662f\u9488\u5bf9Android\u4e2ddispatchTouchEvent(\u5206\u53d1TouchEvent), onInterceptTouchEvent(\u62e6\u622aTouchEvent), onTouchEvent(\u5904\u7406TouchEvent)\u4e8b\u4ef6\u7684\u7406\u89e3.<\/span><\/p>\n<p><!--more--><br \/>\n\u9996\u5148\u6211\u4eec\u6765\u8ba4\u8bc6\u4e0bdispatchTouchEvent, onInterceptTouchEvent, onTouchEvent\u4e8b\u4ef6.<\/p>\n<p><span style=\"color: #800000;\">dispatchTouchEvent\u7528\u4e8e\u51b3\u5b9a\u4e8b\u4ef6\u662f\u5426\u5206\u53d1\uff0c\u5f53TouchEvent\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u6700\u5148\u5230\u8fbe\u9876\u5c42Activity\u7684dispatchTouchEvent\u65b9\u6cd5,<\/span><span style=\"color: #800000;\">\u8fd4\u56deture\u6216false\u65f6\u4e8b\u4ef6\u88ab\u81ea\u8eab\u6d88\u8d39\u5904\u7406\uff0c\u8fd4\u56de<\/span>super.dispatchTouchEvent(ev)(\u540e\u9762\u7b80\u79f0super)\u4f20\u9012\u7ed9\u4e0b\u4e00\u5c42\u7ea7dispatchTouchEvent\u65b9\u6cd5\u5904\u7406.<\/p>\n<p><span style=\"color: #800000;\">onInterceptTouchEvent\u7528\u4e8e\u51b3\u5b9a\u4e8b\u4ef6\u662f\u5426\u7ee7\u7eed\u5411\u4e0b\u4f20\u9012\u8fd4\u56defalse\u6216super\u65f6\u4e8b\u4ef6\u4f1a\u4f20\u9012\u7ed9\u5b50View\uff0c\u8fd4\u56de\u503c\u4e3atrue\u65f6\u4e8b\u4ef6\u4e0d\u4f1a\u7ee7\u7eed\u4f20\u9012\u7ed9\u5b50View,\u4e8b\u4ef6\u5c06\u4ea4\u7ed9\u5f53\u524dViewGroup\u7684onTouchEvent()\u5904\u7406\uff0c\u8fd9\u5c31\u662f\u6240\u8c13\u7684Intercept(\u62e6\u622a)\u3002<\/span><\/p>\n<p><span style=\"color: #800000;\">onTouchEvent\u7528\u4e8e\u5904\u7406\u4e8b\u4ef6(\u4f20\u9012\u65b9\u5411\u662f\u4ece\u5b50View-&gt;\u7236View),\u8fd4\u56de\u503c\u51b3\u5b9a\u5f53\u524d\u63a7\u4ef6\u662f\u5426\u6d88\u8d39(\u5904\u7406)\u4e86\u8fd9\u4e2a\u4e8b\u4ef6\u3002\u8fd4\u56de\u503c\u4e3atrue\u8868\u793a\u5904\u7406\u4e86\u5f53\u524d\u4e8b\u4ef6,\u7236View\u7684OnTouchEvent\u4e8b\u4ef6\u5c06\u4e0d\u4f1a\u6267\u884c,\u8fd4\u56de\u503c\u4e3afalse\u6216super\u8868\u793a\u4e0d\u5904\u7406\u5f53\u524d\u4e8b\u4ef6\u4ea4\u7ed9\u7236View\u7684OnTouchEvent\u4e8b\u4ef6\u5904\u7406\u3002<\/span><\/p>\n<p><strong>\u6ce8:\u5bf9\u4e8e\u5f53\u524d\u4e8b\u4ef6\u7406\u89e3: \u4e00\u6b21\u70b9\u51fb\u64cd\u4f5c\u53ef\u80fd\u5305\u542b\u4e09\u4e2a\u4e8b\u4ef6,\u6309\u4e0b\u4e8b\u4ef6(Down)\uff0c\u79fb\u52a8\u4e8b\u4ef6\uff08Move\uff09,\u5f39\u8d77\u4e8b\u4ef6(up).<\/strong><br \/>\n<strong> \u5bf9\u4e8eACTION_DOWN\u4e8b\u4ef6\uff0c\u8fd4\u56detrue\uff0c\u8868\u793a\u6211\u60f3\u8981\u5904\u7406\u540e\u7eed\u4e8b\u4ef6\uff1b\u8fd4\u56defalse\uff0c\u8868\u793a\u4e0d\u5173\u5fc3\u6b64\u4e8b\u4ef6\uff0c\u5e76\u8fd4\u56de\u7531\u7236\u7c7b\u8fdb\u884c\u5904\u7406\u3002<\/strong><br \/>\n<strong>\u7279\u522b\u6ce8\u610fACTION_MOVE\u6216\u8005ACTION_UP\u53d1\u751f\u7684\u524d\u63d0\u662f\u4e00\u5b9a\u66fe\u7ecf\u53d1\u751f\u4e86ACTION_DOWN\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u6d88\u8d39ACTION_DOWN\uff0c\u90a3\u4e48\u7cfb\u7edf\u4f1a\u8ba4\u4e3aACTION_DOWN\u6ca1\u6709\u53d1\u751f\u8fc7\uff0c\u6240\u4ee5ACTION_MOVE\u6216\u8005ACTION_UP\u5c31\u4e0d\u80fd\u88ab\u6355\u83b7\u3002<\/strong><\/p>\n<p>\u6211\u4eec\u770b\u4e0b\u793a\u4f8b:<br \/>\n\u5e03\u5c40\u5982\u4e0b<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908162106.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-996\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908162106.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908162106\" width=\"455\" height=\"385\" \/><\/a><\/p>\n<p>[java]&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br \/>\n&lt;com.scxh.android1502.event.RootLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n    android:layout_width=&quot;match_parent&quot;<br \/>\n    android:layout_height=&quot;match_parent&quot;<br \/>\n    android:orientation=&quot;vertical&quot; &gt;<\/p>\n<p>    &lt;com.scxh.android1502.event.TwotLayout<br \/>\n        android:layout_width=&quot;match_parent&quot;<br \/>\n        android:layout_height=&quot;match_parent&quot;<br \/>\n        android:orientation=&quot;vertical&quot; &gt;<\/p>\n<p>        &lt;com.scxh.android1502.event.ChildTextView<br \/>\n            android:id=&quot;@+id\/event_text&quot;<br \/>\n            android:layout_width=&quot;match_parent&quot;<br \/>\n            android:layout_height=&quot;wrap_content&quot;<br \/>\n            android:text=&quot;android\u4e8b\u4ef6\u5904\u7406\u673a\u5236&quot; \/&gt;<\/p>\n<p>    &lt;\/com.scxh.android1502.event.TwotLayout&gt;<\/p>\n<p>&lt;\/com.scxh.android1502.event.RootLayout&gt;[\/java]<\/p>\n<p>&nbsp;<\/p>\n<pre>\u6d4b\u8bd5\u4ee3\u7801<\/pre>\n<p>[java]public class RootLayout extends LinearLayout {<\/p>\n<p>\tpublic RootLayout(Context context, AttributeSet attrs) {<br \/>\n\t\tsuper(context, attrs);<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic boolean onInterceptTouchEvent(MotionEvent ev) {<br \/>\n\t\tLogs.d(&quot;RootLayout   onInterceptTouchEvent &gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<\/p>\n<p>\t\treturn false;<br \/>\n\t}<br \/>\n\t@Override<br \/>\n\tpublic boolean onTouchEvent(MotionEvent event) {<br \/>\n\t\tswitch (event.getAction()) {<br \/>\n\t\tcase MotionEvent.ACTION_UP:<br \/>\n\t\t\tLogs.i(&quot;RootLayout onTouchEvent \u4e8b\u4ef6 ACTION_UP&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MotionEvent.ACTION_MOVE:<br \/>\n\t\t\tLogs.v(&quot;RootLayout onTouchEvent \u4e8b\u4ef6   ACTION_MOVE&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MotionEvent.ACTION_DOWN:<br \/>\n\t\t\tLogs.d(&quot;RootLayout onTouchEvent \u4e8b\u4ef6  ACTION_DOWN&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\t}<\/p>\n<p>\t\treturn true;<br \/>\n\t}<br \/>\n}[\/java]<\/p>\n<p>[java]public class TwotLayout extends LinearLayout {<\/p>\n<p>\tpublic TwotLayout(Context context, AttributeSet attrs) {<br \/>\n\t\tsuper(context, attrs);<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic boolean onInterceptTouchEvent(MotionEvent ev) {<br \/>\n\t\tLogs.i(&quot;TwotLayout   onInterceptTouchEvent &gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<\/p>\n<p>\t\treturn false;<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic boolean onTouchEvent(MotionEvent event) {<br \/>\n\t\tswitch (event.getAction()) {<br \/>\n\t\tcase MotionEvent.ACTION_UP:<br \/>\n\t\t\tLogs.i(&quot;TwotLayout onTouchEvent \u4e8b\u4ef6 ACTION_UP&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MotionEvent.ACTION_MOVE:<br \/>\n\t\t\tLogs.v(&quot;TwotLayout onTouchEvent \u4e8b\u4ef6   ACTION_MOVE&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MotionEvent.ACTION_DOWN:<br \/>\n\t\t\tLogs.d(&quot;TwotLayout onTouchEvent \u4e8b\u4ef6  ACTION_DOWN&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\t}<\/p>\n<p>\t\treturn false;<br \/>\n\t}<\/p>\n<p>}[\/java]<\/p>\n<p>[java]<br \/>\npublic class ChildTextView extends TextView {<\/p>\n<p>\tpublic ChildTextView(Context context, AttributeSet attrs) {<br \/>\n\t\tsuper(context, attrs);<br \/>\n\t}<\/p>\n<p>\t@Override<br \/>\n\tpublic boolean onTouchEvent(MotionEvent event) {<br \/>\n\t\tswitch (event.getAction()) {<br \/>\n\t\tcase MotionEvent.ACTION_UP:<br \/>\n\t\t\tLogs.i(&quot;ChildTextView onTouchEvent \u4e8b\u4ef6 ACTION_UP&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MotionEvent.ACTION_MOVE:<br \/>\n\t\t\tLogs.v(&quot;ChildTextView onTouchEvent \u4e8b\u4ef6   ACTION_MOVE&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\tcase MotionEvent.ACTION_DOWN:<br \/>\n\t\t\tLogs.d(&quot;ChildTextView onTouchEvent \u4e8b\u4ef6  ACTION_DOWN&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;);<br \/>\n\t\t\tbreak;<br \/>\n\t\t}<\/p>\n<p>\t\treturn true;<br \/>\n\t}<\/p>\n<p>}<br \/>\n[\/java]<\/p>\n<p>1. \u5f53RootLayout,TwoLayout\u7684onInterceptTouchEvent\u4e8b\u4ef6\u8fd4\u56defalse\u4e5f\u5c31\u662f\u4e0d\u62e6\u622a\u4e14ChildTextView\u7684onTouchEvent\u4e8b\u4ef6\u8fd4\u56detrue\u6d88\u8017\u7b2c\u4e00\u4e2aAction_Down\u4e8b\u4ef6\u6267\u884c\u60c5\u51b5\u4e3a:<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908162721.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-997\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908162721.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908162721\" width=\"405\" height=\"142\" \/><\/a><br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163419.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-998\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163419.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908163419\" width=\"347\" height=\"257\" \/><\/a>\u00a0 \u00a0<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163514.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-999\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163514.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908163514\" width=\"339\" height=\"255\" \/><\/a><\/p>\n<p>2.\u5f53RootLayout,TwoLayout\u7684onInterceptTouchEvent\u4e8b\u4ef6\u8fd4\u56defalse\u4e5f\u5c31\u662f\u4e0d\u62e6\u622a\u4e14ChildTextView\u7684onTouchEvent\u4e8b\u4ef6\u8fd4\u56defalse\u4e0d\u6d88\u8017\u7b2c\u4e00\u4e2aAction_Down\u4e8b\u4ef6\u6267\u884c\u60c5\u51b5\u4e3a:<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163649.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1000\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163649.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908163649\" width=\"377\" height=\"128\" \/><\/a><\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163832.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1001\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908163832.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908163832\" width=\"355\" height=\"267\" \/><\/a>\u00a0<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908164027.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1002\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/QQ\u255c\u256a\u2550\u255d20150908164027.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150908164027\" width=\"347\" height=\"263\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e8b\u4ef6\u4f20\u8f93\u673a\u5236\u66f4\u8be6\u7ec6\u7406\u89e3\u63a8\u8350kelin\u5199\u7684\u8fd9\u7f16\u6587\u7ae0\uff0c<a href=\"http:\/\/www.jianshu.com\/p\/e99b5e8bd67b\">http:\/\/www.jianshu.com\/p\/e99b5e8bd67b<\/a><\/p>\n<p>\u8fd9\u91cc\u8d34\u5f20\u56fe.<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/966283-b9cb65aceea9219b.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-1681\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/966283-b9cb65aceea9219b.png\" alt=\"966283-b9cb65aceea9219b\" width=\"735\" height=\"525\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/966283-b9cb65aceea9219b.png 885w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/09\/966283-b9cb65aceea9219b-140x100.png 140w\" sizes=\"(max-width: 735px) 100vw, 735px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u53c2\u8003<\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/yanzi1225627\/article\/details\/22592831\">\u7ec6\u8bf4Android\u4e8b\u4ef6\u4f20\u9012\u673a\u5236(dispatchTouchEvent\u3001onInterceptTouchEvent\u3001onTouchEvent)<\/a><\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/lvxiangan\/article\/details\/9309927\">Android-onInterceptTouchEvent()\u548conTouchEvent()\u603b\u7ed3<\/a><\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/guitk\/article\/details\/7057155\">Android\u4e2ddispatchTouchEvent, onInterceptTouchEvent, onTouchEvent\u7684\u7406\u89e3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android\u4e8b\u4ef6\u5904\u7406\u673a\u5236\u66f4\u51c6\u786e\u7684\u8bf4\u5e94\u8be5\u53eb &#8220;Android\u4e8b\u4ef6\u5206\u53d1\u3001\u62e6\u622a\u3001\u5904\u7406\u673a\u5236&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/995"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=995"}],"version-history":[{"count":21,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions"}],"predecessor-version":[{"id":1683,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions\/1683"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}