点击文本视图点击中未在浏览器中打开的锚标记

时间:2017-10-16 13:19:18

标签: android textview

使用此字符串的字符串:

String stringConvertor="We are looking for talented individuals who share our values and <a href='http://www.precipeace.com/about/' target='_blank'>mission</a>.  What is your motivation for joining our company?","quesNo":1,"__v":0,"isDelete":false,"createdAt":"2017-08-16T11:51:05.644Z"},{"_id":"599431be8eedf235d9965a08","question":"Currently, we utilize coaching techniques like CBT, positive psychology, and other solution focused techniques to help our clients. <br class='breakclass'/><br class='breakclass'/> Are you familiar and capable of utilizing these techniques or open to learning about them?  What other techniques do you have experience with that you feel would benefit your future clients?"

代码:

<TextView
      android:id="@+id/txt_screening_question"
      android:layout_width="match_parent"
      android:gravity="center"
      android:layout_height="wrap_content"
      android:paddingBottom="@dimen/text_size_medium"
      android:textSize="@dimen/text_size_medium"
      android:autoLink="web"
      android:linksClickable="true"
      android:text="We are looking for talented individuals who share our values and mission. What is your motivation for joining our company?"
      android:textColor="@color/colorGrey"/>

Java代码:

Linkify.addLinks(txtScreeningQuestion, Linkify.WEB_URLS);
txtScreeningQuestion.setText(Html.fromHtml(stringConvertor));

为我提供解决方案如何点击它 请给我任何解决方案如何执行点击锚标记字符串

1 个答案:

答案 0 :(得分:1)

尝试按照以下格式将字符串保存在strings.xml中。

<string name="welcome">
<![CDATA[ Welcome to <a href=\'http://www.google.com\'>Google</a>.]]>
</string>

然后将字符串分配给TextView,如下所示:

 Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(getString(R.string.welcome)));
            PackageManager manager = getPackageManager();
            List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);

            if (infos.size() > 0) {
                //At least one application can handle your intent
                //Put this code in onCreate and only Linkify the TextView from here
                //instead of using android:autoLink="web" in xml
                Linkify.addLinks(txtScreeningQuestion, Linkify.ALL);
                // or tvTextView.setAutoLinkMask(Linkify.WEB_URL), 
            }
    txtScreeningQuestion.setText(Html.fromHtml(getString(R.string.welcome));