textView 實(shí)現(xiàn) 文本改變顏色 或者 加點(diǎn)擊事件 根據(jù)需求部分文字改變

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import project.com.pcddproject.R;

/**

  • <p>
  • SpannableStringBuilder和SpannableString主要通過使用setSpan(Object what, int start, int end, int flags)改變文本樣式。
  • 對(duì)應(yīng)的參數(shù):
  • start: 指定Span的開始位置
  • end: 指定Span的結(jié)束位置,并不包括這個(gè)位置。
  • flags:取值有如下四個(gè)
  • Spannable. SPAN_INCLUSIVE_EXCLUSIVE:前面包括,后面不包括,即在文本前插入新的文本會(huì)應(yīng)用該樣式,而在文本后插入新文本不會(huì)應(yīng)用該樣式
  • Spannable. SPAN_INCLUSIVE_INCLUSIVE:前面包括,后面包括,即在文本前插入新的文本會(huì)應(yīng)用該樣式,而在文本后插入新文本也會(huì)應(yīng)用該樣式
  • Spannable. SPAN_EXCLUSIVE_EXCLUSIVE:前面不包括,后面不包括
  • Spannable. SPAN_EXCLUSIVE_INCLUSIVE:前面不包括,后面包括
  • what: 對(duì)應(yīng)的各種Span,不同的Span對(duì)應(yīng)不同的樣式。已知的可用類有:
  • BackgroundColorSpan : 文本背景色
  • ForegroundColorSpan : 文本顏色
  • MaskFilterSpan : 修飾效果,如模糊(BlurMaskFilter)浮雕
  • RasterizerSpan : 光柵效果
  • StrikethroughSpan : 刪除線
  • SuggestionSpan : 相當(dāng)于占位符
  • UnderlineSpan : 下劃線
  • AbsoluteSizeSpan : 文本字體(絕對(duì)大?。?/li>
  • DynamicDrawableSpan : 設(shè)置圖片,基于文本基線或底部對(duì)齊。
  • ImageSpan : 圖片
  • RelativeSizeSpan : 相對(duì)大?。ㄎ谋咀煮w)
  • ScaleXSpan : 基于x軸縮放
  • StyleSpan : 字體樣式:粗體、斜體等
  • SubscriptSpan : 下標(biāo)(數(shù)學(xué)公式會(huì)用到)
  • SuperscriptSpan : 上標(biāo)(數(shù)學(xué)公式會(huì)用到)
  • TextAppearanceSpan : 文本外貌(包括字體、大小、樣式和顏色)
  • TypefaceSpan : 文本字體
  • URLSpan : 文本超鏈接
  • ClickableSpan : 點(diǎn)擊事件
  • <p>
  • //TODO textView 實(shí)現(xiàn) 文本改變顏色 或者 加點(diǎn)擊事件 根據(jù)需求部分文字改變
    */
    public class SpannableStringUtils {
/**
 * 使用SpannableString設(shè)置樣式——字體顏色
 * 修改字體顏色
 *
 * @param content 改變的文本內(nèi)容
 * @param color   "#009ad6"
 * @param start   開始文字的位置 坐標(biāo)從 0開始
 * @param end     改變結(jié)束的位置 ,并不包括這個(gè)位置。
 * @return
 */
public static SpannableString textColor(String content, String color, int start, int end) {
    SpannableString spannableString = new SpannableString(content);
    ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(color));
    spannableString.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    // ((TextView)findViewById(R.id.mode1)).setText(spannableString);
    return spannableString;
}


/**
 * 使用SpannableStringBuilder設(shè)置樣式——字體顏色
 * SpannableStringBuilder
 * 修改字體顏色
 *
 * @param content 改變的文本內(nèi)容
 * @param color   #009ad6
 * @param start   開始文字的位置 坐標(biāo)從 0開始
 * @param end     改變結(jié)束的位置 ,并不包括這個(gè)位置。
 * @return
 */
