【问题标题】:Android make phone numbers clickable, autodetectAndroid使电话号码可点击,自动检测
【发布时间】:2018-02-14 02:51:34
【问题描述】:

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

网络上的这些元素以多种方式进行格式化,因此有一些内置函数可以检测这些内容。

如何在我的应用程序中允许这样做?我有一个以纯文本形式显示联系信息的页面,我希望用户能够点击。

我是否绝对需要为每个文本视图创建点击侦听器,或者是否有我只需要启用的系统功能?

【问题讨论】:

    标签: java android sdk click textview


    【解决方案1】:

    使用

    android:autoLink="phone"
    

    在xml布局文件中的textView中

    【讨论】:

    • android:autoLink="phone"
    • 酷!如何获取链接点击事件?
    • @jeet.chanchawat 不幸的是,这是在内部处理的。因此你不能自定义它
    • 我对数字的经验是数字以 0 开头,然后它不可点击,但是当我用 +92 之类的国家代码替换 0 时,它工作正常.. 希望对其他人有所帮助
    • 您也可以使用 android:autoLink="all" 来检测电话号码、链接等。
    【解决方案2】:

    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

    【讨论】:

    • 您可以编辑答案以更清楚地了解 Linkifi 是什么。我的第一个想法是,这是一个 3rd 方库,我自动跳过了你的答案。但 Linkify 确实是终极解决方案! :)
    【解决方案3】:
    import android.text.util.Linkify;
    
    Linkify.addLinks(text, Linkify.PHONE_NUMBERS);
    

    【讨论】:

      【解决方案4】:

      你可以像这样在TextView中使用它,

      如下设置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" />
      

      但是,

      由于某种原因,上述代码并非一直有效。所以,也添加下面的代码,

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

      【讨论】:

        【解决方案5】:
        android:autoLink="phone"
        

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

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

        然后使用这个静态辅助方法将 Web 链接支持添加到我的 TextViews

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

        【讨论】:

          【解决方案6】:

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

          使用该库的示例代码。

          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);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-07
            • 1970-01-01
            相关资源
            最近更新 更多