【问题标题】:How to add email link to layout xml, Android如何将电子邮件链接添加到布局 xml,Android
【发布时间】:2012-02-20 13:09:32
【问题描述】:

我有一个 xml 文件,用于在我的应用程序中显示 About AlertDialog。 我能够从文本资源中获得一个文本区域,以及文本右侧的图像。 现在我需要在文本中添加支持电子邮件和可能的主页地址。但我需要它是可点击的。因此,单击电子邮件将发送电子邮件,单击网站将打开浏览器。 如何添加这些链接文本?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:weightSum="1" android:orientation="horizontal" android:baselineAligned="true">
    <TextView android:text="@string/AboutString" android:layout_gravity="center_horizontal" android:id="@+id/textView1" android:layout_height="173dp" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:drawableRight="@drawable/explain"></TextView>
</LinearLayout>

编辑:我选择的解决方案:

                View layout = inflater.inflate(R.layout.about, null);
                Pattern p = Pattern.compile("somemail@domain.net");
                String Scheme = "mailto:somemail@domain.net";
                Linkify.addLinks((TextView)layout.findViewById(R.id.textView1), p, Scheme);

【问题讨论】:

    标签: android email text hyperlink android-alertdialog


    【解决方案1】:

    你需要linkify你的文字。以下是该文章中的一个示例,展示了如何在找到 WikiWord 时添加链接:

    Pattern wikiWordMatcher = Pattern.compile("\\b[A-Z]+[a-z0-9]+[A-Z][A-Za-z0-9]+\\b");
    String wikiViewURL =    "content://com.google.android.wikinotes.db.wikinotes/wikinotes/";
    Linkify.addLinks(noteView, wikiWordMatcher, wikiViewURL);
    

    【讨论】:

      【解决方案2】:

      这对我来说很简单(使用 SDK 版本 23。不确定此属性是否存在于早期版本中)。在 textview 中添加 autoLink="email" 并在 Strings.xml 中提供电子邮件 ID。此电子邮件 ID 以蓝色作为链接,单击后会打开我们的电子邮件。请尝试告诉我。

      <string name="MailText">Click abc@gmail.com to send us a mail for any query</string>
      
      <TextView
          android:id="@+id/mailtext"
          android:text="@string/MailText"
          android:autoLink="email"
          android:layout_height="wrap_content" />
      

      【讨论】:

        【解决方案3】:

        你需要这样的无耻插件:https://github.com/ariefbayu/Clickable-URL-TextView-Example

        基本上,您可以将 TextView 设置为:

        <TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="TextView"
          android:clickable="true" />
        

        在onCreate:

        html.append("<a href='lauch.TCActivity://SENDEMAIL?email=mail@example.com&subject=email subject&body=this is email body'>Email</a>");
        textView.setClickable(true);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        

        并在 TCActivity 中处理:

        if(data.getHost().equals("SENDEMAIL")){
            Log.i("LOG", "Email:" + data.getQueryParameter("email"));
            Log.i("LOG", "Subject:" + data.getQueryParameter("subject"));
            Log.i("LOG", "Body:" + data.getQueryParameter("body"));
        }
        

        就是这样。您现在应该知道下一步该做什么了。

        见:

        【讨论】:

        • 我可以将它添加到字符串中:somemail@domain.com 但它只是将电子邮件着色为链接并且不会当我点击它时不做任何事情。也许如果我添加 clickable = true 它将起作用。基本上,我不想编写自己的发送电子邮件对话框,那是没有意义的。有没有一种简单的方法来调用设备的默认邮寄方式?
        • 在if(data.getHost().equals("SENDEMAIL")){} 内,您可以按照线索中的说明调用Intent.ACTION_SEND
        【解决方案4】:

        Android TextView 有一个名为 autoLink 的属性,除了一些参数之外,其中一个是 email,它将告诉框架将 textView 中的任何类型的电子邮件处理为可点击的。

        请按照以下示例:

        <TextView
            ....
            android:text="youremail@gmail.com"
            android:autoLink = "email"
        />
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。您可以在帮助中心找到更多关于如何写好答案的信息:stackoverflow.com/help/how-to-answer。祝你好运?
        猜你喜欢
        • 1970-01-01
        • 2012-10-29
        • 1970-01-01
        • 2019-04-06
        • 1970-01-01
        • 2013-08-22
        • 2020-01-15
        • 1970-01-01
        • 2021-03-28
        相关资源
        最近更新 更多