gpt4 book ai didi

android - textIsSelectable 正在使 TextView 滚动

转载 作者:行者123 更新时间:2023-12-02 10:10:55 29 4
gpt4 key购买 nike

<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="5"
android:padding="16dip"
android:textIsSelectable="true"
android:textSize="16sp"/>

如果 android:textIsSelectable 不存在,则 TextView 的 maxLines 为 5,并且您无法让 TextView 滚动,即使有其他文本已超出 View 。

但是,添加 android:textIsSelectable 后,TextView 的最大行数仍为 5,但您可以通过点击 TextView 的底部边框或选择一些文本来使其滚动并拖动文本选择器控件。

android:textIsSelectable 存在时,有没有办法阻止 TextView 滚动?

最佳答案

好吧,你可能不明白 android:textIsSelectable 已经在做什么......

根据TextView文档(你可以找到它here):

To allow users to copy some or all of the TextView's value and paste it somewhere else, set the XML attribute android:textIsSelectable to true or call setTextIsSelectable(true).

The textIsSelectable flag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.

滚动似乎是 isSelectable 标志的主要功能之一,但是......

根据问题,这是来自:How do I completely prevent a TextView from scrolling?的帖子

So I did a little research and I don't think it's as simple as just disabling scrolling, but there are a few things you can do/try.

The first is setEnabled(false) but this will disable links and alter the text color.

The second, which I suggest trying, is using the scrollTo(int x, int y) method. Just scrollTo(0,0) after setting the text of the TextView, my guess is the large text is the only thing causing the scrolling so this should be able to take care of it.

The third answer I found that you can try is a bit more complicated and not exactly your question but it may work for you can be found here.

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

}

"After that apply it to the target textview as touch listener: -

textview.setOnTouchListener(new LinkMovementMethodOverride());"

您也可以尝试将此行放入您的 TextView 属性中:

android:isScrollContainer="false"
android:ellipsize="end"

希望对你有帮助

关于android - textIsSelectable 正在使 TextView 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34423509/

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