public static SpannableStringBuilder textColor02(String content, String color, int start, int end) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    // spannableString.append("已經(jīng)開始暴走了");
    ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(color));
    spannableString.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    // ((TextView)findViewById(R.id.mode2)).setText(spannableString);
    return spannableString;
}

/**
 * 使用SpannableStringBuilder設(shè)置樣式——背景顏色
 * 設(shè)置背景顏色 --文本內(nèi)容字體顏色不改變
 * 使用BackgroundColorSpan設(shè)置背景顏色。
 *
 * @param content 改變的文本內(nèi)容
 * @param color   #009ad6
 * @param start   開始文字的位置  坐標(biāo)從 0開始
 * @param end     改變結(jié)束的位置 ,并不包括這個(gè)位置。
 * @return
 */
public static SpannableStringBuilder textBackgroundColor(String content, String color, int start, int end) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    BackgroundColorSpan bgColorSpan = new BackgroundColorSpan(Color.parseColor(color));
    spannableString.setSpan(bgColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //((TextView)findViewById(R.id.mode3)).setText(spannableString);
    return spannableString;
}

/**
 * 使用SpannableStringBuilder設(shè)置樣式——字體大小
 * 設(shè)置字體大小
 * 使用AbsoluteSizeSpan設(shè)置字體大小。
 *
 * @param content  改變的文本內(nèi)容
 * @param textSize 設(shè)置字體大小  代碼中 設(shè)置字體的大小是 px 如需要設(shè)置dp 請先把dp轉(zhuǎn)換為px
 * @param start    開始文字的位置  坐標(biāo)從 0開始
 * @param end      改變結(jié)束的位置 ,并不包括這個(gè)位置。
 * @return
 */
public static SpannableStringBuilder textSize(String content, int textSize, int start, int end) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(textSize);
    spannableString.setSpan(absoluteSizeSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    // ((TextView)findViewById(R.id.mode4)).setText(spannableString);
    return spannableString;
}


/**
 * 使用SpannableStringBuilder設(shè)置樣式——粗體\斜體
 * @param content   改變的文本內(nèi)容
 * @param start    開始文字的位置  坐標(biāo)從 0開始
 * @param end      改變結(jié)束的位置 ,并不包括這個(gè)位置。
 * @param Tepy   字體樣式  1 :粗體  2:斜體 3:粗斜體
 * @return
 */
public static SpannableStringBuilder textSetStyle(String content, int start, int end, int Tepy) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    // TODO setSpan可多次使用 也就是一段字符串上都可以使用  粗體,斜體,粗斜體 如有需要請自行配置使用
   /* StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);//粗體
    spannableString.setSpan(styleSpan, 0, 3, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    StyleSpan styleSpan2 = new StyleSpan(Typeface.ITALIC);//斜體
    spannableString.setSpan(styleSpan2, 3, 6, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    StyleSpan styleSpan3 = new StyleSpan(Typeface.BOLD_ITALIC);//粗斜體
    spannableString.setSpan(styleSpan3, 6, 9, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    ((TextView)findViewById(R.id.mode5)).setText(spannableString);
    */
    StyleSpan styleSpan = null;
    switch (Tepy) {
        case Typeface.BOLD://粗體
            styleSpan = new StyleSpan(Typeface.BOLD);//粗體
            spannableString.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            break;
        case Typeface.ITALIC://斜體
            styleSpan = new StyleSpan(Typeface.ITALIC);//斜體
            spannableString.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            break;
        case Typeface.BOLD_ITALIC://粗斜體
            styleSpan = new StyleSpan(Typeface.BOLD_ITALIC);//粗斜體
            spannableString.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            break;
        default://粗體
            styleSpan = new StyleSpan(Typeface.BOLD);//粗體
            spannableString.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            break;
    }

    return spannableString;
}



/**
 * 使用SpannableStringBuilder設(shè)置樣式——?jiǎng)h除線   文字中間帶橫線
 *
 * @param content   改變的文本內(nèi)容
 * @param start    開始文字的位置  坐標(biāo)從 0開始
 * @param end      改變結(jié)束的位置 ,并不包括這個(gè)位置。
 */
