【问题标题】:TextView link not working in my fragment with emulatorTextView 链接在我的带有模拟器的片段中不起作用
【发布时间】:2018-10-05 22:55:38
【问题描述】:

我想在我的TextView 中添加一个网络链接。我在我的片段中使用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?

我的TextViewlinearLayout里面

 <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;
    }

【问题讨论】:

    标签: android textview fragment


    【解决方案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);
    

    【讨论】:

    • 我的网址可以放在哪里??
    • @ArpitPatel 你想在浏览器中打开网址吗?
    • 是的,但我的问题是我可以把我的网页网址放在哪里?像在字符串或java代码中
    • @ArpitPatel 我用你的网址修正了我的答案
    • 我尝试了你的解决方案,但我什至看不到我的 textView。
    【解决方案2】:

    您可以尝试以下代码,它工作正常,我已经测试了自己。

    不要使用 android:autoLink 标签属性。因为它会导致 LinkMovementMethod 不起作用。

        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());
    

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

    <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());
    

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

        FROM_HTML_MODE_COMPACT = 63;
        FROM_HTML_MODE_LEGACY = 0;
        FROM_HTML_OPTION_USE_CSS_COLORS = 256;
        FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
        FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
        FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
        FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
        FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
        FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
    

    祝你好运。 :)

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2020-12-15
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      相关资源
      最近更新 更多