启用辅助功能后,使超链接可单击

时间:2019-04-22 10:43:50

标签: android android-accessibility

我正在通过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,但它似乎不起作用。

2 个答案:

答案 0 :(得分:0)

Google Support page中所述,用户将需要通过Local Context Menu手势(默认手势为TalkBack)访问Swipe up then right来激活{{1} }链接。

实际上,TextView小部件上任何可单击的span都可以使用其上下文菜单通过TextView进行激活,甚至是无法打开浏览器的链接。您实施的TalkBack可能会显示一条performAction消息,并且用户将能够通过上下文菜单激活。

我用ToastTalkBack 7.3.0Android 8.1中的Linkify进行了测试。两者都按预期工作。

android:autoLink="web"

TalkBack context menu listing TextView links to activate

答案 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>

Reference and also Java sample