Android触摸屏事件派发机制详解与源码分析二(ViewGroup篇)

2-1 例子

这个例子布局等还和上一篇的例子相似,只是重写了Button和LinearLayout而已,所以效果图不在提供,具体参见上一篇。

首先我们简单的自定义一个Button(View的子类),再自定义一个LinearLayout(ViewGroup的子类),其实没有自定义任何属性,只是重写部分方法(添加了打印,方便查看)而已,如下:

public class TestButton extends Button {

public TestButton(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

Log.i(null, “TestButton dispatchTouchEvent-- action=” + event.getAction());

return super.dispatchTouchEvent(event);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

Log.i(null, “TestButton onTouchEvent-- action=” + event.getAction());

return super.onTouchEvent(event);

}

}

public class TestLinearLayout extends LinearLayout {

public TestLinearLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

Log.i(null, “TestLinearLayout onInterceptTouchEvent-- action=” + ev.getAction());

return super.onInterceptTouchEvent(ev);

}

@Override

public boolean dispatchTouchEvent(MotionEvent event) {

Log.i(null, “TestLinearLayout dispatchTouchEvent-- action=” + event.getAction());

return super.dispatchTouchEvent(event);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

Log.i(null, “TestLinearLayout onTouchEvent-- action=” + event.getAction());

return super.onTouchEvent(event);

}

}

如上两个控件很简单吧,不解释,继续看其他代码:

<?xml version="1.0" encoding="utf-8"?>

<com.zzci.light.TestLinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:gravity=“center”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:id="@+id/mylayout">

<com.zzci.light.TestButton

android:id="@+id/my_btn"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“click test”/>

</com.zzci.light.TestLinearLayout>

public class ListenerActivity extends Activity implements View.OnTouchListener, View.OnClickListener {

private TestLinearLayout mLayout;

private TestButton mButton;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mLayout = (TestLinearLayout) this.findViewById(R.id.mylayout);

mButton = (TestButton) this.findViewById(R.id.my_btn);

mLayout.setOnTouchListener(this);

mButton.setOnTouchListener(this);

mLayout.setOnClickListener(this);

mButton.setOnClickListener(this);

}

@Override

public boolean onTouch(View v, MotionEvent event) {

Log.i(null, “OnTouchListener–onTouch-- action=”+event.getAction()+" --"+v);

return false;

}

@Override

public void onClick(View v) {

Log.i(null, “OnClickListener–onClick–”+v);

}

}

到此基础示例的代码编写完成。没有啥难度,很简单易懂,不多解释了。

2-2 运行现象

当直接点击Button时打印现象如下:

TestLinearLayout dispatchTouchEvent

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值