Android自定义长按事件
2012-12-24 16:49
190 查看
Android系统自带了长按事件,setOnLongClickListener即可监听。但是有时候,你不希望用系统的长按事件,比如当希望长按的时间更长一点的时候。这时候就需要自己来定义这个长按事件了。
下面是去年我写代码的时候,自定义长按事件的方式:
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView1 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//是否释放了
private boolean isReleased;
//计数器,防止多次点击导致最后一次形成longpress的时间变短
private int mCounter;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView1(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
mCounter--;
//计数器大于0,说明当前执行的Runnable不是最后一次down产生的。
if(mCounter>0 || isReleased || isMoved) return;
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mCounter++;
isReleased = false;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
}
break;
case MotionEvent.ACTION_UP:
//释放了
isReleased = true;
break;
}
return true;
}
}
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView1 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//是否释放了
private boolean isReleased;
//计数器,防止多次点击导致最后一次形成longpress的时间变短
private int mCounter;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView1(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
mCounter--;
//计数器大于0,说明当前执行的Runnable不是最后一次down产生的。
if(mCounter>0 || isReleased || isMoved) return;
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mCounter++;
isReleased = false;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
}
break;
case MotionEvent.ACTION_UP:
//释放了
isReleased = true;
break;
}
return true;
}
}
代码里注释的比较清楚。主要思路是在down的时候,让一个Runnable一段时间后执行,如果时间到了,没有移动超过定义的阈值,也没有释放,则触发长按事件。在真实环境中,当长按触发之后,还需要将后来的move和up事件屏蔽掉。此处是示例,就略去了。
下面讲讲第二种方式:
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView2 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView2(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
removeCallbacks(mLongPressRunnable);
}
break;
case MotionEvent.ACTION_UP:
//释放了
removeCallbacks(mLongPressRunnable);
break;
}
return true;
}
}
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView2 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView2(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
removeCallbacks(mLongPressRunnable);
}
break;
case MotionEvent.ACTION_UP:
//释放了
removeCallbacks(mLongPressRunnable);
break;
}
return true;
}
}
思路跟第一种差不多,不过,在移动超过阈值和释放之后,会将Runnable从事件队列中remove掉,长按事件也就不会再触发了。源码中实现长按的原理也基本如此。
下面是去年我写代码的时候,自定义长按事件的方式:
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView1 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//是否释放了
private boolean isReleased;
//计数器,防止多次点击导致最后一次形成longpress的时间变短
private int mCounter;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView1(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
mCounter--;
//计数器大于0,说明当前执行的Runnable不是最后一次down产生的。
if(mCounter>0 || isReleased || isMoved) return;
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mCounter++;
isReleased = false;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
}
break;
case MotionEvent.ACTION_UP:
//释放了
isReleased = true;
break;
}
return true;
}
}
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView1 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//是否释放了
private boolean isReleased;
//计数器,防止多次点击导致最后一次形成longpress的时间变短
private int mCounter;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView1(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
mCounter--;
//计数器大于0,说明当前执行的Runnable不是最后一次down产生的。
if(mCounter>0 || isReleased || isMoved) return;
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mCounter++;
isReleased = false;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
}
break;
case MotionEvent.ACTION_UP:
//释放了
isReleased = true;
break;
}
return true;
}
}
代码里注释的比较清楚。主要思路是在down的时候,让一个Runnable一段时间后执行,如果时间到了,没有移动超过定义的阈值,也没有释放,则触发长按事件。在真实环境中,当长按触发之后,还需要将后来的move和up事件屏蔽掉。此处是示例,就略去了。
下面讲讲第二种方式:
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView2 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView2(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
removeCallbacks(mLongPressRunnable);
}
break;
case MotionEvent.ACTION_UP:
//释放了
removeCallbacks(mLongPressRunnable);
break;
}
return true;
}
}
package chroya.fun;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
public class LongPressView2 extends View{
private int mLastMotionX, mLastMotionY;
//是否移动了
private boolean isMoved;
//长按的runnable
private Runnable mLongPressRunnable;
//移动的阈值
private static final int TOUCH_SLOP = 20;
public LongPressView2(Context context) {
super(context);
mLongPressRunnable = new Runnable() {
@Override
public void run() {
performLongClick();
}
};
}
public boolean dispatchTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
isMoved = false;
postDelayed(mLongPressRunnable, ViewConfiguration.getLongPressTimeout());
break;
case MotionEvent.ACTION_MOVE:
if(isMoved) break;
if(Math.abs(mLastMotionX-x) > TOUCH_SLOP
|| Math.abs(mLastMotionY-y) > TOUCH_SLOP) {
//移动超过阈值,则表示移动了
isMoved = true;
removeCallbacks(mLongPressRunnable);
}
break;
case MotionEvent.ACTION_UP:
//释放了
removeCallbacks(mLongPressRunnable);
break;
}
return true;
}
}
思路跟第一种差不多,不过,在移动超过阈值和释放之后,会将Runnable从事件队列中remove掉,长按事件也就不会再触发了。源码中实现长按的原理也基本如此。
相关文章推荐
- (Android学习之路)Android中listView结合自定义适配器,并实现item中button点击事件
- Android_自定义View---三种事件的触发、自定义View属性
- Android自定义View滑动事件处理总结
- Android自定义view之事件传递机制
- Android中View的事件体系(3)——自定义横向滚动viewGroup
- Android自定义适配器和ListView的点击事件相结合的使用
- android自定义长按事件
- 2014-10-27Android学习------布局处理(八)------自定义ListView的监听事件和Adapter的实现-----城市列表应用程序
- Android自定义控件系列 十:利用添加自定义布局来搞定触摸事件的分发,解决组合界面中特定控件响应特定方向的事件
- android自定义日历并添加事件
- Android 自定义view的监听事件
- android自定义View及事件
- React Native API模块BackAndroid自定义返回键事件处理
- Android自定义View中的常用方法(距离、位置、点击事件)
- Android中自定义ScrollView的滑动监听事件,并在滑动时渐变标题栏背景颜色
- 控件视图Android 自定义视图容器控件拦截ontouch事件传递给其子控件
- Android自定义View与添加点击事件
- Android自定义控件之触摸事件分发机制
- Android——自定义多击事件
- 自定义按钮实现Android 返回按钮事件