TextView 添加超链接

TestView添加超链接的方法有两种

一种是谷歌自带的android:aotuLink=""

  <TextView
        android:id="@+id/tz"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text:"www.baidu.com"
        android:autoLink="web"/>

一种自己在class中设置

package com.cyl.soso.gd;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import android.graphics.drawable.Drawable;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView url=(TextView) findViewById(R.id.tz);
        String s1 = "<font  color='blue'><b>百度一下</b></font><br>";
        s1 += "<a href = 'http://www.baidu.com'>点进去百度</a>";
        //<font>:设置颜色和字体。
        //<big>:设置字体大号
        //<small>:设置字体小号
        //<i><b>:斜体粗体
        //<a>:连接网址
        //<img>:图片
        //其中的flags表示:
        //FROM_HTML_MODE_COMPACT:html块元素之间使用一个换行符分隔
        //FROM_HTML_MODE_LEGACY:html块元素之间使用两个换行符分隔
        CharSequence charSequence = Html.fromHtml(s1,Html.FROM_HTML_MODE_COMPACT);
        url.setText(charSequence);
        //为TextView添加链接
        url.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40213204/article/details/84343600