您的位置:首页 > 其它

独立组件之间重叠放置时,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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: