先看效果:
textview中穿插可点击的文字,可用于阅读同意协议啥的,或者动态列表中的链接/昵称啥的
ClickableSpan 正好适合这个效果
非常简单的用法:
xml中
<com.andromeda.volley.tcHoutai.myViews.MyColorfulText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:orientation="vertical"
android:id="@+id/colorful"
android:layout_centerInParent="true">
</com.andromeda.volley.tcHoutai.myViews.MyColorfulText>
java中
private MyColorfulTex colorful;
....
colorful=(MyColorfulText)findViewById(R.id.colorful);
colorful.from(15,0xff8a8a8a,0xffeb5a49,0xffFBB7A2,true)
.addText("点击提交即意味着您充分阅读并同意")
.addColorFulText("《跑步不算运动协议》",1)
.addText("以及牛逼的")
.addColorFulText("《吃饭喝水电子合同》",2)
.addText(",且没有疑问,且看了")
.addColorFulText("《用户吃饭协议》",3)
.setOnItemClick(new MyColorfulText.OnItemClick() {
@Override
public void onClick(int id) {
if(id==1){
//id为1的点击事件
}else if(id==2){
//id为2的点击事件
}else if(id==3){
//id为3的点击事件
}
}
})
.show();
提示:用法如下,2和3自由搭配
例如:
1、 from(文字大小,文字颜色,选中颜色,选中背景色,是否加粗)
2、 addText(String s) 添加基础文字
3、addColorFulText(String s,int id)添加点击文字,以及id,后续根据id判断事件
4:setOnItemClick()回调点击事件
5:show() 展示
代码如下:
注意:继承自LinearLayout,很简单的几步,复制粘贴就行
public class MyColorfulText extends LinearLayout {
private Context context;
private TextView textView;
private SpannableStringBuilder spannableStringBuilder;
private int chooseColor=0xffeb5a49;
private int chooseBgColor=0xffFBB7A2;
public MyColorfulText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
}
public MyColorfulText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
public MyColorfulText(Context context) {
super(context);
this.context=context;
}
public MyColorfulText from(int textSize, int baseColor, int chooseColor,int chooseBgColor, boolean isBold){
textView=new TextView(context);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(params);
this.chooseColor=chooseColor;
this.chooseBgColor=chooseBgColor;
textView.setTextSize(textSize);
if(isBold){
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
}
textView.setTextColor(baseColor);
spannableStringBuilder= new SpannableStringBuilder();
addView(textView);
return this;
}
public MyColorfulText addText(String s){
spannableStringBuilder.append(s);
return this;
}
public MyColorfulText addColorFulText(String s,int id){
spannableStringBuilder.append(s);
MyClickSpan clickSpan=new MyClickSpan(id);
spannableStringBuilder.setSpan(clickSpan,spannableStringBuilder.length()-s.length(),spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return this;
}
public MyColorfulText setOnItemClick(OnItemClick onItemClick){
this.onItemClick=onItemClick;
return this;
}
public void show(){
textView.setText(spannableStringBuilder);
textView.setMovementMethod(LinkMovementMethod.getInstance());//不设置 没有点击事件
textView.setHighlightColor(chooseBgColor);
}
public class MyClickSpan extends ClickableSpan {
private int id;
public MyClickSpan(int id) {
this.id=id;
}
@Override
public void onClick(View widget) {
//下面两句重新赋值一下,避免出现点击后背景色不清除
textView.setText(spannableStringBuilder);
textView.setHighlightColor(chooseBgColor);
//回调点击事件
if(onItemClick!=null){
onItemClick.onClick(id);
}
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(chooseColor);//颜色
ds.setUnderlineText(false); //去掉下划线
}
}
public interface OnItemClick{
void onClick(int id);
}
private OnItemClick onItemClick;
}