Android View事件分发机制总结(上)

论坛 期权论坛     
选择匿名的用户   2021-6-1 18:18   229   0
<h1><br> </h1>
<p><span style="color:rgb(90,90,90); font-family:&#39;microsoft yahei&#39;; font-size:25px; font-weight:600; white-space:pre-wrap"><span style="color:rgb(90,90,90); font-family:&#39;microsoft yahei&#39;; font-size:18px; white-space:pre-wrap">这里View不包括ViewGroup,它没有子元素不需要向下传递事件,只能自己去处理事件,因此只有dispatchTouchEvent方法和onTouchEvent方法。源码分析之前,咱们先来一个简单例子。</span></span></p>
<p><span style="font-family:microsoft yahei; font-size:18px; color:#5a5a5a"><span style="font-weight:600; white-space:pre-wrap">咱们首先自定义一个Button叫MyButton,并且重写其dispatchTouchEvent方法和onTouchEvent方法,代码如下。</span></span></p>
<p><span style="font-family:microsoft yahei; font-size:18px; color:#5a5a5a"><span style="font-weight:600; white-space:pre-wrap"></span></span></p>
<pre class="blockcode"><code class="language-java">public class MyButton extends Button {
    public static final String TAG &#61; MyButton.class.getSimpleName();

    public MyButton(Context context) {
        super(context);
    }

    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    &#64;Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, &#34;dispatchTouchEvent---ACTION_DOWN&#34;);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, &#34;dispatchTouchEvent---ACTION_MOVE&#34;);
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, &#34;dispatchTouchEvent---ACTION_UP&#34;);
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    &#64;Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, &#34;onTouchEvent---ACTION_DOWN&#34;);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, &#34;onTouchEvent---ACTION_MOVE&#34;);
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, &#34;onTouchEvent---ACTION_UP&#34;);
                break;
        }
        return super.onTouchEvent(event);
    }
}
</code></pre>然后将MyButton添加至MainActivty布局文件中,代码如下。
<p></p>
<p></p>
<p><span style="font-family:microsoft yahei; font-size:18px; color:#5a5a5a"></span></p>
<pre class="blockcode"><code class="language-html">&lt;?xml version&#61;&#34;1.0&#34; encoding&#61;&#34;utf-8&#34;?&gt;
&lt;RelativeLayout xmlns:android&#61;&#34;http://schemas.android.com/apk/res/android&#34;
    xmlns:tools&#61;&#34;http://schemas.android.com/tools&#34;
    android:id&#61;&#34;&#64;&#43;id/activity_main&#34;
    android:layout_width&#61;&#34;match_parent&#34;
    android:layout_height&#61;&#34;match_parent&#34;
    android:paddingBottom&#61;&#34;&#64;dimen/activity_vertical_margin&#34;
    android:paddingLeft&#61;&#34;&#64;dimen/activity_horizontal_margin&#34;
    android:paddingRight&#61;&#34;&#64;dimen/activity_horizontal_margin&#34;
    android:paddingTop&#61;&#34;&#64;dimen/activity_vertical_margin&#34;
    tools:context&#61;&#34;com.richsoft.viewdemo.MainActivity&#34;&gt;

    &lt;com.richsoft.viewdemo.MyButton
        android:id&#61;&#34;&#64;&#43;id/btn_click&#34;
        android:layout_width&#61;&#34;wrap_content&#34;
        android:layout_height&#61;&#34;wrap_content&#34;
        android:text&#61;&#34;button click&#34; /&gt;
&lt;/RelativeLayout&gt;</code></pre>
<br>
<span style="font-weight:600; white-space:pre-wrap">最后在MainActivity中给MyButton设置onTouchListener,代码如下。</span>
<p></p>
<p><span style="font-family:microsoft yahei; font-size:18px; color:#5a5a5a"><span style="font-weight:600; white-space:pre-wrap"></span></span></p>
<pre class="blockcode"><code class="language-java">public class MainActivity extends AppCompatActivity {
    private MyButton btn_click;

    &#64;Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_click &#61; (MyButton) findViewById(R.id.btn_click);

        btn_click.setOnTouchListener(new View.OnTouchListener() {
            &#64;Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i(MyButton.TAG, &#34;onTouch---ACTION_DOWN&#34;);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.i(MyButton.TAG, &#34;onTouch---ACTION_MOVE&#34;);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i(MyButton.TAG, &#34;onTouch---ACTION_UP&#34;);
                        break;
                }
                return false;
            }
        });

    }

}
</code></pre>运行程序,打印日志如下:
<p></p>
<p><span style="font-family:microsoft yahei; font-size:18px; color:#5a5a5a"><span style="font-weight:600; white-space:pre-wrap"><img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-9ff87bc8820d
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP