textview点击指定字符串跳转 textview超链接效果

来源:互联网 发布:前端 知乎 编辑:程序博客网 时间:2024/05/17 22:01

网上搜了很多的博客看写的都不够详尽,每个方法的用法含义写的都不全,甚至方法乱其八糟,所以我也厚着脸皮写下来这个效果吧。

由于这个页面采用MVP,所以这里会有一个IView的积累 ,这个方法自然就要@Override


@Override    public void setBluetext(String zw_longtext, String zw_bluetext) {        // "成为联通用户,可将默认网速免费升至200K/S,且有效期更长!点击联通用户登记页进行申请";        remindlongWords = zw_longtext + "点击联" + zw_bluetext + "进行申请  ";//显示的全部字符串        //绘制颜色部分代码/*        SpannableStringBuilder style = new SpannableStringBuilder(                remindlongWords);        style.setSpan(                new ForegroundColorSpan(getResources().getColor(                        R.color.deepblue_word)), start, end,                Spannable.SPAN_EXCLUSIVE_INCLUSIVE); // 设置指定位置文字的颜色        remindWords.setText(style);        remindWords.setOnClickListener(this);*/        //需要监听click的范围 start end        int start = zw_longtext.length() + 3;        int end = zw_longtext.length() + 3 + zw_bluetext.length();        //1.必须要的新建一个Span        SpannableString spStr = new SpannableString(remindlongWords);        //2.NoLineClickSpan  写好了制定位置的颜色和click事件        NoLineClickSpan clickSpan = new NoLineClickSpan(wantUpDkPresenter.factoryDKMainModel.zw_type,                getResources().getColor(R.color.deepblue_word));        //3.span帮顶下click span        spStr.setSpan(clickSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);        //4.需要设置下str        remindWords.setText(spStr);        //5.设置TextView可以点击        remindWords.setMovementMethod(LinkMovementMethod.getInstance());    }

NoLineClickSpan是一个继承于CLickSpan的类,重写了颜色和click事件,代码如下:


/** * Created by aa on 2015/4/24. * NoLineClickSpan是一个继承于CLickSpan的类,重写了颜色和click事件,代码如下: */public class NoLineClickSpan extends ClickableSpan {    int type;    int showcolor;    public NoLineClickSpan(int type,int cl) {        super();        this.type=type;        showcolor=cl;    }    @Override    public void updateDrawState(TextPaint ds) {        ds.setColor(showcolor);        ds.setUnderlineText(false); //去掉下划线    }    @Override    public void onClick(View widget) {        //processHyperLinkClick(text);//点击超链接时调用        Log.e("onclick","已经被点击");        Intent intent=new Intent(widget.getContext(),UniApplyActivity.class);        intent.putExtra("type",type);        widget.getContext().startActivity(intent);    }}





0 1