TextView链接在模拟器中无法正常工作

时间:2018-10-05 22:55:39

标签: android textview fragment

我想在我的TextView中放置一个Web链接。我在片段中使用了TextView。我认为它在模拟器上不起作用。我更换了模拟器,仍然遇到相同的问题。

这是我尝试过的解决方案:

  1. How to open URL from Fragment TextView?
  2. Android - Hyperlink is not clickable
  3. Why link does not work in the text view?

我在TextView内的linearLayout

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/privacy_policy"
        android:textColor="@color/colorPrimary"
        android:padding="10dp"
        android:textSize="17sp"
        android:textStyle="bold"
        android:autoLink="all"
        android:clickable="true"
        android:focusable="true"
        android:linksClickable="true"
        android:id="@+id/tv_privacy_policy" />

我的字符串值

 <string name="privacy_policy">Privacy Policy<a href="https://csunix.mohawkcollege.ca/~000762465/Privacy%20Policy/ielts_up.html"></a></string>

我的onCreateView()

      @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_aboutus, container, false);

        TextView tv_privacy_policy = (TextView)rootView.findViewById(R.id.tv_privacy_policy);

//        Linkify.addLinks(tv_privacy_policy, Linkify.WEB_URLS);
        // Inflate the layout for this fragment

        tv_privacy_policy.setMovementMethod(LinkMovementMethod.getInstance());
        String text = "<a href='http://www.google.com'>Pricacy Policy</a>";
        tv_privacy_policy.setText(Html.fromHtml(text));

        return rootView;
    }

2 个答案:

答案 0 :(得分:1)

也许对您有帮助:

    SpannableString string = new SpannableString("Privacy Policy");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://csunix.mohawkcollege.ca/~000762465/Privacy%20Policy/ielts_up.html"));
            startActivity(browserIntent);
        }
    };
    string.setSpan(clickableSpan, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView textView = (TextView) findViewById(R.id.text);
    textView.setText(string);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(Color.TRANSPARENT);

答案 1 :(得分:0)

您可以尝试以下代码,并且工作正常,我已经测试过。

请勿与其一起使用android:autoLink标记属性。因为它会导致LinkMovementMethod不起作用。

mapreduce.map.failures.maxpercent
mapreduce.map.failures.maxpercent

如果您要在XML上传递字符串,则可以执行以下操作

    String textToShow="<a href=\"https://csunix.mohawkcollege.ca/~000762465/Privacy%20Policy/ielts_up.html\">Privacy Policy</a>";

        Spanned result;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            result = Html.fromHtml(textToShow,Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(textToShow);
        }
        textView.setText(result);
        textView. setMovementMethod(LinkMovementMethod.getInstance());

以下是可能帮助您的标志列表:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/privacy_policy"
            android:textColor="@color/colorPrimary"
            android:padding="10dp"
            android:textSize="17sp"
            android:textStyle="bold"
            android:id="@+id/tv_privacy_policy" />

    TextView tv_privacy_policy= (TextView) findViewById(R.id.tv_privacy_policy);
    tv_privacy_policy.setMovementMethod(LinkMovementMethod.getInstance());

祝你好运。 :)