TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
CharSequence text = tv.getText();
if (text instanceof Spannable) {
int end = text.length();
Spannable sp = (Spannable) text;
URLSpan urls[] = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans();
for (URLSpan urlSpan : urls) {
MyURLSpan myURLSpan = new MyURLSpan(urlSpan.getURL());
style.setSpan(myURLSpan, sp.getSpanStart(urlSpan),
sp.getSpanEnd(urlSpan),
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
tv.setText(style);
}
}
private class MyURLSpan extends ClickableSpan {
private String url;
public MyURLSpan(String url) {
this.url = url;
}
@Override
public void onClick(View arg0) {
startActivity(new Intent(WXEntryActivity.this,RegisterActivity.class));
}
}
}
3. The above two steps perfectly solve the problem of website links being highlighted and underlined. When clicked, you can jump to the determined Activity
Finally, I would like to thank you all again for your answers, and I would also like to thank netizen @小谷xg for providing a blog reference in this regard. I have learned a lot.
Does it need to be such a roundabout way? Directly set clickable="true" on textView, then set textview.setOnclickListener click event, jump, textview.getText gets the URL
Don’t use the autolink attribute. Directly set the click event of the textview, including a qualified String, unless there is a corresponding event: such as calling an activity
Thank you all the enthusiastic netizens for your answers. I have found the relevant answers. The details are as follows:
Note android:autoLink="web"
2. Rewrite the click event of url
public class MainActivity extends Activity {
3. The above two steps perfectly solve the problem of website links being highlighted and underlined. When clicked, you can jump to the determined Activity
Finally, I would like to thank you all again for your answers, and I would also like to thank netizen @小谷xg for providing a blog reference in this regard. I have learned a lot.
Does it need to be such a roundabout way? Directly set clickable="true" on textView, then set textview.setOnclickListener click event, jump, textview.getText gets the URL
Inherited
ClickableSpan
重写onClick
MethodI have written this before, see if it helps
Don’t use the autolink attribute. Directly set the click event of the textview, including a qualified String, unless there is a corresponding event: such as calling an activity
Description of requirements:
1. The poster needs to underline the website link
2. The poster hopes to jump to the determined Activity when clicked
Has the group owner found a solution? I also encountered this problem