【发布时间】:2016-09-13 09:02:33
【问题描述】:
我在研究Android View Touch事件的时候遇到了一个很奇怪的问题。我在下面有一个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
我在文本视图和线性布局中添加了触摸监听器。如下:
private final static String TAG = MainActivity.class.getSimpleName();
LinearLayout llMain;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llMain = (LinearLayout) findViewById(R.id.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
llMain.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "Layout: Down");
return true;
case MotionEvent.ACTION_UP:
Log.d(TAG, "Layout: UP");
return false;
default:
return false;
}
}
});
tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "TextView: Down");
return false;
case MotionEvent.ACTION_UP:
Log.d(TAG, "TextView: UP");
return false;
default:
return false;
}
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "Activity: Down");
return true;
case MotionEvent.ACTION_UP:
Log.d(TAG, "Activity: UP");
return true;
}
return super.onTouchEvent(event);
}
我的问题是:当我按下 textview 时,执行事件的顺序是:“TextView: Down” -> “TextView: UP” -> Activity: Up。为什么没有执行linearlayout的onTouch方法?
【问题讨论】: