独立组件之间重叠放置时,OnTouch事件的响应顺序
2015-09-15 20:53
357 查看
结论: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() {@Overridepublic 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-----------textView09-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() {@Overridepublic 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-----------textView09-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() {@Overridepublic boolean onTouch(View v, MotionEvent event) {Log.e("--", "onTouch-----------imageView");return true;}});
textView.setOnTouchListener(new View.OnTouchListener() {@Overridepublic 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-----------textView09-15 20:39:25.022 31824-31824/com.example.langjian.myapplication E/--﹕ onTouch-----------imageView测试代码四:
imageView -> return true;
textView -> return true;结果四:同结果2
相关文章推荐
- poj 2356 Find a multiple(鸽巢原理)
- Android开发-Listview中显示不同的视图布局
- Android开发-Listview中显示不同的视图布局
- 第九课 3D空间中移动图像:
- 项目实战No8 刷新 日历处理
- C/C++中“#”和“##”的作用和用法
- BZOJ 1123 [POI2008]BLO 点双连通分量
- BZOJ1170 : [Balkan2007]Cipher
- 图的遍历 DFS和BFS
- 安卓:回退栈,类似新闻的布局界面
- Fragment 与 Activity交互
- codeforces 244B Restoring Table(“&”位运算)
- 三种获取LayoutInflater对象的方式
- iOS中的UITabBarController(标签视图控制器)
- ASP.NET Helper
- hdu 4745 区间回文最长长度
- 紫书 习题 9-8 uva1632
- hdu5446 lucas+crt
- C#向上转型与向下转型
- 基本TCP套接字编程