0

我有一個文字,比如「SUV是最好的」。在這篇文章中,只有SUV必須突出顯示,點擊SUV時,必須將其引導至某個網站。我怎樣才能做到這一點?如何點擊文字打開網頁

問候

回答

0

你能在文字之間添加標籤?如果是的話根本就喜歡下面:

<a href="your url">SUV</a> are the best 

如果你想linkify文本,而不在文字之間添加標籤,你可以使用正則表達式來做到這一點。

請按照this url爲:

1

需要使用Spannablestring和點擊跨度選項以獲得你需要什麼。

請嘗試以下解決方案。

解決方案1:

XML

<TextView 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" 
    android:textSize="24sp" 
    android:textColor="#234356" 
    android:padding="5dp" 
    android:id = "@+id/testview"/> 

JAVA

TextView test = (TextView) findViewById(R.id.testview); 

SpannableString ss = new SpannableString("SUV is the Best. Offers Avail"); 
ClickableSpan clickableSpan = new ClickableSpan() { 
    @Override 
    public void onClick(View textView) { 
    // open your browser here with the link 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setType("*/*"); 
    i.setData(Uri.parse("http://www.google.com")); // your link goes here don't forget to add http:// 
    startActivity(new Intent(Intent.createChooser(i, "Open website using"))); 
    } 

    @Override 
    public void updateDrawState(TextPaint ds) { 
    super.updateDrawState(ds); 
    ds.setUnderlineText(false); 
    } 
}; 
// 0 and 3 are the characters start and end where we need to open link on click here SUV 
ss.setSpan(clickableSpan, 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
test.setText(ss); 
test.setMovementMethod(LinkMovementMethod.getInstance()); 
test.setHighlightColor(Color.GREEN); 

解決方案2:

TextView test = (TextView) findViewById(R.id.testview); 
test.setText(Html.fromHtml("<a href=\"http://www.google.com\">SUV</a> <b> is the Best. Offers Avail</b> ")); 
test.setMovementMethod(LinkMovementMethod.getInstance()); 

快樂編碼.. !! :)