【问题标题】:android custom URLSpan not workingandroid自定义URLSpan不起作用
【发布时间】:2020-08-07 00:04:29
【问题描述】:

问题是处理我自己在点击 URL 范围时的操作。我编写了自定义 URLSpan,但它不起作用。

这是我的自定义 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 对话框来选择电子邮件程序。我在网上找到的所有例子都是一样的。

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

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

【问题讨论】:

    标签: android urlspan


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。我试过你的解决方案。在这种情况下,我得到了无法点击的文本,根本没有任何链接。
    • @AlexanderTumanin:确保您拨打的是setText(current),而不是setText(current.toString())。如果不是这样,请检查您从getText() 返回的对象类型。如果实现了Spannable,则无需新建SpannedString,直接编辑TextView的内容即可。
    • 谢谢!有用!我只是设置文本错误。我在我的对话框中做到了,但我必须在 CustomURLSpan 中做到。你节省了我的大量时间!
    【解决方案2】:

    在使用任何ClickableSpan 时将movementMethod = LinkMovementMethod() 设置为您的文本视图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2016-05-17
      相关资源
      最近更新 更多