还是以自定义的TestButton为例。
我们可以通过重写onTouchEvent方法来处理诸如down move up的消息:
- public class TestButton extends Button {
- public TestButton(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- }
- public TestButton(Context context, AttributeSet attributeSet) {
- super(context, attributeSet);
- // TODO Auto-generated constructor stub
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- boolean value = super.onTouchEvent(event);
- System.out.println("super.onTouchEvent: " + value+ " event: " + event.getAction());
- return value;
- }
public class TestButton extends Button { public TestButton(Context context) { super(context); // TODO Auto-generated constructor stub } public TestButton(Context context, AttributeSet attributeSet) { super(context, attributeSet); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent event) { boolean value = super.onTouchEvent(event); System.out.println("super.onTouchEvent: " + value+ " event: " + event.getAction()); return value; }
也可以通过实现OnTouchListener的接口,然后设置TestButton的onTouchListener可以达到同样的目的
- class OnTouchListenerTest implements View.OnTouchListener{
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return false;
- }
- }
class OnTouchListenerTest implements View.OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { return false; } }
- TestButton b = (TestButton)findViewById(R.id.button);
- OnTouchListenerTest listener = new OnTouchListenerTest();
- b.setOnTouchListener(listener);
TestButton b = (TestButton)findViewById(R.id.button); OnTouchListenerTest listener = new OnTouchListenerTest(); b.setOnTouchListener(listener);
但上述两种监听有什么区别呢?
先看一下Android源码中对于View中dispatchTouchEvent的实现:
- public boolean dispatchTouchEvent(MotionEvent event){
- ... ...
- if(onFilterTouchEventForSecurity(event)){
- ListenerInfo li = mListenerInfo;
- if(li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
- && li.mOnTouchListener.onTouch(this, event)) {
- return true;
- }
- if(onTouchEvent(event)){
- return true;
- }
- }
- ... ...
- return false;
- }
//源码:
public boolean dispatchTouchEvent(MotionEvent event){ ... ... if(onFilterTouchEventForSecurity(event)){ ListenerInfo li = mListenerInfo; if(li != null && li.mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED && li.mOnTouchListener.onTouch(this, event)) { return true; } if(onTouchEvent(event)){ return true; } } ... ... return false; }
可以看到onTouchListener的接口的优先级是要高于onTouchEvent的,假若onTouchListener中的onTouch方法返回true,
表示此次事件已经被消费了,那onTouchEvent是接收不到消息的。
因为Button的performClick是利用onTouchEvent实现,假若onTouchEvent没有被调用到,那么Button的Click事件也无法响应。
综合来讲:
onTouchListener的onTouch方法优先级比onTouchEvent高,会先触发。
假如onTouch方法返回false会接着触发onTouchEvent,反之onTouchEvent方法不会被调用。
内置诸如click事件的实现等等都基于onTouchEvent,假如onTouch返回true,这些事件将不会被触发。
//------------布局文件--------------------
<?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"
tools:context="myontouch.bwie.com.myontouch.MainActivity">
<myontouch.bwie.com.myontouch.TestButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击"
android:id="@+id/button"/>
</RelativeLayout>
//--------------自定义TextButton--------------------------
public class TestButton extends Button{
public TestButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean value = super.onTouchEvent(event);
System.out.println("super.onTouchEvent: " + value+ " event: " + event.getAction());
Log.i("我是onTouchEvent","onTouchEvent");
return value;
}
}
//----------------主Activity-------------
public class MainActivity extends AppCompatActivity {
private TestButton button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
button = (TestButton) findViewById(R.id.button);
OnTouchListenerTest listener = new OnTouchListenerTest();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("我是OnClickListener","OnClickListener");
}
});
button.setOnTouchListener(listener);
}
class OnTouchListenerTest implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("我是onTouch","onTouch");
return false;
}
}
//---------------下面是我的OnTouch 返回false 时,的打印----------
12-14 13:52:51.550 8752-8752/myontouch.bwie.com.myontouch I/我是onTouch: onTouch
12-14 13:52:51.553 8752-8752/myontouch.bwie.com.myontouch I/System.out: super.onTouchEvent: true event: 0
12-14 13:52:51.554 8752-8752/myontouch.bwie.com.myontouch I/我是onTouchEvent: onTouchEvent
12-14 13:52:51.565 8752-8752/myontouch.bwie.com.myontouch I/我是onTouch: onTouch
12-14 13:52:51.566 8752-8752/myontouch.bwie.com.myontouch I/System.out: super.onTouchEvent: true event: 2
12-14 13:52:51.566 8752-8752/myontouch.bwie.com.myontouch I/我是onTouchEvent: onTouchEvent
12-14 13:52:51.649 8752-8752/myontouch.bwie.com.myontouch I/我是onTouch: onTouch
12-14 13:52:51.649 8752-8752/myontouch.bwie.com.myontouch I/System.out: super.onTouchEvent: true event: 1
12-14 13:52:51.649 8752-8752/myontouch.bwie.com.myontouch I/我是onTouchEvent: onTouchEvent
12-14 13:52:51.656 8752-8752/myontouch.bwie.com.myontouch I/我是OnClickListener: OnClickListener
//-------------------下面是 onTouch 返回 true时的打印-------------------------
12-14 14:03:33.399 19886-19886/myontouch.bwie.com.myontouch I/我是onTouch: onTouch
12-14 14:03:33.415 19886-19886/myontouch.bwie.com.myontouch I/我是onTouch: onTouch
12-14 14:03:33.429 19886-19886/myontouch.bwie.com.myontouch I/我是onTouch: onTouch