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

标签 android textview android-phone-call

我有一个包含电子邮件和电话号码的 TextView 。我想要的效果是,当用户点击电子邮件时,它会打开默认的电子邮件应用程序,当用户点击电话号码时,应用程序会在拨号器中将其拉出。使用以下代码:

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 代码中删除电子邮件地址,然后运行该应用程序。电话号码变成蓝色并带有下划线,就好像它可以点击一样,但是当我点击它时,没有任何反应。

我怎样才能让它工作?

此外,我在我的 list 文件和我的模拟器设备上都授予了 CALL_PHONE 权限。

最佳答案

从电子邮件 (.ac.uk) 来看,我猜你来自英国。您只需要添加您所在国家/地区的电话号码前缀来替换 0,即 +44。而且您不需要使用 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"

关于android - 如何在 Android 的 TextView 中使电话号码可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717190/

相关文章:

android - 设备充电时 CPU 是否保持唤醒状态?

Android Linkify web 和 @mentions 都在同一个 TextView 中

android - 将 TelecomManager 与我们的自定义协议(protocol)结合使用

android - TelephonyManager 为 IMEI 号码 : what can cause this? 返回 null

android - AndEngine:PIXEL_TO_METER_RATIO的使用

android - SendBroadcast 可能会导致哪些安全问题,什么是更好的方法?

android - 在 LinearLayout 中对齐 TextView 和 ImageView

android - 在问卷应用程序中使用 textView 数组

android - 从连接到 Linux 的 Android 手机进行语音通话

java - android 电话 - android.telecom.Call