gpt4 book ai didi

android - TextView 中显示的最后一个词

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:00:42 29 4
gpt4 key购买 nike

我将在 Android 应用程序的多个页面上显示完整的文本文档。每个页面都包含一个TextView控件,负责显示文本。它还包含一个下一个按钮,该按钮负责更新 TextView 并从上次显示的部分结束处开始跟踪文本。
问题是如何在TextView的可显示区域找到最后显示的单词?
注意:在此应用中,textView 应该滚动或使用椭圆大小。还必须注意文本大小可以增加或减少。为了更好地了解情况:要显示的示例文本:

This is whole text. At first Page It just ended here and must be followed from here.

这是其显示的示例图。问题是如何找到第 1 页显示的最后一个单词“here”? And this is the sample illustration of the its display. The question is how to find word "here" as last word displayed in page 1?

最佳答案

我现在不能尝试这个想法,但让我们试一试吧!根据this post您可以使用以下方法来确定字符串是否适合 TextView 。

private boolean isTooLarge (TextView text, String newText) {
float textWidth = text.getPaint().measureText(newText);
return (textWidth >= text.getMeasuredWidth ());
}

所以一个想法,如果文本总是相同的,您可以手动为每个 TextView 定义初始值,然后当您增加或减少字体时重新计算它,删除或添加单词。

如果无法手动输入初始值,您可以执行以下操作:

String wordsInTextview = new String();
int lastUsedWord= 0;
int totalNumberOfWords = text.size();
for (int i=lastUsedWord;i<totalNumberOfWords;i++) {
if !(isTooLarge(TextView,wordsInTextview)) {
wordsInTextview = wordsInTextview + text(i); // add the words to be shown
} else {
TextView.setText(wordsInTextView);
lastUsedWord= i;
wordsInTextView = new String(); // no idea if this will erase the textView but shouldn't be hard to fix
}
}

您还需要存储 textView 的第一个单词的位置,因此当您调整文本大小时您知道从哪里开始!

int firstWordOnTextView = TextView.getText().toString().split(" ")[0]

当它调整大小时,您可以使用相同的方法计算屏幕上的文本。

lastUsedWord = firstWordOnTextView;

如果你想更快,你可以跟踪你在每一页上有多少单词,取一个平均值,在几次运行后总是从那里统计你的循环。或者在避免重复返回之前说几句话。

如果您不必一次显示太多页面,我相信这是一个合理的解决方案!

很抱歉代码中的错误我现在没有地方可以尝试!对此解决方案有何评论?很有趣的问题!

关于android - TextView 中显示的最后一个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10499535/

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