一、如果用户在获得焦点的VIEW上按KEYCODE_DPAD_CENTER或KEYCODE_ENTER键,即OK键后,在VIEW的onKeyDown方法中会开启一个延迟线程,在延迟线程中会去回调onLongClick()方法,代码如下: 在如下代码
一、如果用户在获得焦点的VIEW上按KEYCODE_DPAD_CENTER或KEYCODE_ENTER键,即OK键后,在VIEW的onKeyDown方法中会开启一个延迟线程,在延迟线程中会去回调onLongClick()方法,代码如下:
在如下代码中开始延迟线程:
[java] view plain copy print ?
- public boolean onKeyDown(int
- boolean result = false;
- switch
- case
- case
- if
- return true;
- }
- // Long clickable items don't necessarily have to be clickable
- if
- (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
- 0)) {
- true);
- if
- 0);//在这里开启延迟线程
- }
- return true;
- }
- break;
- }
- }
- return
- }
public boolean onKeyDown(int keyCode, KeyEvent event) {
boolean result = false;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER: {
if ((mViewFlags & ENABLED_MASK) == DISABLED) {
return true;
}
// Long clickable items don't necessarily have to be clickable
if (((mViewFlags & CLICKABLE) == CLICKABLE ||
(mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
(event.getRepeatCount() == 0)) {
setPressed(true);
if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
postCheckForLongClick(0);//在这里开启延迟线程
}
return true;
}
break;
}
}
return result;
}
延迟线程代码如下:
[java] view plain copy print ?
- class CheckForLongPress implements
- private int
- public void
- if (isPressed() && (mParent != null)
- && mOriginalWindowAttachCount == mWindowAttachCount) {
- if (performLongClick()) { //这里回调onLongClick()方法
- true;
- }
- }
- }
- public void
- mOriginalWindowAttachCount = mWindowAttachCount;
- }
- }
class CheckForLongPress implements Runnable {
private int mOriginalWindowAttachCount;
public void run() {
if (isPressed() && (mParent != null)
&& mOriginalWindowAttachCount == mWindowAttachCount) {
if (performLongClick()) { //这里回调onLongClick()方法
mHasPerformedLongPress = true;
}
}
}
public void rememberWindowAttachCount() {
mOriginalWindowAttachCount = mWindowAttachCount;
}
}
二、如果用户在触摸屏上长按某个VIEW,VIEW中首先会检测在这个触摸点移动没,如果没有移动再开启一个延迟线程去回调onLongClick()方法,代码如下:
在View中的onTouchEvent中的DOWN事件中:
[java] view plain copy print ?
- case
- if (mPendingCheckForTap == null) {
- new
- }
- mPrivateFlags |= PREPRESSED;
- false;
- //开始延迟线程检测触摸点移动没
- break;
case MotionEvent.ACTION_DOWN:
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
mPrivateFlags |= PREPRESSED;
mHasPerformedLongPress = false;
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());//开始延迟线程检测触摸点移动没
break;
如果没有移动,则会开启一个延迟线程回调onLongClick()方法:
[java] view plain copy print ?
- private final class CheckForTap implements
- public void
- mPrivateFlags &= ~PREPRESSED;
- mPrivateFlags |= PRESSED;
- refreshDrawableState();
- if
- //开启延迟线程回调onLongClick()方法
- }
- }
- }
private final class CheckForTap implements Runnable {
public void run() {
mPrivateFlags &= ~PREPRESSED;
mPrivateFlags |= PRESSED;
refreshDrawableState();
if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
postCheckForLongClick(ViewConfiguration.getTapTimeout());//开启延迟线程回调onLongClick()方法
}
}
}
剩下来就和按键长按一样的处理了。
在其中要注意二个参数:
ViewConfiguration.getTapTimeout() 是用于检测触摸点有没有移动的时间,默认为115毫秒
ViewConfiguration.getLongPressTimeout() 是用于检测是不是长按的时间,默认为500毫秒