【问题标题】:How to make normal links in TextView clickable?如何使 TextView 中的普通链接可点击?
【发布时间】:2013-08-15 20:33:36
【问题描述】:

在我的 Android 应用中,我有一个 TextView。文本可以包含链接。这是一个文本示例:

This is just a test. Click the following link http://www.google.com to visit Google.

请注意,文本不是 HTML;它只是一个普通的文本。

我想做textView.parseLinks()之类的事情,然后在TextView中,http://www.google.com会被超链接并可以点击打开页面。

这可能吗?

谢谢

【问题讨论】:

  • 你说:the text is not HTML it will be just regular text a normal person will put in. 如果用户会输入文本,那么这是 EditText 还是 TextView ?我的问题是用户会输入文本还是预先定义?
  • 不会有两个活动,一个是多行文本编辑,它被保存到数据库中。用户不会知道 html,所以他们会在没有 html 的情况下输入文本和链接。然后在当前视图上,他们之前放置的文本会被下载并放在文本视图上。

标签: android hyperlink textview


【解决方案1】:

尝试在 XML 文件中的 TextView 定义中包含以下内容:

<TextView
    ...
    android:autoLink="web"/>

android:autoLink 的文档说:

控制是否自动找到 url 和电子邮件地址等链接并转换为可点击链接

因此,对于自动查找链接,上述内容可能会有所帮助。试试看。

【讨论】:

  • 超棒的内置解决方案......thanxx 人提供了如此重要的信息。
  • ( super.inbuilt ) 如果我们输入括号中的单词,它还会显示这些单词的链接.. 我该如何防止这种情况发生
【解决方案2】:

这样的事情应该可以工作。

    TextView tv = (TextView) findViewById(R.id.textView1);
    String text = "This is just a test. Click this link here <a href=\"http://www.google.com\">Google</a> to visit google.";
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setText(Html.fromHtml(text));

【讨论】:

    【解决方案3】:
        <TextView
            ...
            android:autoLink="..."/>
    //set  ... by web|email|none|phone|map|all according to your need
    

    // 更改链接颜色添加以下行 android:textColorLink="@color/yourcolor"

    【讨论】:

      【解决方案4】:

      试试这个..它对我有用

        <TextView
          android:id="@+id/text"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:autoLink="web"
          android:text="click here http://www.google.com/"/>
      

      【讨论】:

        【解决方案5】:

        选择URLTextView 中的电话号码的简单方法:

        TextView textView = (TextView)findViewById(R.id.textView1);
        textView.setText("some url is www.google.com phone 7504567890 another url lkgndflg.com ");
        Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
        

        【讨论】:

          猜你喜欢
          • 2011-02-13
          • 2019-08-11
          • 1970-01-01
          • 2012-03-13
          • 2012-09-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多