獨立組件之間重疊放置時,OnTouch事件的響應順序

結論:OnTouch由上到下依次響應。上層指排在後面的View,既在UI界面顯示在上層的View。
如果上層控件return true,消費掉事件,下層組件不在響應OnTouch事件。

注意:OnTouch事件只能由子控件向父控件傳遞。

測試佈局文件

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="200dip"
android:layout_centerHorizontal="true"
android:background="@color/material_blue_grey_900"
android:layout_alignParentBottom="true">

<ImageView
android:id="@+id/imageView"
android:text="@string/hello_world" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="center"
android:src="@drawable/ic_launcher"/>

<TextView
android:id="@+id/textView"
android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:layout_gravity="center" />

</FrameLayout>


測試代碼一:
imageView -> return false;
textView -> return false;
即:imageView 和 textView 的OnTouch事件都返回false。

textView = (TextView)findViewById(R.id.textView);

imageView = (ImageView)findViewById(R.id.imageView);
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("--", "onTouch-----------imageView");
return false;
}
});

textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("--", "onTouch-----------textView");
return false;
}
});

結果一:
09-15 20:23:12.134  28895-28895/com.example.langjian.myapplication E/--﹕ onTouch-----------textView
09-15 20:23:12.134  28895-28895/com.example.langjian.myapplication E/--﹕ onTouch-----------imageView

測試代碼二:
imageView -> return false;
textView -> return true;

textView = (TextView)findViewById(R.id.textView);

imageView = (ImageView)findViewById(R.id.imageView);



imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("--", "onTouch-----------imageView");
return false;
}
});
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("--", "onTouch-----------textView");
return true;
}
});
結果二:
09-15 20:25:13.844  30500-30500/com.example.langjian.myapplication E/--﹕ onTouch-----------textView
09-15 20:25:13.864  30500-30500/com.example.langjian.myapplication E/--﹕ onTouch-----------textView

測試代碼三:
imageView -> return true;
textView -> return false;

textView = (TextView)findViewById(R.id.textView);

imageView = (ImageView)findViewById(R.id.imageView);

imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("--", "onTouch-----------imageView");
return true;
}
});
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("--", "onTouch-----------textView");
return false;
}
});
結果三:
09-15 20:39:25.022  31824-31824/com.example.langjian.myapplication E/--﹕ onTouch-----------textView
09-15 20:39:25.022  31824-31824/com.example.langjian.myapplication E/--﹕ onTouch-----------imageView
測試代碼四:
imageView -> return true;
textView -> return true;
結果四:同結果2
發佈了78 篇原創文章 · 獲贊 38 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章