0
我在學習Android View Touch事件時遇到了一個非常奇怪的問題。我有一個佈局如下:關於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>
而且我添加了觸摸監聽器的textview和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中:向下」 - > 「的TextView:UP」 - >活動:向上。爲什麼linearlayout的onTouch方法沒有執行?