栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 常用工具方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 常用工具方法

1、设置文本是否加粗

    
    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选中及未选中文字颜色

    
    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库)

        
        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() {
                override fun onResourceReady(resource: Bitmap, transition: Transition?) {
                    val draw = BitmapDrawable(resource)
                    drawable.addState(intArrayOf(android.R.attr.state_selected), draw)

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

4、 判断字符串是否为空

    
    public static boolean isEmpty(String content) {
        return null == content || TextUtils.isEmpty(content) || "null".equals(content);
    }

5、设置TextView中划线

    
    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);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930869.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号