如何在Android的TextView中使电话号码可以点击

5

我有一个文本视图,其中包含电子邮件和电话号码。我的期望效果是,当用户点击电子邮件时,它会打开默认的电子邮件应用程序,并且当用户点击电话号码时,该应用程序会在拨号器中呼叫它。使用以下代码:

XML:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/careers_guidance"
            android:id="@+id/careers_guidance_text"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginTop="5dp"
            android:linksClickable="true"
            android:textColorLink="#0000EE"
            android:autoLink="all"/>

Java:

careersGuidance = view.findViewById(R.id.careers_guidance_text);
    careersGuidance.setText(Html.fromHtml("<p>Help with choosing the right course and with thinking about where this might take you in the future.</p>" +
            "<p>Tel: <a href=\"tel:01274433043\">01274 433043</a></p>Email: <a href=\"mailto:careers@bradfordcollege.ac.uk\">careers@bradfordcollege.ac.uk</a>"));

当我运行我的应用程序时,只有电子邮件是可点击的,并且按照我想要的方式工作,电话号码没有被点击或突出显示。

enter image description here

然而,我注意到如果我从setText Java代码中删除电子邮件地址并运行应用程序,则电话号码会变为蓝色并带有下划线,就像可点击一样,但是当我点击它时,什么也不会发生。
我该如何使其正常工作?
此外,我的清单文件和模拟器设备上都已授予CALL_PHONE权限。

我认为这是一个重复的问题。请尝试使用此链接 https://dev59.com/6nE85IYBdhLWcg3whT5r - LinuxMasterRace
你来自哪里?你的国家电话号码前缀是什么? - HendraWD
2个回答

5

从邮件地址 (.ac.uk) 来看,我猜您来自英国。您只需在电话号码前加上国家区号 +44 代替 0 即可。并且不需要使用 Html.fromHtml

careersGuidance.setText("Help with choosing the right course and with thinking about where this might take you in the future.\n\nTel: +441274433043\n\nEmail: careers@bradfordcollege.ac.uk");

在您的 XML 中,您只需要这个属性。
android:autoLink="all"

0

您需要在电话号码中添加国家代码。 像这样:

careers_guidance_text.setText(Html.fromHtml("
<p>Help with choosing the right course and with thinking about where this might take you in the future.</p>"
+"<p>Tel: <a href=\"tel:+4401274433043\">+441274 433043</a></p>
Email: <a href=\"mailto:careers@bradfordcollege.ac.uk\">
careers@bradfordcollege.ac.uk</a>"));

还必须在 Android 清单文件中添加 CALL PHONE 权限。 uses-permission android:name="android.permission.CALL_PHONE"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接