项目需求:

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>

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

最新文章

  1. HDU 3642 Get The Treasury (线段树扫描线)
  2. CodeForces460B. Little Dima and Equation
  3. linux环境下给文件加密/解密的方法
  4. nyoj 15 括号匹配(二)动态规划
  5. 自动化基础普及之selenium是啥?
  6. 解决 Eclipse 项目有红感叹号的方法
  7. 算法:comparable比较器的排序原理实现(二叉树中序排序)
  8. JSON的操作
  9. UVA 10765 Doves and bombs
  10. ADO简单封装(MFC)
  11. Shrio登陆验证实例详细解读(转)
  12. Autopep8的使用
  13. 小程序开发基础-view视图容器
  14. jupyter-notebook后home页面空白问题
  15. zzw原创_mysql脚本打印出提示信息
  16. PDF 补丁丁 0.6.0.3427 版发布(修复提取图片问题)
  17. Java基础巩固计划
  18. VMware下三种网络连接模式介绍
  19. jQuery(七):节点操作
  20. python包/模块路径

热门文章

  1. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_all_from_list(self, locator)
  2. PHP.ini 配置文件解析
  3. C#开源框架(整理)
  4. collectionView/tableview刷新时关闭动画无效
  5. EventSource (node.js 与 OC)
  6. HTML5每日一练之视频标签的应用
  7. HD1060Leftmost Digit
  8. cisco tftp 备份/恢复
  9. LeetCode7:Reverse Integer
  10. 用python实现远程复制 (scp + expect )