三次点击事件的原理图:数组的复制(android源码的调用):

下面就是第一步:

创建long数组,里面的数字代表点击的次数。
下面是主要代码实现:

system.arraycopy();里面的参数描述:

 
最后就是在if里面实现你想要的按钮的点击逻辑,不过最后一点很重要,当按钮还有触摸的点击事件的时候,有一点:

一定记得返回false;
下面贴出我的源码:tv_drag实现双击居中方法(里面有触摸事件):
 
 
package com.example.mobilesafe74.activity;
 
import com.example.mobilesafe74.R;
import com.example.mobilesafe74.utils.ConstantValue;
import com.example.mobilesafe74.utils.SpUtil;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
 
public class ToastLocationActivity extends Activity {
 private ImageView tv_drag;
 private Button bt_top;
 private Button bt_bottom;
 private WindowManager mWm;
 private int mScreenwidth;
 private int mScreenheight;
 private long[] mhits = new long[2];
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_toast_location);
  initUI();
 }
 
 private void initUI() {
  // 当前可以拖拽的图片控件
  tv_drag = (ImageView) findViewById(R.id.tv_drag);
  bt_top = (Button) findViewById(R.id.bt_top);
  bt_bottom = (Button) findViewById(R.id.bt_bottom);
  mWm = (WindowManager) getSystemService(WINDOW_SERVICE);
  mScreenwidth = mWm.getDefaultDisplay().getWidth();
  mScreenheight = mWm.getDefaultDisplay().getHeight();
 
  int locationX = SpUtil.getInt(getApplicationContext(),
    ConstantValue.LOCATION_X, 0);
  int locationY = SpUtil.getInt(getApplicationContext(),
    ConstantValue.LOCATION_Y, 0);
 
  // 左上坐标作用在控件上面
  // 指定宽高都是w w
  LayoutParams LayoutParams = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
  LayoutParams.leftMargin = locationX;
  LayoutParams.topMargin = locationY;
  if (LayoutParams.topMargin > mScreenheight / 2) {
   bt_bottom.setVisibility(View.INVISIBLE);
   bt_top.setVisibility(View.VISIBLE);
 
  } else {
   bt_bottom.setVisibility(View.VISIBLE);
   bt_top.setVisibility(View.INVISIBLE);
 
  }
  // 将以上规则作用在控件上面
  tv_drag.setLayoutParams(LayoutParams);
  tv_drag.setOnClickListener(new OnClickListener() {
 
   @Override
   public void onClick(View arg0) {
    System.arraycopy(mhits, 1, mhits, 0, mhits.length - 1);
    mhits[mhits.length - 1] = SystemClock.uptimeMillis();
    if (mhits[mhits.length - 1] - mhits[0] < 500) {
     // 满足双击事件后,居中
     int left = mScreenwidth / 2 - tv_drag.getWidth() / 2;
     int right = mScreenheight / 2 - tv_drag.getHeight() / 2;
     int top = mScreenwidth / 2 + tv_drag.getWidth() / 2;
     int bottom = mScreenheight / 2 + tv_drag.getHeight() / 2;
 
     // 显示出来
     tv_drag.layout(left, top, right, bottom);
 
    }
 
   }
  });
 
  // 可以拖拽的点击事件
  tv_drag.setOnTouchListener(new OnTouchListener() {
 
   private int startX;
   private int startY;
 
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:// 按下
     // 初始位置的坐标
     startX = (int) event.getRawX();
     startY = (int) event.getRawY();
 
     break;
    case MotionEvent.ACTION_MOVE:// 移动
     int moveX = (int) event.getRawX();
     int moveY = (int) event.getRawY();
 
     int disX = moveX - startX;
     int disY = moveY - startY;
 
     // 当前控件的上下左右的坐标
     int left = tv_drag.getLeft() + disX;
     int top = tv_drag.getTop() + disY;
     int right = tv_drag.getRight() + disX;
     int bottom = tv_drag.getBottom() + disY;
     // 容错处理
     if (left < 0) {
      return true;
     }
 
     if (right > mScreenwidth) {
      return true;
     }
 
     if (top < 0) {
      return true;
     }
 
     if (bottom > mScreenheight - 22) {
      return true;
     }
     if (top > mScreenheight / 2) {
      bt_bottom.setVisibility(View.INVISIBLE);
      bt_top.setVisibility(View.VISIBLE);
 
     } else {
      bt_bottom.setVisibility(View.VISIBLE);
      bt_top.setVisibility(View.INVISIBLE);
 
     }
 
     // 告知控件,按计算的坐标去展示
 
     tv_drag.layout(left, top, right, bottom);
     // 重置坐标
     startX = (int) event.getRawX();
     startY = (int) event.getRawY();
 
     break;
    case MotionEvent.ACTION_UP:// 抬起
     // 记录位置坐标
     SpUtil.putInt(getApplicationContext(),
       ConstantValue.LOCATION_X, tv_drag.getLeft());
     SpUtil.putInt(getApplicationContext(),
       ConstantValue.LOCATION_Y, tv_drag.getTop());
 
     break;
    }
    // 返回事件,只有触摸操作的时候用true,有点击事件记得加上return false
    return false;
   }
  });
 }
}
 
 

