Android自定义URLSpan不起作用

编程入门 行业动态 更新时间:2024-10-10 05:26:21
本文介绍了Android自定义URLSpan不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问题是要处理我自己的单击URL范围的操作.我写了自定义URLSpan,但是不起作用.

The problem is to handle my own action on click on URL span. I wrote custom URLSpan but it doesn't work.

这是我的自定义URLSpan:

This is my custom URLSpan:

public class CustomURLSpan extends android.text.style.URLSpan { private Command mClickAction; public CustomURLSpan(String url, Command clickAction) { super(url); mClickAction = clickAction; } @Override public void onClick(View widget) { try { mClickAction.execute(); } catch (Exception e) { } } public static void clickifyTextView(TextView tv, Command clickAction) { SpannableString current = new SpannableString(tv.getText()); URLSpan[] spans = current.getSpans(0, current.length(), URLSpan.class); for (URLSpan span : spans) { int start = current.getSpanStart(span); int end = current.getSpanEnd(span); current.removeSpan(span); current.setSpan(new CustomURLSpan(span.getURL(), clickAction), start, end, 0); } } public interface Command { void execute(); } }

在这里我用它:

@Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle bundle = getArguments(); String message = bundle.getString("message"); final Activity activity = getActivity(); text = new TextView(activity); text.setText(message); Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES); CustomURLSpan.clickifyTextView(text, new CustomURLSpan.Command() { @Override public void execute() { //I want to do my stuff here, but not working } }); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); alertDialogBuilder.setView(text); alertDialogBuilder.setNegativeButton("Close", new DialogInterface.OnClickListener() { ... }

但是,如果我单击url,我将获得本地android对话框来选择电子邮件程序.我在互联网上找到的所有示例都是相同的.

But if I click on url, I get the native android dialog to choose email programm. All examples I find in internet are same.

编辑:根据@CommonWare的回答.我只需要:

According to answer from @CommonWare. I just needed:

... public static void clickifyTextView(TextView tv, Command clickAction) { SpannableString current = new SpannableString(tv.getText()); URLSpan[] spans = current.getSpans(0, current.length(), URLSpan.class); for (URLSpan span : spans) { int start = current.getSpanStart(span); int end = current.getSpanEnd(span); current.removeSpan(span); current.setSpan(new CustomURLSpan(span.getURL(), clickAction), start, end, 0); tv.setText(current); //this is what I need } } public interface Command { void execute(); }

推荐答案

clickifyTextView()从TextView中检索文本,将其包装在新的SpannableString中,然后从不更新TextView.因此clickifyTextView()正在修改TextView中内容的副本,因此不会影响TextView.

clickifyTextView() retrieves the text from the TextView, wraps it in a new SpannableString... then never updates the TextView. So clickifyTextView() is modifying a copy of what is in the TextView, which therefore does not affect the TextView.

尝试在clickifyTextView()中的跨度转换循环之后在TextView上调用setText().

Try calling setText() on the TextView after your span conversion loop in clickifyTextView().

更多推荐

Android自定义URLSpan不起作用

本文发布于:2023-11-02 06:04:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1551572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   不起作用   Android   URLSpan

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!