Android使电话号码可点击,自动检测

时间:2011-06-27 18:35:58

标签: java android sdk click textview

当我在网站上使用Android并阅读电子邮件时,我注意到我可以点击地址加载到谷歌地图,或点击电话号码来打电话,或点击电子邮件并发送电子邮件。

Web上的这些元素以各种方式进行格式化,因此有一些内置函数可以检测这些类型的东西。

如何在我的应用中允许此操作?我有一个以纯文本显示联系信息的页面,我希望用户能够点击。

我是否绝对需要为每个textview创建clicklistener,或者是否需要启用系统功能?

6 个答案:

答案 0 :(得分:58)

使用

android:autoLink="phone"

在xml布局文件的textView中

答案 1 :(得分:21)

Android有一个明确用于此目的的实用程序:Linkify

TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);

另请参阅:https://android-developers.googleblog.com/2008/03/linkify-your-text.html

答案 2 :(得分:18)

import android.text.util.Linkify;

Linkify.addLinks(text, Linkify.PHONE_NUMBERS);

答案 3 :(得分:3)

您可以在TextView中使用它,

设置 android:autoLink ="电话" ,如下所示,

<TextView
    android:layout_width="fill_parent"
    android:id="@+id/text"
    android:layout_height="wrap_content"
    android:autoLink="phone"
    android:gravity="center"
    android:linksClickable="true"
    android:text="@string/txtCredits" />

<强>然而,

出于某种原因,上面的代码不能一直运行。所以,还要添加以下代码,

TextView textView = (TextView) findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());

答案 4 :(得分:1)

android:autoLink="phone"

在所有手机上为我工作...... 除了三星。 因此,我选择了以下选项。已转换的电话号码文字以支持click to call

<a href="tel:+4930123456789">+49 / 30 123456789</a>

然后使用此静态帮助器方法向我的TextViews添加Web链接支持

public static void linkifyTextViews(@NonNull TextView... textViews) {
    for (TextView textView : textViews) {
        Linkify.addLinks(textView, Linkify.WEB_URLS);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

答案 5 :(得分:0)

如果您想检测不同的模式,如电子邮件,联系号码,网络链接,并为这些模式设置单独的点击实施,我建议您使用CustomClickableEmailPhoneTextview

enter image description here

使用该库的示例代码。

CustomPartialyClickableTextview customPartialyClickableTextview= (CustomPartialyClickableTextview) findViewById(R.id.textViewCustom);

                /**
                 * Create Objects For Click Patterns
                 */
                ClickPattern email=new ClickPattern();
                ClickPattern phone=new ClickPattern();
                ClickPattern weblink=new ClickPattern();

                /**
                 * set Functionality for what will happen on click of that pattern
                 * In this example pattern is email
                 */
                email.setOnClickListener(new ClickPattern.OnClickListener() {
                    @Override
                    public void onClick() {

                        Toast.makeText(MainActivity.this,"email clicked",Toast.LENGTH_LONG).show();


                    }
                });

                /**
                 * set Functionality for what will happen on click of that pattern
                 * In this example pattern is phone
                 */
                phone.setOnClickListener(new ClickPattern.OnClickListener() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this,"phone clicked",Toast.LENGTH_LONG).show();

                    }
                });

                /**
                 * set Functionality for what will happen on click of that pattern
                 * In this example pattern is weblink
                 */
                weblink.setOnClickListener(new ClickPattern.OnClickListener() {
                    @Override
                    public void onClick() {
                        Toast.makeText(MainActivity.this,"website clicked",Toast.LENGTH_LONG).show();

                    }
                });

                /**
                 * set respective regex string to be used to identify patter
                 */
                email.setRegex("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b"); // regex for email
                phone.setRegex("[1-9][0-9]{9,14}"); // regex for phone number
                weblink.setRegex("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"); // regex for weblink

                /**
                 * add click pattern to the custom textview - first parameter is tag for reference second parameter is ClickPattern object
                 */
                customPartialyClickableTextview.addClickPattern("email",email);
                customPartialyClickableTextview.addClickPattern("phone",phone);
                customPartialyClickableTextview.addClickPattern("weblink",weblink);
相关问题