zoukankan      html  css  js  c++  java
  • Android小感悟-重写textview组件感悟

    虽然Android为我们提供了大量的组件,但是有时候我们还是显得不够用必要时还是要重写逐渐,这篇就是要记下我重写TextView的感悟

    我的目的是让textview把显示内容中用#好括住和@到空格的部分变为蓝色

    首先定义一个WeiboTextView类,我们要是textview中的文字变色要是用的类是android.text.

    public class WeiboTextView extends TextView {
    
        public WeiboTextView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
    
        public WeiboTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public WeiboTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public void setTextHightLight() {
            
            String text = (String) this.getText();
            SpannableStringBuilder spannable = new SpannableStringBuilder(text);
            // 用于可变字符
            //储存话题的“#”位置和用户的"@"与空格位置
            //查找#和@和空格的位置
            for(int i=0;i<text.length();i++)
            {
                int start = text.indexOf("#",i);
                int end = text.indexOf("#",start+1);
                if(start!=-1&&end!=-1)
                {
                    spannable.setSpan(new ForegroundColorSpan(Color.BLUE), start, end+1,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    i = end;
                }
            }
            for(int i =0;i<text.length();i++)
            {
                int start = text.indexOf("@",i);
                int end = text.indexOf(" ",start+1);
                if(start!=-1&&end!=-1)
                {
                    spannable.setSpan(new ForegroundColorSpan(Color.BLUE), start, end+1, 
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    i = end;
                }
            }
    
        
            setText(spannable);
        }
    }
  • 相关阅读:
    bzoj1042 [ HAOI2008 ] --容斥原理+完全背包
    bzoj1079 [ SCOI2008 ] --记忆化搜索
    bzoj1584 [ Usaco2009 Mar ] --DP
    bzoj4724 [ POI2017 ] --数论
    bzoj3208--记忆化搜索
    bzoj3095--数学题
    resque 遍历加载job目录下的类
    php,js清除cookie
    nginx 设置 fastcgi缓存
    php缓冲区 sapi缓冲区
  • 原文地址:https://www.cnblogs.com/Jabba93/p/2674840.html
Copyright © 2011-2022 走看看