gpt4 book ai didi

android - 启用可访问性时使超链接可点击

转载 作者:行者123 更新时间:2023-12-04 13:42:00 24 4
gpt4 key购买 nike

我正在使用 Talk/Voice Over 的可访问性使应用程序可访问。

How to make links clickable with accessibility?



使用以下方法制作 Hyperlink可点击 TextView .
/**
* This method creates the link and associates the ClickableSpan to it.
* Once the ClickableSpan is clicked, it will call the
* ClickableSpanListener.performAction
*/
public static void makeLinkClickable(
final ClickableSpanListener clickableSpanListener,
final SpannableStringBuilder strBuilder,
final URLSpan span) {
final int start = strBuilder.getSpanStart(span);
final int end = strBuilder.getSpanEnd(span);
final int flags = strBuilder.getSpanFlags(span);

//get the String that is used as the link
final char[] characters = new char[end - start];
strBuilder.getChars(start, end, characters, 0);

final ClickableSpan clickable = new ClickableSpan() {
public void onClick(final View view) {
clickableSpanListener.performAction(span.getURL(), new String(characters));
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}

/**
* This method takes in a String that contains at least one link defined by HTML <a></a> tags.
* A link will be created in the String and added to the TextView.
* The link will become clickable and the action (onClick) is defined by the
* ClickableSpanListener.
*/
public static void setTextViewHTML(
final ClickableSpanListener clickableSpanListener,
final TextView text,
final String html) {
final CharSequence sequence = Html.fromHtml(html);
final SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
final URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for (final URLSpan span : urls) {
makeLinkClickable(clickableSpanListener, strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}

/**
* This is the interface that should be implemented by classes to define the action
* that will occur when a clickablespan is clicked
*/
public interface ClickableSpanListener {
void performAction(String url, String linkText);
}


setTextViewHTML(
this,
txtTermsPrivacy,
getString(R.string.terms_condition_privacy_policy)
);

试过:

正如 google 对可访问性的支持中提到的那样 Google Support - Accessibility , 我试过使用 Linkify但它似乎不起作用。

最佳答案

Google Support page 中所述,用户需要访问 Local Context Menu通过 TalkBack手势(默认手势是 Swipe up then right )激活 TextView关联。

事实上,任何可点击的spanTextView小部件可以通过 TalkBack 激活使用它的上下文菜单,甚至是不打开浏览器的链接。您对 performAction 的实现可能会显示 Toast消息,用户将能够通过上下文菜单激活。

我用 TalkBack 7.3.0 对其进行了测试在 Android 8.1Linkifyandroid:autoLink="web" .两者都按预期工作。

<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="This is a TextView with link to StackOverflow: http://www.stackoverflow.com, and to Android Documentation: https://developer.android.com." />

TalkBack context menu listing TextView links to activate

关于android - 启用可访问性时使超链接可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55793098/

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