I have a TextView which is rendering basic HTML, containing 2+ links. I need to capture clicks on the links and open the links -- in my own internal WebView (not in the default browser.)
The most common method to handle link rendering seems to be like this:
String str_links = "<a href='http://google.com'>Google</a><br /><a href='http://facebook.com'>Facebook</a>";
text_view.setLinksClickable(true);
text_view.setMovementMethod(LinkMovementMethod.getInstance());
text_view.setText( Html.fromHtml( str_links ) );
However, this causes the links to open in the default internal web browser (showing the "Complete Action Using..." dialog).
I tried implementing a onClickListener, which properly gets triggered when the link is clicked, but I don't know how to determine WHICH link was clicked...
text_view.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// what now...?
}
});
Alternatively, I tried creating a custom LinkMovementMethod class and implementing onTouchEvent...
public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
String url = text.toString();
// this doesn't work because the text is not necessarily a URL, or even a single link...
// eg, I don't know how to extract the clicked link from the greater paragraph of text
return false;
}
Ideas?
Example solution
I came up with a solution which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…