项目需求:

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. 页面性能测试&提升方式

    性能测试包括:web系统页面测试.web系统后台测试 2种方式来提升你的web 应用程序的速度: ● 减少请求和响应的往返次数 ● 减少请求和响应的往返字节大小. 详细的看此文http://www.5 ...

  2. 在Mac OS X 通过抓包、“第三方下载工具”加速下载、安装APP或系统

    #!/bin/bash ######################################################################################## ...

  3. Asp.Net学习进度备忘(第一步:ASP.NET Web Forms)

    书签:“Web Pages”和“MVC”跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.ASP. ...

  4. 【LeetCode】237 & 203 - Delete Node in a Linked List & Remove Linked List Elements

    237 - Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly l ...

  5. Lucene 入门需要了解的东西

    全文搜索引擎的原理网上大段的内容,要想深入的学习,最好的办法就是先用一下,lucene 发展比较快,下面是写第一个demo  要注意的一些事情: 1.Lucene的核心jar包,下面几个包分别位于不同 ...

  6. [质疑]编程之美求N!的二进制最低位1的位置的问题

    引子:编程之美给出了求N!的二进制最低位1的位置的二种思路,但是呢?但是呢?不信你仔细听我道来. 1.编程之美一书给出的解决思路 问题的目标是N!的二进制表示中最低位1的位置.给定一个整数N,求N!二 ...

  7. For Microsoft Azure Network VNET to VNET Connection

    将一个 Azure 虚拟网络 (VNet) 连接到另一个 Azure 虚拟网络非常类似于将虚拟网络连接到本地站点位置.这两种连接类型都使用虚拟网络网关通过 IPsec/IKE 提供安全隧道.连接的 V ...

  8. 解决getOutputStream() has already been called for this response

    http://qify.iteye.com/blog/747842 —————————————————————————————————————————————————— getOutputStream ...

  9. -ms-viewport的问题

    Windows 8 中的 Internet Explorer 10 和 Windows Phone 8 Internet Explorer 10 doesn't differentiate devic ...

  10. 查询processlist具体信息

    SELECT * FROM information_schema.PROCESSLIST WHERE HOST LIKE '%172.16.10.22%' AND COMMAND <> ' ...