Android(java)自定义文本框 实现多种颜色可点击文本显示
程序员文章站
2022-06-09 08:09:36
...
应用app 开发需求,一个登录页面就有许多工作需要做,比如 账号认证的提示、验证码按钮的倒计时,以及用户协议隐私协议等各种协议的显示等等。
本文就针对 用户隐私协议的显示文本进行封装处理,来统一管理app 的各处的协议信息的显示。
为了好用,本次是使用builder 模式进行的封装,完整代码如下:
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
public class TextContentUtil {
private final String prefixContent;//前缀
private final String suffixContent;//后缀
private final String clickContent;//可点击内容
private final TextView textView;
private final SpannableString clickField;
private final OnFieldClick onFieldClick;
private final boolean HAS_SUFFIX;
private final boolean HAS_PREFIX;
private final boolean HAS_COLOR;
private final int color;
private final boolean HAS_UNDERLINE;
public interface OnFieldClick {
void callback(String clickMessage);
}
private TextContentUtil(Builder builder) {
this.prefixContent = builder.prefixContent;
this.clickContent = builder.clickContent;
this.suffixContent = builder.suffixContent;
this.HAS_PREFIX = builder.HAS_PREFIX;
this.HAS_SUFFIX = builder.HAS_SUFFIX;
this.HAS_COLOR = builder.HAS_COLOR;
this.HAS_UNDERLINE = builder.HAS_UNDERLINE;
this.clickField = builder.clickField;
this.textView = builder.textView;
this.color = builder.color;
this.onFieldClick = builder.onFieldClick;
}
public void init() {
clickField.setSpan(new ClickableSpan() {
@Override
public void onClick(View view) {
//点击的响应事件
onFieldClick.callback(clickContent);
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
//设置字体颜色和下划线控制
if (HAS_COLOR) {
ds.setColor(color);
}
ds.setUnderlineText(HAS_UNDERLINE);
}
}, 0, clickField.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (HAS_PREFIX) {
this.textView.setText(this.prefixContent);
}
this.textView.append(this.clickField);
if (HAS_SUFFIX) {
this.textView.append(this.suffixContent);
}
this.textView.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
this.textView.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明
}
public static class Builder {
private String prefixContent;//前缀
private String suffixContent;//后缀
private String clickContent;//可点击内容
private TextView textView;
private SpannableString clickField;
private OnFieldClick onFieldClick;
private boolean HAS_SUFFIX;
private boolean HAS_PREFIX;
private boolean HAS_COLOR;
private int color;
private boolean HAS_UNDERLINE;
public Builder setModelPre(boolean HAS_PREFIX) {
this.HAS_PREFIX = HAS_PREFIX;
return this;
}
public Builder setModelSuf(boolean HAS_SUFFIX) {
this.HAS_SUFFIX = HAS_SUFFIX;
return this;
}
public Builder setModelLine(boolean HAS_UNDERLINE) {
this.HAS_UNDERLINE = HAS_UNDERLINE;
return this;
}
public Builder setModelColor(boolean HAS_COLOR) {
this.HAS_COLOR = HAS_COLOR;
return this;
}
public Builder setClickFieldColor(int clickFieldColor) {
this.color = clickFieldColor;
return this;
}
public Builder setClickContent(String clickContent) {
this.clickContent = clickContent;
return this;
}
public Builder setPrefixContent(String prefixContent) {
this.prefixContent = prefixContent;
return this;
}
public Builder setSuffixContent(String suffixContent) {
this.suffixContent = suffixContent;
return this;
}
public Builder initSingleClickField(OnFieldClick onFieldClick) {
this.onFieldClick = onFieldClick;
this.clickField = new SpannableString(clickContent);
return this;
}
public Builder setTextView(TextView textView) {
this.textView = textView;
return this;
}
public TextContentUtil build() {
return new TextContentUtil(this);
}
}
}
使用方式:
tvInfo = fv(R.id.tv_sign_xy);
TextContentUtil.Builder builder = new TextContentUtil.Builder();
builder.setModelPre(true)
.setPrefixContent("hello")
.setModelSuf(true)
.setSuffixContent("nihao")
.setClickContent("wahaha")
.setModelLine(false)
.setModelColor(true)
.setClickFieldColor(getResources().getColor(R.color.colorTextError))
.setTextView(tvInfo)
.initSingleClickField(ToastUtil::showToast).build().init();
本人自觉 封装简介程度还有不足,欢迎各位朋友提供优化意见。
上一篇: 【原创】MySQL 以及 Python 实现排名窗口函数
下一篇: AMD Opteron