textIsSelectable属性导致TextView滚动

5
<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的最大行数为5,即使有额外的文本溢出视图,你也无法使TextView滚动。
然而,添加android:textIsSelectable后,TextView仍然具有最大行数为5,但你可以通过点击TextView的底部边框或选择一些文本并拖动文本选择器控件来使其滚动。
当存在android:textIsSelectable时,有没有一种方法可以防止TextView可滚动?
2个回答

1
你可以创建自定义的不可滚动 TextView。
public class NonScrollableTextView extends TextView {
    public NonScrollableTextView(Context context) {
        super(context);
    }

    public NonScrollableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NonScrollableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void scrollTo(int x, int y) {
        //nop
    }
}

1

嗯,你可能不理解android:textIsSelectable已经实现了什么...

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

为了允许用户复制TextView的某些或全部值并将其粘贴到其他地方,请将XML属性android:textIsSelectable设置为true或调用setTextIsSelectable(true).

textIsSelectable标志允许用户在TextView中进行选择手势,这反过来触发系统内置的复制/粘贴控件。

看起来滚动是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"

希望它有帮助。

很遗憾,以上的想法都没有奏效,但还是感谢你的尝试。 - Andrew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接