android的多次点击事件的实现(有源码)





import com.example.mobilesafe74.utils.ConstantValue;
import com.example.mobilesafe74.utils.SpUtil;
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;
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];
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast_location);
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();
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);
bt_bottom.setVisibility(View.VISIBLE);
bt_top.setVisibility(View.INVISIBLE);
// 将以上规则作用在控件上面
tv_drag.setLayoutParams(LayoutParams);
tv_drag.setOnClickListener(new OnClickListener() {
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 startY;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 按下
// 初始位置的坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
case MotionEvent.ACTION_MOVE:// 移动
int moveX = (int) event.getRawX();
int moveY = (int) event.getRawY();
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;
}
return true;
}
return true;
}
return true;
}
if (top > mScreenheight / 2) {
bt_bottom.setVisibility(View.INVISIBLE);
bt_top.setVisibility(View.VISIBLE);
bt_bottom.setVisibility(View.VISIBLE);
bt_top.setVisibility(View.INVISIBLE);
// 重置坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
case MotionEvent.ACTION_UP:// 抬起
// 记录位置坐标
SpUtil.putInt(getApplicationContext(),
ConstantValue.LOCATION_X, tv_drag.getLeft());
SpUtil.putInt(getApplicationContext(),
ConstantValue.LOCATION_Y, tv_drag.getTop());
}
// 返回事件,只有触摸操作的时候用true,有点击事件记得加上return false
return false;
}
});
}
}
android的多次点击事件的实现(有源码)的更多相关文章
- Android Button四种点击事件和长按事件
项目XML代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- 查找和定位Android应用的按钮点击事件的代码位置基于Xposed Hook实现
本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80956455 在进行Android程序的逆向分析的时候,经常需要通过Androi ...
- Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用
如题,本文将介绍 listview的点击事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的注册点击事件 //注册点击事件 personListView.s ...
- android 入门 003 (点击事件)
点击事件 有四种实现方式. 1.内部类实现方式 1.0 package cn.rfvip.clickevent; import android.app.Activity; import android ...
- Android 给TextView添加点击事件
首先设定TextView的clickable属性为true. 可以在布局文件中进行设定,比如: <TextView android:id="@+id/phone" andro ...
- Android ListView中Item点击事件失效解决方案
欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布 ...
- Android 自定义View——自定义点击事件
每个人手机上都有通讯录,这是毫无疑问的,我们通讯录上有一个控件,在通讯录的最左边有一列从”#”到”Z”的字母,我们通过滑动或点击指定的字母来确定联系人的位置,进而找到联系人.我们这一节就通过开发这个控 ...
- Android 7.0 TextView点击事件无效修复方案
public class PostTextView extends TextView { private Handler handler = new Handler(Looper.getMainLoo ...
- 关于Android避免按钮重复点击事件
最近测试人员测试我们的APP的时候,喜欢快速点击某个按钮,出现一个页面出现多次,测试人员能不能禁止这样.我自己点击了几下,确实存在这个问题,也感觉用户体验不太好.于是乎后来我搜了下加一个方法放在我们U ...
随机推荐
- C#实现eval 进行四则运算(有码)
在JavaScript中实现四则运算很简单,只需要调用eval函数就行了,但是不知道什么原因万能的.NET却没有封装这个函数~ 在这里为大家封装了一个C#版本的eval函数,具体的设计参考了<大 ...
- Python+Selenium练习篇之11-浏览器上前进和后退操作
本文来介绍上如何,利用webdriver中的方法来演示浏览器中地址栏旁边的前进和后退功能. 相关脚本代码如下: # coding=utf-8import timefrom selenium impor ...
- Python-S9-Day125-Web微信&爬虫框架之scrapy
01 今日内容概要 02 内容回顾:爬虫 03 内容回顾:网络和并发编程 04 Web微信之获取联系人列表 05 Web微信之发送消息 06 为什么request.POST拿不到数据 07 到底使用j ...
- Django框架学习-01Django介绍
01-Django介绍 02-HTTP协议介绍 01-Django介绍 1.什么是Web框架? 随着Web最新发展趋势的不断升级,Web项目开发也越来越难,而且需要花费更多的开发时间.所以,Web程序 ...
- CSU-2116 Polyline Simplification
CSU-2116 Polyline Simplification Description Mapping applications often represent the boundaries of ...
- [错误解决]paramiko.ssh_exception.SSHException: Error reading SSH protocol banner 设置
报错信息 上午的时候数据组的同事跟我说有几个程序报错,经过查看log发现找到报错信息: paramiko.ssh_exception.SSHException: Error reading SSH p ...
- idea的ssm搭建(复制)
1.工具/原料 • apache-tomcat-7.0.63 http://download.csdn.net/detail/lxfhahaha/9778163 • apache-maven-3.3. ...
- getRequestURI,getRequestURL的区别,获取各种路径的方法
getRequestURI,getRequestURL的区别 test1.jsp======================= <a href ="test.jsp?p=fuck&qu ...
- 【转】unity自带寻路Navmesh入门教程(二)
http://liweizhaolili.blog.163.com/blog/static/16230744201271210237616/ 上一节简单介绍了NavMesh寻路的基本用法,这次来介绍一 ...
- table中填写数据并批量增加
<table class = "table jtable table-bordered table-striped hide" id = "table_1" ...