gpt4 book ai didi

android - 我可以在使用 LinkMovementMethod 时禁用 TextView 中的滚动吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:37 31 4
gpt4 key购买 nike

我在 textView 中使用 clickable span 来使只有部分文本可以点击。它工作正常,除了 textView 正在向下滚动,这是我不想要的。发生这种情况是因为我使用了 LinkMovementMethod,它可以根据需要滚动。无论如何取消滚动?

SpannableString ss = "My text [click area] end."

ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
// My click action
}
};

// Set the span
String fromString = "text";
int startClickPos = ss.toString().indexOf(fromString)+fromString.length()+1;
int endCickPos=startClickPos+ 12;
ss.setSpan(clickableSpan, startClickPos, endCickPos, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());

最佳答案

我正在使用此代码禁用带有 clickableSpan 的 TextView 的滚动。

public class LinkMovementMethodOverride implements View.OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
TextView widget = (TextView) v;
Object text = widget.getText();
if (text instanceof Spanned) {
Spanned buffer = (Spanned) text;

int action = event.getAction();

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);

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

if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
} else if (action == MotionEvent.ACTION_DOWN) {
// Selection only works on Spannable text. In our case setSelection doesn't work on spanned text
//Selection.setSelection(buffer, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]));
}
return true;
}
}

}

return false;
}

}

之后将它作为触摸监听器应用到目标 TextView :-

textview.setOnTouchListener(new LinkMovementMethodOverride());

关于android - 我可以在使用 LinkMovementMethod 时禁用 TextView 中的滚动吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14579785/

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