Android 常用工具方法

1、设置文本是否加粗

    /**
     * 设置文本加粗
     * @param textView 文本
     * @param isBold   是否加粗
     */
    public static void setTextBold(TextView textView, boolean isBold) {
        if (textView != null) {
            if (isBold) {
                textView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
            } else {
                textView.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
            }
        }
    }

2、设置textView选中及未选中文字颜色

    /***
     * 设置textView选中及未选中文字颜色
     * @param  textView  控件
     * @param  normalColor 常规文字颜色
     * @param  selectorColor  选中文字颜色
     */
    public static void addSelectorText(TextView textView, String normalColor, String selectorColor) {
        int[][] states = new int[][]{
                new int[]{-android.R.attr.state_selected}, // unchecked
                new int[]{android.R.attr.state_selected}   // checked
        };//把两种状态一次性添加

        int[] colors = new int[]{
                Color.parseColor(normalColor),
                Color.parseColor(selectorColor)
        };//把两种颜色一次性添加

        ColorStateList colorStateList = new ColorStateList(states, colors);
        textView.setTextColor(colorStateList);
    }

3、加载imageView的selector效果(使用的是glide库)

        /**
         * 加载imageView的selector效果
         * @param normalUrl    常规图片地址
         * @param selectorUrl  选中图片地址
         * @param imageView 图片控件
         */
        fun addSelectorFromNet(
            context: Context?, normalUrl: String?, selectorUrl: String?, imageView: ImageView?
        ) {
            if (context == null || Utils.isEmpty(normalUrl) ||
                Utils.isEmpty(selectorUrl) || imageView == null
            ) {
                return
            }

            val drawable = StateListDrawable()
            Glide.with(context).asBitmap().load(selectorUrl).into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    val draw = BitmapDrawable(resource)
                    drawable.addState(intArrayOf(android.R.attr.state_selected), draw)

                    Glide.with(context).asBitmap().load(normalUrl)
                        .into(object : SimpleTarget<Bitmap>() {
                            override fun onResourceReady(
                                resource: Bitmap,
                                transition: Transition<in Bitmap>?
                            ) {
                                val draw = BitmapDrawable(resource)
                                drawable.addState(intArrayOf(-android.R.attr.state_selected), draw)
                                imageView.setImageDrawable(drawable)
                            }
                        })
                }
            })
        }

4、 判断字符串是否为空

    /**
     * 判断字符串是否为空
     *
     * @param content 内容
     * @return 是否为空
     */
    public static boolean isEmpty(String content) {
        return null == content || TextUtils.isEmpty(content) || "null".equals(content);
    }

5、设置TextView中划线

    /***
     * 设置TextView中划线
     * @param textView tv
     */
    public static void setTextFlags(TextView textView) {
        if (textView != null) {
            //不使用硬件加速===解决部分机型不显示中划线问题
            textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

            //方法一:
            //抗锯齿
            textView.getPaint().setAntiAlias(true);
            // 设置中划线并加清晰
            textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        }

        //方法二:
//        String string = textView.getText().toString();
//        SpannableString sp = new SpannableString(string);
//        sp.setSpan(new StrikethroughSpan(), 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        textView.setText(sp);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值