gpt4 book ai didi

android - 长按 Clickable span 在点击被释放之前不会触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:17 25 4
gpt4 key购买 nike

我需要一个可点击范围,以便在我的应用程序中同时具有正常点击和长按方法,我在这里 ( In Android - How can I register only long clicks using a ClickableSpan ) 发现我可以扩展 LinkMovementMethod 类和 ClickableSpan 类以允许这样做,但目前长按和短按都有效,但是长按而不是触发长按操作当项目被按下足够长的时间时,它会等到您释放项目才能触发。这是我的扩展类代码:

LinkMovementClass

import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.MotionEvent;
import android.widget.TextView;

public class LongClickLinkMovementMethod extends LinkMovementMethod {

private Long lastClickTime = 0l;
private int lastX = 0;
private int lastY = 0;
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();

if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
lastX = x;
lastY = y;
int deltaX = Math.abs(x-lastX);
int deltaY = Math.abs(y-lastY);

x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();

x += widget.getScrollX();
y += widget.getScrollY();

Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);

LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);

if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - lastClickTime < 1000)
link[0].onClick(widget);
else if (deltaX < 10 && deltaY < 10)
link[0].onLongClick(widget);
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
lastClickTime = System.currentTimeMillis();
}
return true;
}
}

return super.onTouchEvent(widget, buffer, event);
}


public static MovementMethod getInstance() {
if (sInstance == null)
sInstance = new LongClickLinkMovementMethod();

return sInstance;
}
private static LongClickLinkMovementMethod sInstance;
}

LongClickableSpanClass

import android.text.style.ClickableSpan;
import android.view.View;

public abstract class LongClickableSpan extends ClickableSpan {

abstract public void onLongClick(View view);
}

实际实现

LongClickableSpan eruptionText = new LongClickableSpan() {

@Override
public void onClick(View tvEruptions) {
LinkFunctions.link_eruption_detail(getApplicationContext(),PostErupionID);
}

@Override
public void onLongClick(View tvEruptions) {
if(SignedInUserID != 0) {
DialogFragment newFragment = new Dialogs.QuickActionsDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putLong("eruptionID", PostErupionID);
newFragment.setArguments(args);
newFragment.show(getFragmentManager(), "QuickActions");
}
}

};
ss.setSpan(eruptionText, startpos[(int) j], endpos[(int) j], Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

最佳答案

我知道这个问题很老,可能 OP 已经解决了这个问题,但我对原来的 LongClickLinkMovementMethod 做了一些调整,让它在时间过去后触发,而不是在我们释放水龙头时触发:

import android.os.Handler;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.MotionEvent;
import android.widget.TextView;


public class LongClickLinkMovementMethod extends LinkMovementMethod {

private Handler mLongClickHandler;
private static int LONG_CLICK_TIME = 1000;
private boolean mIsLongPressed = false;

@Override
public boolean onTouchEvent(final TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();

if(action == MotionEvent.ACTION_CANCEL){
if(mLongClickHandler!=null){
mLongClickHandler.removeCallbacksAndMessages(null);
}
}

if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();

x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();

x += widget.getScrollX();
y += widget.getScrollY();

Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);

final LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);

if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
if(mLongClickHandler!=null){
mLongClickHandler.removeCallbacksAndMessages(null);
}
if(!mIsLongPressed) {
link[0].onClick(widget);
}
mIsLongPressed = false;
} else {
Selection.setSelection(buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
mLongClickHandler.postDelayed(new Runnable() {
@Override
public void run() {
link[0].onLongClick(widget);
mIsLongPressed = true;
}
},LONG_CLICK_TIME);
}
return true;
}
}

return super.onTouchEvent(widget, buffer, event);
}


public static MovementMethod getInstance() {
if (sInstance == null) {
sInstance = new LongClickLinkMovementMethod();
sInstance.mLongClickHandler = new Handler();
}

return sInstance;
}
private static LongClickLinkMovementMethod sInstance;
}

所以基本上这使用处理程序在 1000 毫秒后触发长按事件

关于android - 长按 Clickable span 在点击被释放之前不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com