如何修复 Html.fromHtml 链接焦点可见性问题(在 ICS 和 Honeycomb 中)?

发布于 2024-12-11 12:04:50 字数 662 浏览 0 评论 0原文

为了让 TextView 显示(并友好地对待)Html 字符串,我的代码如下所示:

// itemHtml is a String of HTML defined above

TextView itemContent = (TextView) findViewById(R.id.itemContent);
itemContent.setText(Html.fromHtml(itemHtml));
itemContent.setMovementMethod(LinkMovementMethod.getInstance());

如果 Html 字符串有一个链接,TextView 会生成可点击的链接,并且可聚焦的。当用户聚焦于特定链接时(例如,通过使用方向键),链接文本会以某种显着的方式发生变化,以表明已获得焦点。

问题是,当我使用带有方向键的设备(使用 Honeycomb(例如 Google TV)或冰淇淋三明治风格的 Android)测试相同的模式时,文本中的链接没有显示出该链接具有焦点的明显指示。

我知道它正在获得焦点,因为当您按下 Enter 键时,它会执行指定的操作。您甚至可以在文本中的各个链接之间移动;您只能猜测当前所在的链接,这会导致非常糟糕的用户体验。

我做错了什么吗?有什么方法可以解决这个问题或解决这个问题吗?

To get a TextView to display (and act friendly with) Html strings my code looks something like:

// itemHtml is a String of HTML defined above

TextView itemContent = (TextView) findViewById(R.id.itemContent);
itemContent.setText(Html.fromHtml(itemHtml));
itemContent.setMovementMethod(LinkMovementMethod.getInstance());

If the Html string has a link, TextView results in links that are clickable and focusable. When the user focuses on a specific link (e.g. by using the d-pad), the link text changes in some significant way to show that focus was obtained.

The problem is that when I test this same pattern using devices with a d-pad using Honeycomb (e.g. a Google TV) or Ice Cream Sandwich flavors of Android, the link in the text shows no noticeable indication that the link has focus.

I know it is getting focus, because when you then hit enter it does the specified action. You can even move around between various links in the text; you're just left guessing which link you're currently at, which results in a very bad user experience.

Is there something I'm doing wrong? Is there some way to fix this or work around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小巷里的女流氓 2024-12-18 12:04:50

编辑:在有点疯狂之后,我终于认为找到了解决方案。但是,此解决方案仅适用于 Honeycomb。 ICS仍然没有解决!

从 API 11 开始,Android 在 TextViews 上有一个新设置,用于定义文本是否可选。

您可以在 TextView 上使用 setTextIsSelectable(true) 设置它,或者在 XML 布局中定义它 android:textIsSelectable="true"

最好在 XML 中定义它,因此保持应用程序向后兼容是微不足道的。只需确保您的目标版本 >= 11,否则您可能会收到错误消息。

Edit: After going a bit nuts, I finally thought I found a solution. However, this solution only works for Honeycomb. ICS is still not solved!

As of API 11, Android has a new setting on TextViews for defining whether the text is selectable.

You can set it using setTextIsSelectable(true) on a TextView, or define it in the XML layout android:textIsSelectable="true"

It's probably best to define it in XML, so that keeping the app backwards-compatible is trivial. Just make sure you're targeting version >= 11, or you'll probably get an error.

昵称有卵用 2024-12-18 12:04:50

HTML.fromHTML 的操作方式是通过在字符串的各个字符中创建具有不同效果的“跨度”。解决此问题的一种方法是使用 ClickableSpan 与另一个将文本着色为可点击的CharacterStyles。前面的跨度将允许您注册一个回调,并且此回调可能是广播查看 url 的意图(这将打开浏览器)。

The way HTML.fromHTML operates is by creating "spans" with varying effects throughout the various characters of the string. One workaround for this would be to use ClickableSpan coupled with another of the CharacterStyles to colorize the text as clickable. The previous span will allow you to register a callback, and this callback could be to broadcast an intent to view a url (which would open a browser).

攒一口袋星星 2024-12-18 12:04:50

Honeycomb+ 的文本颜色状态列表可能不会将焦点状态设置为其他颜色,或者您将颜色覆盖为常量。

检查 your_android_sdk_directory/android-14/data/res/ 中的颜色+样式

将文本设置为 android:autoLink="web" 也可能有帮助?

The text colour state lists for Honeycomb+ might not set the focused state to a different colour, or you override the colour to be constant.

Check the colors + styles in your_android_sdk_directory/android-14/data/res/

Setting the text to android:autoLink="web" might also help?

终止放荡 2024-12-18 12:04:50

最好的方法是将 CSS 样式添加到您的 html 中。我知道 Android 支持 :hover 选择器。所以你可能会纠正这样的事情:

String myLink = "<a href=\"http:\/\/google.com\">your link</a>"
Html.fromHtml(myLink);

并找到一种方法来包含 CSS 数据:(我不知道如何,但我认为这是可能的)

a :hover {
   color: red;
}

更新:

我认为你的问题的答案是 那里

The best way to do that is to add CSS styling to your html. I know Android supports :hover selector. So you might right something like this:

String myLink = "<a href=\"http:\/\/google.com\">your link</a>"
Html.fromHtml(myLink);

and find a way to include CSS data to it: (I'm not sure how but I think it's possible)

a :hover {
   color: red;
}

UPDATE:

I think the answer of your question is there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文