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);
}