我正在通过Talk / Voice Over使用可访问性来使应用程序可访问。
如何使链接可点击且具有辅助功能?
使用以下方法在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,但它似乎不起作用。
答案 0 :(得分:0)
如Google Support page中所述,用户将需要通过Local Context Menu
手势(默认手势为TalkBack
)访问Swipe up then right
来激活{{1} }链接。
实际上,TextView
小部件上任何可单击的span
都可以使用其上下文菜单通过TextView
进行激活,甚至是无法打开浏览器的链接。您实施的TalkBack
可能会显示一条performAction
消息,并且用户将能够通过上下文菜单激活。
我用Toast
和TalkBack 7.3.0
在Android 8.1
中的Linkify
进行了测试。两者都按预期工作。
android:autoLink="web"
答案 1 :(得分:0)
val clickSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
// Handel your click
Util.launchUrl(activity, getString(R.string.permissions_request_agreement_url))
}
}
val agreementLabel = getString(R.string.permissions_request_agreement_label)
val textHeadingSpannable: SpannableString = SpannableString(agreementLabel)
//From "I agree to the Optus Privacy Policy", make "Optus Privacy Policy" clickable
textHeadingSpannable.setSpan(clickSpan, 15, agreementLabel.length, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
privacyLink.text = textHeadingSpannable
privacyLink.movementMethod = LinkMovementMethod.getInstance()
listener = ValidatedListener {
btnPrimary.isEnabled = isValidated()
}
另一方面:
<string name="permissions_request_agreement_label">I agree to the Optus Privacy Policy</string>
<string name="permissions_request_agreement_url">https://www.optus.com.au/about/legal/privacy/telco-services</string>