Android 事件中 OnTouch 事件

Android 事件中 OnTouch  事件:

实现的方式:

1 监听

2 回调

1 监听:

package com.example.conflicttest;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnTouchListener {

	private LinearLayout subLayout;
	private ImageView imgView;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		subLayout = (LinearLayout) findViewById(R.id.subLayout);
		imgView = (ImageView) findViewById(R.id.imgView);

//		subLayout.setOnTouchListener(this);
		/**
		 * 事件监听  第一种方案: 设置监听!
		 * */
		imgView.setOnTouchListener(this);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * return  false 事件 返回调用,传递给 父类!
	 *
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub

		int action = event.getAction();

		float rawX = event.getRawX();
		float rawY = event.getRawY();

		float x = event.getX();
		float y = event.getY();

		Log.d("lvoe", "----on touch:" + v + " raw[" + rawX + "," + rawY + "],xy[" + x
				+ "," + y + "]");

		return true;
	}

}

2 回调“:

package com.example.conflicttest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * @author hades
 *
 *
 */
public class CustomLinerLayout extends LinearLayout {

	public CustomLinerLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public CustomLinerLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public CustomLinerLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	/**
	 *
	 * 第二中实现方式:
	 *
	 * 自定义 视图类:
	 *
	 *   处理 触摸事件,可以使用 onTouch()
	 *
	 * */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub

		float rawX = event.getRawX();
		float rawY = event.getRawY();

		float x = event.getX();
		float y = event.getY();

		Log.d("lvoe", "----on touch:" + " raw x y = [" + rawX + "," + rawY + "],x,y=[" + x
				+ "," + y + "]");

		return super.onTouchEvent(event);

	}

}
时间: 2024-08-28 20:03:53

Android 事件中 OnTouch 事件的相关文章

android事件系列-onTouch事件与手势操作

提示记忆:应用流程:在Activity中对控件执行 view.setOnTouchListener( OnTouchListener i);实现里面的OnTouchListener 接口中的方法,重点再于理解里面的方法的实现步骤, 触摸,手势操作已经很好的融入了我们的生活.那么Android开发中触摸事件要如何捕捉?如何处理?如何识别手势?事件的传递机制又是怎么样的?下面我们将通过一个小例子来进行这方面的学习. 先看效果图 <ignore_js_op> 如上图所示,就是一个跟随手指移动的按钮.

事件中嵌套事件而出现的事件累积!

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="js/jquery-172.js"></script> <style> .demo1{width: 50px;height: 50px;mar

Android ListView中Button事件 点击冲突问题的解决方案

今天遇到一个问题:在设置listview  自定义adapter的时候 ,点击item有点击效果,然后点击item布局文件中的某个button 的时候  也同时触发了listview的点击效果, 解决方案是: 1.在adapter布局文件中添加 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk

Android中的事件分发机制

1. 一个小问题引发的思考 2. 通过源码探索View中的事件分发机制 3.通过源码探索ViewGroup的事件分发机制 最近的一个项目中涉及,布局为一个RelativeLayout包含了一个EditText和一个Button,当点击EditText时,弹出软键盘,点击RelativeLayout中除了EditText和Button之外其它的地方时,收起软键盘. 实现起来很简单,为EditText和RelativeLayout分别注册一个onTouch事件,为Button注册一个click事件,

Android View的onTouch和onClick和onLongClick事件

这三个事件的调用顺序是: onTouch->onLongClick->onClick 先看这三个事件的处理函数: public boolean onTouch(View v, MotionEvent event): public boolean onLongClick(View v): public void onClick(View v): 看到三个函数的返回值,只有onClick是void,而onTouch和onLongClick是boolean,原因是系统对这些事件的处理是有条件,必须满

js中的事件 bom对象 dom对象.

3.事件 什么是事件?为什么使用事件? 我们学习事件首先了解一些概念 事件源 事件 监听器 事件源:事件的源头(也就是这个事件是由某某触发的,那么我们就管其叫事件源) 监听器:监听事件发生的组件.那么监听器要想监听事件是否发生,必须注册监听(绑定监听) js中常用的事件 1.onclick 鼠标点击某个对象 我们在开发中一般会对按钮,文本框或radio checkbox等进行onclick操作. 2.onblur 失去焦点 3.onfocus 获去焦点 4.onchange 改变域的内容 针对于

回调在事件中的妙用

回调定义 CallBack: A callback is a function that is passed as an argument to another function and is executed after its parent function has completed. ### 回调: 回头调用,函数 A 的事先干完,回头再调用函数 B. 函数 A 的参数为函数 B, 函数 B 被称为回调函数.至于为何要用参数的形式传入,而不是直接在 A 中直接调用 B 函数,主要是为了变

C#中窗口关闭时没有取消事件订阅导致事件重复执行的解决方法

场景 C#中委托与事件的使用-以Winform中跨窗体传值为例: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100150700 参考上面的博客. 现在是在主页面点击按钮时弹出窗体,在窗体的load事件中进行事件的订阅,然后关闭窗体,再次打开时执行触发事件后, 订阅的事件就会执行两次,依次类推. 这是因为在窗体关闭的时候没有将原来的事件订阅解除掉. 注: 博客主页: https://blog.csdn.net/badao_l

【转】Android中的事件分发和处理

原文链接:http://www.apkbus.com/home.php?mod=space&uid=705730&do=blog&id=61207 上次跟大家分享了一下自定义View的一下要点,这次跟大家聊一下View的事件分发及处理,为什么主题都是View,因为作为一名初级应用层Android工程师,跟我打交道最多的莫过于各种各样的View,只有详细了解他们各自的习性,才能更好地跟他们沟通交流,做出自己想要的效果. 基础储备 View.MotionEvent 我们都能详细地说出A