在 Android 上突出显示 TextView 中的文本
这可能只是想法问题。 我在一个 ScrollLayout 中有许多 textView(所以我们可以说屏幕上某一时刻只显示其中的一半)。然后我有一个 editText,我正在考虑如何突出显示和编辑文本。滚动到 textView,其中包含与用户在 editText 中写入的字符串值相同的字符串值。 例如:
editText.text = "Test"
textView.text = "Testing Issue"
textView2.text = "Bla Bla"
我想用相应的文本值突出显示部分/完整(无关紧要)textView。
非常感谢,如果我的问题很蹩脚,抱歉:),因为我是 Android 新手。
This is propably just idea issue.
I have many textViews in one ScrollLayout (so let's say only half of them are showed at one moment on screen). Then I have an editText and I am thinking of a way how to highlight & scroll to the textView which contains same string value as user wrote in editText.
For instance :
editText.text = "Test"
textView.text = "Testing Issue"
textView2.text = "Bla Bla"
I want to highlight the part/full(doesnt matter) textView with the corresponding text value.
Many thanks in advance and sorry if my question is lame :) as I am new to Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,我建议使用 ListView,并为每个文本段设置一个单独的行。
您为 EditText 分配一个 TextWatcher,它有一个回调,在输入每个字母后调用。在这个回调中,您搜索输入的字符串(可能是异步的),并返回包含指定字符串的行的位置子串。
现在要将 ListView 滚动到第一次出现,您可以使用其 .smoothScrollToPosition() 方法。
至于突出显示,您可能会使用 TextView 小部件查看行中的文本。您可以使用 .setText() 设置在其中查看的文本,它也接受 Spannable/Spanned 对象。这些是类固醇字符串,您可以在字符串的不同区域分配样式跨度。因此,您可以使用它来显示您在不同颜色/背景/等中找到的子字符串。
或者使用 html。 Html.fromHtml() 实际上将提供的字符串中的 html 标签处理为 Spannable 对象,因此使用起来可能更容易:)
In this case I'd recommend using a ListView, and have a separate row for evey text segment you have.
You assign a TextWatcher for the EditText, it has a callback that gets called after every letter typed in. In this callback, you do you search for the entered string (possibly asynchronously), and get back the positions of the rows that contain the specified substring.
Now to scroll the ListView to the first occurence, you can use its .smoothScrollToPosition() method.
As for the highlights, you probably view the texts in the rows using TextView widgets. You can set the text viewed inside these with .setText(), which accepts Spannable/Spanned objects as well. These are strings on steroids, you can assign style spans on different regions of the strings. So you can use this to e.g. show the substring you've found in a different colour/background/etc.
Or use html. Html.fromHtml() actually processes the html tags in the provided strings into a Spannable objects, so it may be easier to work with :)
如果您正在谈论使某些文本在同一文本视图中看起来不同,那么您可以将特定文本设置为粗体或不同的内容,例如:
If you are talking about making some text looks different withing the same textview, then, you can make particular text bold or something different like:
创建一个样式作为资源,然后当用户输入文本并单击“提交”时,搜索具有匹配文本的所有文本框并将样式资源应用于它,以便它突出显示
create a style as a resource then when user enters the text and clicks submit search all the textbox with the matching text and apply the style resource to it so that it is highlited