Android textView 中的链接不可点击

问题描述 投票:0回答:1

我想将链接嵌入到文本视图中。但是,链接没有下划线且不可点击。

我有以下字符串资源

    <string name="info_efficiency_link">
    <![CDATA[
    &#8226; Heat pumps are by far the most efficient heating technology.<br/><br/>
    &#8226; They convert 1 kWh of electricity into 3-4 kWh of heat.<br/><br/>
    &#8226; Ground-source heat pumps are more efficient than air-source heat pumps but also more expensive.<br/><br/>
    &#8226; By the way: Fridges and freezers use a process similar to a heat pump, moving heat from inside the appliance to the outside.<br/><br/>
    &#8226; Sources: <a href="https://www.entega.de/blog/wirkungsgrad-waermepumpe/">Prof. Dipl. –Ing. W. Schenk, Hochschule München, 2020</a> (accessed on 19.10.2024).<br/><br/>
    <a href="https://www.techem.de/blog/wirkungsgrad-waermepumpe/">Techem Energy Services GmbH, 2022</a> (accessed on 19.10.2024).<br/><br/>
    ]]>
</string>

这是xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FR_Options">

    <!-- HorizontalScrollView for scrolling horizontally -->
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"> <!-- Ensures it uses full width even if content is small -->

        <!-- LinearLayout inside to arrange items horizontally -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"> <!-- Set orientation to horizontal for scrolling -->

            <!-- First item: Caption, ImageView and TextView stacked vertically -->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="10dp">

                <!-- Caption TextView -->
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/cap_efficiency"
                    android:textSize="13sp"
                    android:textStyle="bold" />

                <ImageView
                    android:layout_width="270dp"
                    android:layout_height="180dp"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/facts_efficiency_english" />

                <TextView
                    android:id="@+id/tv_facts_efficiency"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="9dp"
                    android:autoLink="web"
                    android:text="@string/info_efficiency_link"
                    android:linksClickable="true"
                    android:textColor="@android:color/black" />
            </LinearLayout>


        </LinearLayout>
    </HorizontalScrollView>

</FrameLayout>

相关的textView是

android:id="@+id/tv_facts_efficiency"
。这是片段的java文件

package com.example.game;

import androidx.fragment.app.Fragment;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.core.text.HtmlCompat;

import com.example.game.databinding.FragmentInterestingFactsBinding;

public class FR_InterestingFacts extends Fragment {

    private FragmentInterestingFactsBinding binding;

    public FR_InterestingFacts() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the screen orientation to landscape
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentInterestingFactsBinding.inflate(inflater, container, false);


        String formattedText = getString(R.string.info_efficiency_link);
        binding.tvFactsEfficiency.setText(HtmlCompat.fromHtml(formattedText, HtmlCompat.FROM_HTML_MODE_LEGACY));


        binding.tvFactsEfficiency.setMovementMethod(LinkMovementMethod.getInstance());

        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}

你知道如何解决这个问题吗?这些链接不会显示,单击它们时也没有任何反应。我用两个不同的模拟器尝试过。

android hyperlink textview
1个回答
0
投票

要解决此问题,您必须在调用

android:autoLink="web"
激活单击事件时从 TextView 中删除属性
setMovementMethod()
。另外,也不需要属性
android:linksClickable="true"
,因为默认情况下 TextView 已将此值启用为 True。

所以 xml 应如下所示:

<TextView
    android:id="@+id/tv_facts_efficiency"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="9dp"
    android:text="@string/info_efficiency_link"
    android:textColor="@android:color/black" /> 

并且您已有的 Java 代码是正确的:

String formattedText = getString(R.string.info_efficiency_link);
tvFactsEfficiency.setText(HtmlCompat.fromHtml(formattedText, HtmlCompat.FROM_HTML_MODE_LEGACY));
tvFactsEfficiency.setMovementMethod(LinkMovementMethod.getInstance());
© www.soinside.com 2019 - 2024. All rights reserved.