修改TextView的字体颜色

本文介绍了一个用于Android应用开发的文本操作工具类,包括修改文本颜色、设置字体加粗及文本有效性验证等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
 * cy
 */
public final class TextUtil {

    private TextUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 修改文本字体颜色
     *
     * @param context
     * @param textView
     * @param text
     */
    public static void setTextColor(Context context, TextView textView, String text, int color, int start, int end) {

        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(color), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableStringBuilder);
    }


    /**
     * TextView字体是否加粗
     *
     * @param view
     * @param isBold
     */
    public static void setTextBold(TextView view, boolean isBold) {
        if (isBold) {
            view.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//粗体
        } else {
            view.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));//正常
        }


    }

    /**
     * @param str
     * @return
     */
    public static boolean isEmpty(CharSequence str) {
        return ((str == null) || (str.length() == 0));
    }

    /**
     * 字符串的值是否有效
     *
     * @param s
     * @return
     */
    public static boolean textIsValid(String s) {
        if (null != s && s.trim().length() != 0) {
            return true;

        } else {
            return false;
        }
    }

    /**
     * @param params
     * @return
     */
    public static boolean paramsIsValid(String... params) {
        for (String param : params) {
            if (!textIsValid(param)) {
                return false;
            }
        }
        return true;
    }


}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值