项目需求:

android中只有单击和其他事件,其实都是由OnTouch事件演变而来;最近有项目要求双击全屏,所以就试着实现了下

具体实现如下:

1.MainActivity.java实现:

public class MainActivity extends Activity implements OnTouchListener {
private long firstClick;
private long lastClick;
// 计算点击的次数
private int count; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.ontourch).setOnTouchListener(this);
} @Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 如果第二次点击 距离第一次点击时间过长 那么将第二次点击看为第一次点击
if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) {
count = 0;
}
count++;
if (count == 1) {
firstClick = System.currentTimeMillis();
} else if (count == 2) {
lastClick = System.currentTimeMillis();
// 两次点击小于300ms 也就是连续点击
if (lastClick - firstClick < 300) {// 判断是否是执行了双击事件
System.out.println(">>>>>>>>执行了双击事件"); }
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
} }

2.main_activity.xml实现:

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/ontourch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>

好了实现就是这么简单;有问题可以@我

andorid 中如何实现双击事件的更多相关文章

  1. C#Winform ListView中没有Item双击事件的两种实现方法!

    第一种: //if (this.listView1.FocusedItem != null) //{ // if (this.listView1.SelectedItems != null) // { ...

  2. easyui-datetimebox 控件绑定双击事件实现自动选中当前日期时间

    本方法是在不改变原 js 的情况下,通过扩展方法来实现本目的 首先在 datetimebox 控件中扩展一个 绑定双击事件 的方法 $.extend($.fn.datetimebox.methods, ...

  3. WPF中如何将ListViewItem双击事件绑定到Command

    今天的博客将介绍如何实现ListViewItem双击事件绑定到ViewModel中的Command.实现方法借助了Style中的EventSetter,请看下面的详细代码: <ListView ...

  4. asp.net中的ListBox控件添加双击事件

    问题:在Aspx页里的ListBox A中添加双击事件,将选中项添加到另一个ListBox B中,双击ListBox B中的选中项,删除当前选中项 页面: <asp:ListBox ID=&qu ...

  5. Angular JS中双击事件ng-dblclick避免同时触发两次单击事件ng-click的解决方案

    有些需求中,需要一个元素上既有双击事件,也有单击事件,而两者实现的效果不一样. 这时可以使用ng-dblclick与ng-click来实现需求,但是要避免浏览器将双击事件误认为是两次单击事件,从而出现 ...

  6. WPF: 在 MVVM 设计中实现对 ListViewItem 双击事件的响应

    ListView 控件最常用的事件是 SelectionChanged:如果采用 MVVM 模式来设计 WPF 应用,通常,我们可以使用行为(如 InvokeCommandAction)并结合命令来实 ...

  7. C# Note16: wpf window 中添加enter和双击事件

     一.添加回车(enter)事件 在C#编程时,有时希望通过按回车键,控件焦点就会自动从一个控件跳转到下一个控件进行操作. 以用户登录为例,当输入完用户名和密码后, 需要点击登录按钮,而登录按钮必须获 ...

  8. WinForm中DataGridView的使用(四) - 区分单双击事件

    虽然DataGridView单双击事件都有,但双击事件其实也会触发单击事件的处理,所以如果双击事件和单击事件的行为不同,或者双击时不想触发单击事件,或者单击事件会阻塞双击事件的处理时(比如单击后会有弹 ...

  9. EasyUI中datagrid双击事件

    EasyUI中datagrid双击事件 在jsp文件底部增加代码: <script type="text/javascript"> //数据表双击事件 $('#tabl ...

随机推荐

  1. <转>selenium+python+API分类总结

    分类 方法 方法描述 客户端操作 __init__(self, host, port, browserStartCommand, browserURL) 构造函数.host:selenium serv ...

  2. 【转】linux下cpio命令使用

    转自:http://www.51testing.com/html/32/498132-816949.html 功能说明:备份文件. 补充说明:cpio是用来建立,还原备份档的工具程序,它可以加入,解开 ...

  3. LR录制测试脚本

    1.录制的业务流程 2.录制脚本 3.查看脚本

  4. HP Web Tours分析

    1.启动Web Tours 2.首页结构 3.预定机票

  5. LoadRunner界面分析(二)

    1.Controller 2.创建运行场景 3.方案设计 4.Resuls settting 5.监视方案

  6. 淘宝API开发(三)

    自动登录到淘宝定时获取订单: C#控制台程序 第一步,获得淘宝真实登录地址.淘宝授权地址(https://oauth.taobao.com/authorize?response_type=token& ...

  7. bzoj1833 digit

    这道题其实挺水,只是写的时候需要想清楚.我的方法是: 1.将[a,b]转化为[0,b+1)-[0,a) 2.预处理出非0的v在区间[0,10^p)出现次数以及0在区间[0,10^p)出现数 3.将一个 ...

  8. 重新安装python

    1. 在上次进行安装python的时候,很多东西不能用,例如后退键,删除键,都是不能在命令行中使用,主要原因是在编译python的时候,相关的安装包没有进行安装,从而导致出现乱码,在编译最新版本的py ...

  9. signal()函数

    转自:http://blog.csdn.net/sddzycnqjn/article/details/7285760 1. 信号概念 信号是进程在运行过程中,由自身产生或由进程外部发过来的消息(事件) ...

  10. Linux Shell脚本教程

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...