[英]How to handle link click events in a textView
I am using Jsoup to parse the a website, formatting it with Html.fromHtml()
and displaying the formatted text in a textview.我正在使用 Jsoup 解析网站,使用Html.fromHtml()
对其进行格式化并在文本视图中显示格式化的文本。
Also, I'm using LinkMoveMentmethod.getInstance
to make the links in the textview clickable.此外,我正在使用LinkMoveMentmethod.getInstance
使文本视图中的链接可点击。
When the links are clicked they fired up a chooser to choose browsers.单击链接时,它们会启动一个选择器来选择浏览器。
Please, how can I override this default behaviour.拜托,我怎样才能覆盖这个默认行为。
For example, I would want to pass the clicked url to my own activity and use Jsoup to parse it also.例如,我想将点击的 url 传递给我自己的活动,并使用 Jsoup 来解析它。
TextView pageContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_details);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
pageContent = (TextView) findViewById(R.id.dpage_content);
}
....
private void parseHtml(String response) {
Log.d(TAG, "parsinghtml");
Document document = Jsoup.parse(response);
page_content = document.select("div.page-content").first().html();
Spanned spanned = Html.fromHtml(page_content, new UILImageGetter(pageContent, this), null );
}
You need to create your custom class which extends LinkMovementMethod.您需要创建扩展 LinkMovementMethod 的自定义类。
public class LinkClickHandler extends LinkMovementMethod{
private static LinkClickHandler sInstance;
public static LinkClickHandler getInstance() {
if (sInstance == null)
sInstance = new LinkClickHandler();
return sInstance;
}
@Override
public boolean onTouchEvent(TextView widget,
Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
//Implement your code for handling the click.
}
return super.onTouchEvent(widget, buffer, event);
}
}
To use this, change LinkMovementMethod.getInstance
to LinkClickHandler.getInstance
要使用它, LinkMovementMethod.getInstance
更改为LinkClickHandler.getInstance
You can use clickable span您可以使用可点击的跨度
ClickableSpan cs = new ClickableSpan() {
@Override
public void onClick(View v) {
Log.d("main", "textview clicked");
Toast.makeText(Main.this, "textview clicked",Toast.LENGTH_SHORT).show();
} };
// set the "test " spannable.
span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span);
tv.setMovementMethod(LinkMovementMethod.getInstance());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.