您的位置:首页 > 移动开发 > Android开发

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;
}

}

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