android的多次点击事件的实现(有源码)的更多相关文章

  1. Android Button四种点击事件和长按事件

    项目XML代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...

  2. 查找和定位Android应用的按钮点击事件的代码位置基于Xposed Hook实现

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80956455 在进行Android程序的逆向分析的时候,经常需要通过Androi ...

  3. Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用

    如题,本文将介绍 listview的点击事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的注册点击事件 //注册点击事件 personListView.s ...

  4. android 入门 003 (点击事件)

    点击事件 有四种实现方式. 1.内部类实现方式 1.0 package cn.rfvip.clickevent; import android.app.Activity; import android ...

  5. Android 给TextView添加点击事件

    首先设定TextView的clickable属性为true. 可以在布局文件中进行设定,比如: <TextView android:id="@+id/phone" andro ...

  6. Android ListView中Item点击事件失效解决方案

    欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布 ...

  7. Android 自定义View——自定义点击事件

    每个人手机上都有通讯录,这是毫无疑问的,我们通讯录上有一个控件,在通讯录的最左边有一列从”#”到”Z”的字母,我们通过滑动或点击指定的字母来确定联系人的位置,进而找到联系人.我们这一节就通过开发这个控 ...

  8. Android 7.0 TextView点击事件无效修复方案

    public class PostTextView extends TextView { private Handler handler = new Handler(Looper.getMainLoo ...

  9. 关于Android避免按钮重复点击事件

    最近测试人员测试我们的APP的时候,喜欢快速点击某个按钮,出现一个页面出现多次,测试人员能不能禁止这样.我自己点击了几下,确实存在这个问题,也感觉用户体验不太好.于是乎后来我搜了下加一个方法放在我们U ...

随机推荐

  1. C#实现eval 进行四则运算(有码)

    在JavaScript中实现四则运算很简单,只需要调用eval函数就行了,但是不知道什么原因万能的.NET却没有封装这个函数~ 在这里为大家封装了一个C#版本的eval函数,具体的设计参考了<大 ...

  2. Python+Selenium练习篇之11-浏览器上前进和后退操作

    本文来介绍上如何,利用webdriver中的方法来演示浏览器中地址栏旁边的前进和后退功能. 相关脚本代码如下: # coding=utf-8import timefrom selenium impor ...

  3. Python-S9-Day125-Web微信&爬虫框架之scrapy

    01 今日内容概要 02 内容回顾:爬虫 03 内容回顾:网络和并发编程 04 Web微信之获取联系人列表 05 Web微信之发送消息 06 为什么request.POST拿不到数据 07 到底使用j ...

  4. Django框架学习-01Django介绍

    01-Django介绍 02-HTTP协议介绍 01-Django介绍 1.什么是Web框架? 随着Web最新发展趋势的不断升级,Web项目开发也越来越难,而且需要花费更多的开发时间.所以,Web程序 ...

  5. CSU-2116 Polyline Simplification

    CSU-2116 Polyline Simplification Description Mapping applications often represent the boundaries of ...

  6. [错误解决]paramiko.ssh_exception.SSHException: Error reading SSH protocol banner 设置

    报错信息 上午的时候数据组的同事跟我说有几个程序报错,经过查看log发现找到报错信息: paramiko.ssh_exception.SSHException: Error reading SSH p ...

  7. idea的ssm搭建(复制)

    1.工具/原料 • apache-tomcat-7.0.63 http://download.csdn.net/detail/lxfhahaha/9778163 • apache-maven-3.3. ...

  8. getRequestURI,getRequestURL的区别,获取各种路径的方法

    getRequestURI,getRequestURL的区别 test1.jsp======================= <a href ="test.jsp?p=fuck&qu ...

  9. 【转】unity自带寻路Navmesh入门教程(二)

    http://liweizhaolili.blog.163.com/blog/static/16230744201271210237616/ 上一节简单介绍了NavMesh寻路的基本用法,这次来介绍一 ...

  10. table中填写数据并批量增加

    <table class = "table jtable table-bordered table-striped hide" id = "table_1" ...