简体   繁体   English

有没有一种标准方法可以在TextView中单击href和电话号码?

[英]Is there a standard way to have hrefs and telephone numbers clickable in a TextView?

I already know how to use TextView to automatically create clickable links for web addresses, phone numbers, etc.. My question is when you have HTML with hrefs in it as well as phone numbers and you want both clickable, is there a better or more standard way of doing it? 我已经知道如何使用TextView自动为网址,电话号码等创建可点击的链接。我的问题是,当您同时具有带有hrefs和电话号码的HTML时,您是否希望两者都可点击,是不是更好还是更多?标准的方式呢? Here is what I have: 这是我所拥有的:

String text = "Click <a href=\"http://stackoverflow.com\">Stackoverflow</a> or call 1-234-567-8901.";
TextView textView = getTextView();
textView.setLinksClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(text));

// This is a pretty primitive US-centric phone number REGEX but it works
Linkify.addLinks(textView, Pattern.compile("(1?[-. ])?\\d\\d\\d([-. ])\\d\\d\\d\\2\\d\\d\\d\\d"), "tel:");

This code works but doesn't seem ideal. 该代码有效,但似乎并不理想。 I do NOT call setAutoLinkMask(Linkify.PHONE_NUMBERS) because it will ignore the href's in the HTML and it will wipe out the Spannables that Html.fromHtml adds even with Linkify.WEB_URLS created. 我不会调用setAutoLinkMask(Linkify.PHONE_NUMBERS),因为它会忽略HTML中的href,即使创建了Linkify.WEB_URLS,它也会抹掉Html.fromHtml添加的Spannable。

At the very least I'd like to use a more robust or standard REGEX for the phone numbers. 至少我想对电话号码使用更可靠或更标准的REGEX。 I'm thinking something like Patterns.PHONE would at least be a better regex. 我在想类似Patterns.PHONE之类的东西,至少是更好的正则表达式。 However, I would hope that there is a more elegant solution that what I have above. 但是,我希望有一个比我上面更优雅的解决方案。

You may use cutom LinkMovementMethod implementation. 您可以使用定制的LinkMovementMethod实现。 Like this: 像这样:

public class CustomLinkMovementMethod extends LinkMovementMethod {

    private static CustomLinkMovementMethod linkMovementMethod = new BayerLinkMovementMethod();

    public boolean onTouchEvent(TextView widget, Spannable buffer,
            MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
            if (link.length != 0) {
                String url = link[0].getURL();
                if (url.startsWith("http://") || url.startsWith("https://")) {
                    //do anything you want 
                } else if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL,
                            Uri.parse(url));
                    widget.getContext().startActivity(intent);
                    return true;
                } else if (url.startsWith("mailto:")) {
                    Intent intent = new Intent(Intent.ACTION_SENDTO,
                            Uri.parse(url));
                    widget.getContext().startActivity(intent);
                    return true;
                }
                return true;
            }
        }

        return super.onTouchEvent(widget, buffer, event);
    }

    public static MovementMethod getInstance() {
        return linkMovementMethod;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM