簡體   English   中英

單擊TextView中的鏈接HTML

[英]Clicking Link HTML in TextView

我有一個文本視圖,顯示一些HTML並正確顯示。 如果HTML包含僅包含URL文本的鏈接,我可以點擊鏈接打開它,但如果URL歸因於其他文本,如下所示:

<a href="https://cdn.kyfb.com/KYFB/assets/File/Federation/Across%20Kentucky/2017/January/AK_Promo_Jan_23_2017_mixdown.mp3">Listen here</a></strong></p>

“在這里聽”在我的文本視圖中看起來像一個可點擊的鏈接,但無法選擇。

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import android.text.method.LinkMovementMethod;

public class BenefitDetailActivity extends BaseActivity {

    static Context context;
    TextView detailTextView;
    String descriptionText;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_benefit_details);
        context = getApplicationContext();

        Intent detailIntent = getIntent();
        descriptionText = detailIntent.getStringExtra("description");

        detailTextView = (TextView)findViewById(R.id.benefitText);

        Typeface kfb = Typeface.createFromAsset(getAssets(), "FranklinGothicStd-ExtraCond.otf");
        detailTextView.setTypeface(kfb);
        detailTextView.setTextSize(20);

        System.out.println("DESCRIPTION: " + descriptionText);
        detailTextView.setText(Html.fromHtml(descriptionText));
    }
}

布局:

<TextView
        android:id="@+id/benefitText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        android:background="@color/transparent"
        android:textColor="@color/white_text"
        android:autoLink="all" />

根據其他人的建議,我補充道

detailTextView.setMovementMethod (LinkMovementMethod.getInstance());

android:linksClickable="true"

有了這個,問題是當我添加setMovementMethod()時,鏈接的文本(在這里監聽)不再顯示為鏈接,仍然無法選擇。

將以下屬性添加到布局文件中的textview

<TextView
    android:id="@+id/benefitText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:background="@color/transparent"
    android:textColor="@color/white_text"
    android:autoLink="all"
    android:linksClickable="true"
    />

並在setMovementMethod上添加以下行。

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

我通過添加detailTextView.setMovementMethod(LinkMovementMethod.getInstance());修復了這個問題detailTextView.setMovementMethod(LinkMovementMethod.getInstance()); android:linksClickable="true" 我還必須刪除android:autoLink="all"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM