Java Android使电话号码可点击,自动检测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6497327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android make phone numbers clickable, autodetect
提问by CQM
When I am using android on websites and reading emails, I notice that I can click on addresses to load into google maps, or click on phone numbers to call, or click on emails and send an email.
当我在网站上使用 android 和阅读电子邮件时,我注意到我可以点击地址加载到谷歌地图,或点击电话号码拨打电话,或点击电子邮件并发送电子邮件。
These elements on the web are formatted in a variety of ways, so there is some built in function that detects these sort of things.
网络上的这些元素以多种方式格式化,因此有一些内置函数可以检测这些类型的事物。
How do I allow this within my app? I have a page which displays contact information in plain text and I would like the user to just be able to click.
我如何在我的应用程序中允许它?我有一个页面以纯文本形式显示联系信息,我希望用户能够点击。
Do I Absolutely need to create clicklisteners for each textview or is there a system function I just need to enable?
我是否绝对需要为每个文本视图创建点击监听器,或者是否有我只需要启用的系统功能?
采纳答案by zienkikk
Android has a utility expressly for this purpose: Linkify
Android 有一个专门用于此目的的实用程序:Linkify
TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);
See also: https://android-developers.googleblog.com/2008/03/linkify-your-text.html
另见:https: //android-developers.googleblog.com/2008/03/linkify-your-text.html
回答by Keith Maurino
import android.text.util.Linkify;
Linkify.addLinks(text, Linkify.PHONE_NUMBERS);
回答by Amt87
Use
用
android:autoLink="phone"
in textView in the xml layout file
在 xml 布局文件中的 textView
回答by Nitin Jaiman
If you want to detect different patterns like emails, contact numbers, weblink and set a separate on click implementations for these patterns I suggest you to use CustomClickableEmailPhoneTextview
如果您想检测不同的模式,如电子邮件、联系电话、网络链接并为这些模式设置单独的点击实现,我建议您使用CustomClickableEmailPhoneTextview
Sample Code to use the library.
使用库的示例代码。
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);
回答by luckyhandler
android:autoLink="phone"
was working for me on all phones... except Samsung. Therefore, I chose the following option. Transformed phone number texts to support click to call:
在所有手机上都在为我工作......除了三星。因此,我选择了以下选项。转换电话号码文本以支持点击通话:
<a href="tel:+4930123456789">+49 / 30 123456789</a>
and then used this static helper method to add web link support to my TextViews
然后使用这个静态辅助方法向我的 TextViews 添加 web 链接支持
public static void linkifyTextViews(@NonNull TextView... textViews) {
for (TextView textView : textViews) {
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
回答by Yuvraj Pandey
You can use it in TextView like this,
你可以像这样在 TextView 中使用它,
Set android:autoLink="phone"as below,
设置android:autoLink="phone"如下,
<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" />
However,
然而,
For some reason above code does not work all time. So, add below code also,
由于某种原因,上面的代码并不总是有效。因此,还添加以下代码,
TextView textView = (TextView) findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());