TextView超链接

这里面涉及两个知识点——超链接和跳转。以下进行逐一解说:
1.实现超链接:
1.1形成超链接文本

    public static SpannableString getUserlink(String userName,
            String userId) {
        SpannableString ss = new SpannableString(userName);
        URLSpan urlSpan = new URLSpan("gch://" + userId);
        ss.setSpan(urlSpan, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

1.2将其加入到TextView上面:

        mTVText.setText(getUserlink("guchuanhang", "2010111180"));

1.3给该TextView设置权限

        mTVText.setMovementMethod(LinkMovementMethod.getInstance());

注意:这里默认生成的超链接是有下划线&&字体颜色是蓝色的,假设要删除下划线||改动超链接的颜色。能够使用UrlSpanNoUnderline .java代替URLSpan,其代码例如以下:

package com.scott.crash;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextPaint;
import android.text.style.URLSpan;
import android.widget.TextView;

public class UrlSpanNoUnderline extends URLSpan {
    int greenColor=0;
    public UrlSpanNoUnderline(String p_Url) {
        super(p_Url);
        greenColor= Color.parseColor("#00ff00");
    }
    public void updateDrawState(TextPaint p_DrawState) {
        super.updateDrawState(p_DrawState);
        p_DrawState.setUnderlineText(false);
        p_DrawState.setColor(greenColor);
    }
}

參考地址:
https://prativas.wordpress.com/2013/05/20/hyperlinktextcolor/
以下是加入超链接页面的完整代码:

package com.example.androidtest;

import android.app.Activity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.widget.TextView;

public class FirstActivity extends Activity {
    TextView mTVText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTVText = new TextView(this);
        setContentView(mTVText);
        mTVText.setMovementMethod(LinkMovementMethod.getInstance());
        mTVText.setText(getUserlink("guchuanhang", "2010111180"));
        // mTVText.setText(getZanSequence());

    }

    public static SpannableString getUserlink(String userName,
            String userId) {
        SpannableString ss = new SpannableString(userName);
        URLSpan urlSpan = new URLSpan("gch://" + userId);
        ss.setSpan(urlSpan, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }

}

2.实现跳转:
2.1给目标页面加入对应标志(在manifest中声明。相似于“声称,我是维修电脑的。有电脑问题的请找我”)。

在目标Activity中加入怎样intent

    <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="gch" >
                </data>
            </intent-filter>

在Android中的intent-filter中。声明,处理“gch://?

?

?”的url。


2.2在目标Activity中解析url。获取想要的数据:


import android.app.Activity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.URLSpan;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView show = new TextView(this);
        setContentView(show);
        /**
         * 因为gch://有6个字符组成,所以。截取6位之后的字符串就可以
         */
        String urlString = getIntent().getDataString().substring(6);
        show.setText(urlString);
    }

}
posted @ 2017-06-20 10:03  cxchanpin  阅读(200)  评论(0编辑  收藏  举报