android:电话号码自动链接并非始终有效 [英] android:autoLink for phone numbers doesn't always work

查看:222
本文介绍了android:电话号码自动链接并非始终有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的TextView,其本地电话号码为852112222或(8 5)211 2222.

I have a simple TextView with local phone number 852112222 or (8 5) 211 2222.

我需要它可以被点击,所以自然地我使用了android:autoLink="all".
但是由于某种原因,我不知道并非所有设备上的电话号码都相同".

I need it to be clickable, so naturally I used android:autoLink="all".
But for some reason I don't understand same phone number is not "linkified" on all devices.

在普通的Genymotion设备上,它不起作用.在我的个人OnePlus2设备上,它可以正常工作. 在不同的设备上进行了一堆测试-没运气.

On plain Genymotion device it didn't work. On my personal OnePlus2 device it worked. Tested on bunch on different devices - no luck.

可能是什么问题?
用户帐户首选项? Android版? ORM?还有吗?

What could be the issue?
User account preferences? Android version? ORM? Something else?

推荐答案

这是我的调查.

我创建了一个新项目,并在activity_main.xml中的文本视图中添加了android:autoLink="all".感谢Android Studio的开发人员,我可以看到预览,并且发现了一些有趣的东西:

I created a new project, and added android:autoLink="all" to a text view in activity_main.xml. Thanks to the developers of Android Studio, I could see the preview, and I found something interesting:

  • 12345未链接
  • 123456未链接
  • 1234567 已链接
  • 12345678 已链接
  • 123456789未链接
  • 1234567890不喜欢
  • 12345678901 已链接
  • 123456789012未链接
  • 12345 not linked
  • 123456 not linked
  • 1234567 linked
  • 12345678 linked
  • 123456789 not linked
  • 1234567890 not likned
  • 12345678901 linked
  • 123456789012 not linked

结果在我的手机上是相同的.因此,我查看了源代码,搜索了关键字 autolink ,然后发现了这一点:

The result is the same on my phone. So I looked into the source code, searched for the keyword autolink, then I found this:

private void setText(CharSequence text, BufferType type,
                     boolean notifyBefore, int oldlen) {

    ...
    // unconcerned code above

    if (mAutoLinkMask != 0) {
        Spannable s2;

        if (type == BufferType.EDITABLE || text instanceof Spannable) {
            s2 = (Spannable) text;
        } else {
            s2 = mSpannableFactory.newSpannable(text);
        }

        if (Linkify.addLinks(s2, mAutoLinkMask)) {
            text = s2;
            type = (type == BufferType.EDITABLE) ? BufferType.EDITABLE : BufferType.SPANNABLE;

            /*
             * We must go ahead and set the text before changing the
             * movement method, because setMovementMethod() may call
             * setText() again to try to upgrade the buffer type.
             */
            mText = text;

            // Do not change the movement method for text that support text selection as it
            // would prevent an arbitrary cursor displacement.
            if (mLinksClickable && !textCanBeSelected()) {
                setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }

    ...
    // unconcerned code above
}

因此关键字现在为Linkify.对于addLinks:

So the keyword is Linkify now. For addLinks:

public static final boolean addLinks(@NonNull Spannable text, @LinkifyMask int mask) {
    ...

    if ((mask & PHONE_NUMBERS) != 0) {
        gatherTelLinks(links, text);
    }

    ...
}

private static final void gatherTelLinks(ArrayList<LinkSpec> links, Spannable s) {
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    Iterable<PhoneNumberMatch> matches = phoneUtil.findNumbers(s.toString(),
            Locale.getDefault().getCountry(), Leniency.POSSIBLE, Long.MAX_VALUE);
    for (PhoneNumberMatch match : matches) {
        LinkSpec spec = new LinkSpec();
        spec.url = "tel:" + PhoneNumberUtils.normalizeNumber(match.rawString());
        spec.start = match.start();
        spec.end = match.end();
        links.add(spec);
    }
}

然后,发生了一件很糟糕的事情,SDK没有PhoneNumberUtil,特别是下面的这3个类:

Then, something bad happened, the SDK doesn't have PhoneNumberUtil, specifically these 3 classes below:

import com.android.i18n.phonenumbers.PhoneNumberMatch;
import com.android.i18n.phonenumbers.PhoneNumberUtil;
import com.android.i18n.phonenumbers.PhoneNumberUtil.Leniency;

目前,第一个原因浮出水面:Locale.getDefault().getCountry().
所以我去设置,找到语言,选择中文.结果如下:

For now, the first reason surfaced: Locale.getDefault().getCountry().
So I went to setting, found language, selected Chinese. The result is below:

  • 12345 已链接
  • 123456 已链接
  • 1234567 已链接
  • 12345678 已链接
  • 123456789 已链接
  • 1234567890 已链接
  • 12345678901 已链接
  • 123456789012 已链接
  • 12345 linked
  • 123456 linked
  • 1234567 linked
  • 12345678 linked
  • 123456789 linked
  • 1234567890 linked
  • 12345678901 linked
  • 123456789012 linked

其次,对于com.android.i18n.phonenumbers的程序包,我发现了这一点:
https ://android.googlesource.com/platform/external/libphonenumber/+/ics-factoryrom-2-release/java/src/com/android/i18n/phonenumbers
如果您有兴趣,请查看上面的链接.请注意URL:ics-factoryrom-2-release.因此,我非常怀疑这是否依赖于平台.

Secondly, for the package of com.android.i18n.phonenumbers, I found this:
https://android.googlesource.com/platform/external/libphonenumber/+/ics-factoryrom-2-release/java/src/com/android/i18n/phonenumbers
If you are interested, check the link above. Notice in the URL: ics-factoryrom-2-release. So I highly doubt that this is platform-dependent.

对于该解决方案, CleverAndroid 是正确的,完全控制LinkMovementMethod是个不错的选择选项.

For the solution, CleverAndroid is right, taking full control of LinkMovementMethod is a good option.

这篇关于android:电话号码自动链接并非始终有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