有趣的Touch事件转移
来源:互联网 发布:企业网络搭建论文 编辑:程序博客网 时间:2024/05/16 10:06
有趣的Touch事件转移。
点击一个Button,另一个Button却被点击了。
关键是点击的那个Button将event分发dispatchTouchEvent到另一个Button那里。
例子:
MainActivity
package com.example.touchdemo7;import com.customview.MyButton;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {MyButton myButton;Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1=(Button)this.findViewById(R.id.button1);button1.setOnClickListener(new OnClickListener(){int count=0;@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根count++;button1.setText("我被点击了+"+count);}});myButton=(MyButton)this.findViewById(R.id.myButton1);myButton.setDispatchToView(button1);}}
MyFrameLayout
package com.customview;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.widget.FrameLayout;public class MyFrameLayout extends FrameLayout{public MyFrameLayout(Context context, AttributeSet attrs) {super(context, attrs);// TODO 自动生成的构造函数存根}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {// TODO 自动生成的方法存根return false;}}
MyButton
package com.customview;import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.Button;public class MyButton extends Button{View dispatchToView;public MyButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO 自动生成的构造函数存根}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {// TODO 自动生成的方法存根if(dispatchToView==null){return super.dispatchTouchEvent(event);}else{return dispatchToView.dispatchTouchEvent(event);}}public View getDispatchToView() {return dispatchToView;}public void setDispatchToView(View dispatchToView) {this.dispatchToView = dispatchToView;}}
activity_main
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.customview.MyFrameLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="bottom" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </com.customview.MyFrameLayout> <com.customview.MyButton android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点我" /></FrameLayout>
0 0
- 有趣的Touch事件转移
- View的touch事件
- android的touch事件
- ImageView的touch事件
- Touch事件的派发
- js的touch事件
- RecyclerView的touch事件
- UIScrollView的touch事件特点
- 获取UIWebView的touch事件
- touch事件的响应过程
- touch事件需要注意的
- android的touch事件详解
- android的Touch事件解析
- 禁用view的touch事件
- View的touch事件分发。
- Android的Touch事件处理
- touch事件的分发机制
- Touch事件的传递机制?
- 机器学习之多变量线性回归(Linear Regression with multiple variables)
- Java中线程(Thread)知识概括
- linux系统,Oracle 命令建表及用户,导入导出 dmp文件
- Unreal Engine 4 初始化流程
- PHP版本-对应的新特性
- 有趣的Touch事件转移
- javascript的JSON对象
- Nginx 下配置支持thinkphp 伪静态
- Windows系统版本判断
- 如何使用eclipse(ADT)使用Android Studio一句话就能添加的依赖
- 题目1172:哈夫曼树
- 根据图片比例设置控件的宽高,避免图片拉伸
- Missing styles. Is the correct theme chosen for this layout
- 一小时建立终生受用的阅读操作系统-笔记