Android 事件传递 2
2016-01-18 03:54
387 查看
Android 事件传递 由父布局传递给子控件
//拦截器 默认返回false
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return super.onInterceptTouchEvent(ev);
}
自定义LinearLayout
package com.tz.touchtrans.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
// 拦截器 默认返回false
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.i("INFO", "ll onInterceptEvent");
return true;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.tz.touchtrans.view.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#00ff00"
android:id="@+id/ll"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:text="@string/hello"
android:background="#ff0000"
android:id="@+id/tv"
/>
</com.tz.touchtrans.view.MyLinearLayout>
结果 ll onInterceptEvent
ll action Down
//拦截器 默认返回false
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
return super.onInterceptTouchEvent(ev);
}
自定义LinearLayout
package com.tz.touchtrans.view;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
// 拦截器 默认返回false
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.i("INFO", "ll onInterceptEvent");
return true;
}
}
package com.tz.touchtrans; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; import com.tz.touchtrans.view.MyLinearLayout; public class MainActivity extends Activity { private MyLinearLayout ll; private TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ll = (MyLinearLayout) findViewById(R.id.ll); tv = (TextView) findViewById(R.id.tv); ll.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction();// 手势的动作 switch (action) { case MotionEvent.ACTION_DOWN: Log.i("INFO", "ll action Down"); break; case MotionEvent.ACTION_MOVE: Log.i("INFO", "ll action Move"); break; case MotionEvent.ACTION_UP: Log.i("INFO", "ll action Up"); break; default: break; } return false; } }); tv.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction();// 手势的动作 switch (action) { case MotionEvent.ACTION_DOWN: Log.i("INFO", "tv action Down"); break; case MotionEvent.ACTION_MOVE: Log.i("INFO", "tv action Move"); break; case MotionEvent.ACTION_UP: Log.i("INFO", "tv action Up"); break; default: break; } return false; } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.tz.touchtrans.view.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#00ff00"
android:id="@+id/ll"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:text="@string/hello"
android:background="#ff0000"
android:id="@+id/tv"
/>
</com.tz.touchtrans.view.MyLinearLayout>
结果 ll onInterceptEvent
ll action Down
相关文章推荐
- Android 事件传递 1
- Android_Ant 编译相关资料整理
- android图像处理
- android Animation动画的xml使用
- android常用配色
- 2015年,从毕业到工作的几点感悟(Android开发新人)
- Android入门第二课:搭建安卓开发环境
- 【Android】ListView与Button的共存问题解决
- android opencv 环境搭建
- Android studio下使用ShareSDK
- 郭霖老师androidpn教学视频笔记
- Android进阶——git上传代码到coding.net
- Android Volley入门到精通:初识Volley的基本用法(示例,出错代码)
- AndroidManifest.xml配置文件详解
- Android EditText 分割文字输入
- Android抓屏真机屏幕ASM- Android Screen Monitor
- 选择粤嵌安卓培训,给你的未来一个交代
- android:利用异步请求同时下载多张图片(07)
- Android Fragment getActivity null 解决
- 蓝牙耳机 事件监听 android.intent.action.MEDIA_BUTTON(二)