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

快乐编码.. !! :)