zoukankan      html  css  js  c++  java
  • Android TextView 高亮字体并添加点击事件

    运行效果

     

    package com.zutil.lib;
    
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.Spannable;
    import android.text.SpannableStringBuilder;
    import android.text.Spanned;
    import android.text.TextPaint;
    import android.text.method.LinkMovementMethod;
    import android.text.style.ClickableSpan;
    import android.text.style.ForegroundColorSpan;
    import android.text.style.StyleSpan;
    import android.text.style.UnderlineSpan;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private TextView tv1 ;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tv1 = (TextView) findViewById( R.id.tv1 );
    
            String url1 =  "这是测试";
            String url2 = "点击一下试试看";
            String url = url1 + url2;
            SpannableStringBuilder style = new SpannableStringBuilder(url1 + url2);
            TextViewURLSpan myURLSpan = new TextViewURLSpan();
            style.setSpan(myURLSpan, url1.length(), url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            style.setSpan(new StyleSpan(Typeface.NORMAL ), url1.length(), url.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            style.setSpan(new UnderlineSpan(), url1.length(), url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimaryDark)), url1.length(), url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          //  style.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.colorAccent)), url1.length(),url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv1.setText(style);
    
            //设置超链接为可点击状态
            tv1.setMovementMethod(LinkMovementMethod.getInstance());
    
    
        }
    
        class TextViewURLSpan extends ClickableSpan {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(getResources().getColor(R.color.colorAccent));
                ds.setUnderlineText(false); //去掉下划线
            }
    
            @Override
            public void onClick(View widget) {//点击事件
                Toast.makeText( MainActivity.this, "点击了", Toast.LENGTH_SHORT).show();
            }
        }
    
    }
  • 相关阅读:
    php安全模式笔记
    ./configure,make,make install的作用(转)
    composer自动载入类库的方式
    Specified key was too long; max key length is 1000 bytes
    海量数据中找出前k大数(topk问题)
    斐波那契数列n项的值。(递归和非递归算法Golang实现)
    基于Docker和Golang搭建Web服务器
    Nginx简单介绍以及linux下使用Nginx进行负载均衡的搭建
    php实现商城秒杀
    一致性hash (PHP)
  • 原文地址:https://www.cnblogs.com/zhaoyanjun/p/5446873.html
Copyright © 2011-2022 走看看