【问题标题】:Transferring text on overflow from one TextView to another TextView将溢出时的文本从一个 TextView 传输到另一个 TextView
【发布时间】:2015-06-14 17:49:13
【问题描述】:

我正在尝试构建类似文本阅读器的东西。它必须有一个水平幻灯片(普通的书本式),而不是向下滚动。

为了实现这一点,我正在用许多 TextViews 动态填充 ViewFlipper。直观的想法是,我在每个先前的 TextView 溢出上创建一个新的 TextView,并将其添加到 ViewFlipper

我可以使用 setMaxLines() 来完成此操作,但我无法获得超出该最大线的文本。

问题:

如何在溢出时将文本从一个 TextView 移动到另一个?

我决定使用的方法是否存在根本错误?

编辑:

抱歉,由于某种原因,我无法明确我的问题。也许这会有所帮助:

TextView text1, text2;
String s = "Extreamly long string. Much longer than this one";
text1.setText(s);
text2.setText( // Text that is overflowing //);

如何判断字符串的哪一部分是可见的,哪一部分溢出了?

谢谢

【问题讨论】:

  • filling a ViewFlipper with many TextViews 好吧,你实际上只需要 2,就像一本书的左右页(或日记或笔记本,等等)。只需适当地填写它们。
  • 这实际上是一个有用的注释。谢谢。我还可以请您更具体地了解我如何将溢出的文本从一个移动到另一个吗?
  • 您可以从源字符串中删除在第一个 TextView 中可见的文本,并将其余部分放在第二个 TextView 中。然后,继续前进,用第二个 TextView 的内容替换第一个 TextView 的内容,并在第二个中通过将第一个 TextView 中的文本从剪切字符串中剪切掉剩下的内容(在步骤 1 中剪切后)
  • 概念对我来说似乎也是正确的。但是如何剪切这个可见的文本呢?
  • ;) 谢谢。现在看起来不错。

标签: android textview


【解决方案1】:
    TextView tv = new TextView(context);

    ViewTreeObserver vto = tv.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(

            new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {

                    Layout l = tv.getLayout();

                    int height = tv.getHeight();
                    int scrollY = tv.getScrollY();

                    /**
                     * Key part
                     */

                    int lineCount = l.getLineForVertical(height + scrollY);

                    int start = l.getLineStart(0);
                    int end = l.getLineEnd(lineCount - 1);

                    /**
                     * Cut string
                     */

                    String s = tv.getText().toString().substring(start, end);

                }

            }
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多