<h1><br> </h1>
<p><span style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:25px; font-weight:600; white-space:pre-wrap"><span style="color:rgb(90,90,90); font-family:'microsoft yahei'; 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 = 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);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "dispatchTouchEvent---ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "dispatchTouchEvent---ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "dispatchTouchEvent---ACTION_UP");
break;
}
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "onTouchEvent---ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG, "onTouchEvent---ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "onTouchEvent---ACTION_UP");
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"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.richsoft.viewdemo.MainActivity">
<com.richsoft.viewdemo.MyButton
android:id="@+id/btn_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button click" />
</RelativeLayout></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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_click = (MyButton) findViewById(R.id.btn_click);
btn_click.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i(MyButton.TAG, "onTouch---ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(MyButton.TAG, "onTouch---ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(MyButton.TAG, "onTouch---ACTION_UP");
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 |
|