为了账号安全,请及时绑定邮箱和手机立即绑定

Android 仿微博 网页链接的效果

标签:
Android

[代码]java代码:

package com.linktextdemo;

 

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.text.SpannableString;

import android.text.Spanned;

import android.text.TextPaint;

import android.text.method.LinkMovementMethod;

import android.text.style.ClickableSpan;

import android.text.style.ImageSpan;

import android.view.View;

import android.widget.TextView;

 

import java.util.HashMap;

import java.util.Map;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

 

 

 

/**

 *  类似微博 将链接([(http(s)://)]www.baidu.com) 替换成 网页链接

 *  实现思路

 *  1、找出字符串中的网址(Bug one: 我这里用了两个简单的正则,不能识别不正确的网址如:https://、www等 )

 *  2、将找出的网址替换成 图片(ic_link)+网址链接,并设置网址链接的颜色和点击事件

 *

 */

public class MainActivity extends AppCompatActivity {

    //待转换字符串

//    www.woaoo.net

    private String info = "#我爱JAVA# www";

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        TextView textView = (TextView) findViewById(R.id.one);

 

 

 

        Map<string, integer=""> map = getHttpPostion();

        if (map != null) {

            info = info.replace(info.substring(map.get("start"), map.get("end")), " 网页链接");

            //实例化一个Spannable对象

            SpannableString spannableString = new SpannableString(info);

            Map<string, integer=""> masp = getWebLink(" 网页链接");

//        spannableString.setSpan(, masp.get("start"), masp.get("end"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

 

//        //设置字体大小

//        spannableString.setSpan(new RelativeSizeSpan((float) 1), masp.get("start"), info.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

 

            //设置图片表情

            Drawable drawable = getResources().getDrawable(R.drawable.ic_link);

            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

 

 

            spannableString.setSpan(new ImageSpan(drawable), masp.get("start") - 1, masp.get("start"), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

            ClickableSpan clickSpan = new NoLineClickSpan(info); //设置超链接

            spannableString.setSpan(clickSpan, masp.get("start") - 1, info.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

 

            textView.setText(spannableString);

            textView.setMovementMethod(LinkMovementMethod.getInstance());

        } else {

            textView.setText(info);

        }

    }

 

    private class NoLineClickSpan extends ClickableSpan {

        String text;

 

        public NoLineClickSpan(String text) {

            super();

            this.text = text;

        }

 

        @Override

        public void updateDrawState(TextPaint ds) {

            ds.setColor(Color.parseColor("#0097cc"));

            ds.setUnderlineText(false); //去掉下划线

        }

 

        @Override

        public void onClick(View widget) {

            startActivity(new Intent(MainActivity.this, MainActivity.class));

        }

    }

 

    /**

     * 获取网址的位置

     */

    public Map<string, integer=""> getHttpPostion() {

 

        //网址 已http/https 开头

//            Pattern pattern = Pattern.compile("((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?", Pattern.CASE_INSENSITIVE);

//            Pattern pattern = Pattern.compile("[^\\s]*((<\\s*[aA]\\s+(href\\s*=[^>]+\\s*)>)(.*)).*", Pattern.CASE_INSENSITIVE);

        Map<string, integer=""> map = null;

        Pattern pattern = null;

        if (info.contains("http")) {

            pattern = Pattern.compile("http:.*");

        } else if (info.contains("www")) {

            pattern = Pattern.compile("www.*");

        }

 

        if (pattern != null) {

            map = new HashMap<string, integer="">();

            Matcher matcher = pattern.matcher(info);

            if (matcher.find()) {

                map.put("start", matcher.start());

                map.put("end", matcher.end());

            }

        }

        return map;

 

    }

 

    /**

     * ( 网页链接)

     * 替换后,将图片放到 空格的位置

     */

    public Map<string, integer=""> getWebLink(String keyword) {

        Map<string, integer=""> map = new HashMap<string, integer="">();

        Pattern pattern = Pattern.compile(keyword);

        Matcher matcher = pattern.matcher(info);

        if (matcher.find()) {

            map.put("start", matcher.start());

            map.put("end", matcher.end());

        }

        return map;

 

    }

 

}

</string,></string,></string,></string,></string,></string,></string,></string,>

 

写在前面的一堆感谢

主要 copy  http://www.cnblogs.com/lichenwei/p/4411607.html   李晨玮 这位大神的代码和思路 稍微修改了下
and Android【E·家人】 46871359  群里好心大牛们的水乳交融!

原文链接:http://www.apkbus.com/blog-795429-60469.html


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消