public static SpannableStringBuilder textDeleteLine(String content, int start, int end) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
    spannableString.setSpan(strikethroughSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
   // ((TextView) findViewById(R.id.mode6)).setText(spannableString);
    return spannableString;
}


/**
 * 使用SpannableStringBuilder設(shè)置樣式——下劃線
 * @param content
 * @param start
 * @param end
 * @return
 */
public static SpannableStringBuilder textUnderline(String content, int start, int end) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    UnderlineSpan underlineSpan = new UnderlineSpan();
    spannableString.setSpan(underlineSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //((TextView) findViewById(R.id.mode7)).setText(spannableString);
    return spannableString;
}



/**
 * 使用SpannableStringBuilder設(shè)置樣式——圖片
 * @param context  上下文
 * @param content   改變文本內(nèi)容
 * @param start
 * @param end
 * @param mipmapId 圖片
 * @param isWidthHeight 是否需要測量寬高
 * @return
 */
public static SpannableStringBuilder textImage(Context context,String content,int start, int end,
                                               @DrawableRes int mipmapId,boolean isWidthHeight) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    ImageSpan imageSpan=null;

    if (isWidthHeight){ // 需要根據(jù) 測量圖片的 寬高不
        Drawable drawable = context.getResources().getDrawable(mipmapId);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        imageSpan = new ImageSpan(drawable);
    }else{
        imageSpan = new ImageSpan(context,mipmapId);
    }
    //將index為6、7的字符用圖片替代
    spannableString.setSpan(imageSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
   // ((TextView) findViewById(R.id.mode8)).setText(spannableString);
    return spannableString;
}


/**
 *使用SpannableStringBuilder設(shè)置點(diǎn)擊事件
 * @param textView
 * @param content  文本內(nèi)容
 * @param start    開始文字的位置  坐標(biāo)從 0開始
 * @param end      改變結(jié)束的位置 ,并不包括這個(gè)位置。
 * @param clickableSpan   文字點(diǎn)擊的回調(diào)
 *
 * textView.setText(spannableString);
 * textView.setMovementMethod(LinkMovementMethod.getInstance()); 代碼中指定index為 start--end 的字符都成了可點(diǎn)擊的文本,其他區(qū)域還是不可點(diǎn)擊的。
 *
 */
public static void textClickable(TextView textView,String content,int start, int end,ClickableSpan clickableSpan) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    spannableString.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    textView.setText(spannableString);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
}


/**
 * 使用SpannableStringBuilder設(shè)置 文本顏色 和 點(diǎn)擊事件
 * @param textView
 * @param content
 * @param start
 * @param end
 * @param color
 * @param clickableSpan
 */
public static void textColorAndClickable(TextView textView,String content,int start, int end,
                                         String color,ClickableSpan clickableSpan) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    //文字顏色
    ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor(color));
    spannableString.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //點(diǎn)擊數(shù)據(jù)
    spannableString.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

    textView.setText(spannableString);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
}

/**
 * 使用SpannableStringBuilder事件組合使用
 * 根據(jù)需求到時(shí)自己 封裝改變了
 */
public static void textCombination(Context context,TextView textView,String content) {
    SpannableStringBuilder spannableString = new SpannableStringBuilder();
    spannableString.append(content);
    //圖片
    ImageSpan imageSpan = new ImageSpan(context, R.mipmap.ic_launcher);
    spannableString.setSpan(imageSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //點(diǎn)擊事件
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context, "請不要點(diǎn)我", Toast.LENGTH_SHORT).show();
        }
    };
    spannableString.setSpan(clickableSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //文字顏色
    ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FFFFFF"));
    spannableString.setSpan(colorSpan, 5, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    //文字背景顏色
    BackgroundColorSpan bgColorSpan = new BackgroundColorSpan(Color.parseColor("#009ad6"));
    spannableString.setSpan(bgColorSpan, 5, 8, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

    textView.setText(spannableString);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容