实现部分可点击的TextView
效果类似微信朋友圈里点赞的效果
1.初始化n个朋友
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append("朋友" + i + ",");
}
2.去除最后的逗号
String likeUsers = sb.substring(0, sb.lastIndexOf(",")).toString();
3.设置文本
mT2.setMovementMethod(LinkMovementMethod.getInstance());
mT2.setText(addClickPart(likeUsers), TextView.BufferType.SPANNABLE);
4.addClickPart(String str);
实现方法
设置赞的图片
ImageSpan imgspan = new ImageSpan(MyHtmlActivity.this, R.drawable.like);
SpannableString spanStr = new SpannableString("p");
spanStr.setSpan(imgspan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
5.创建一个SpannableStringBuilder对象,连接多个字符串
SpannableStringBuilder ssb = new SpannableStringBuilder(spanStr);
ssb.append(str);
6.切割字符串,得到”,”分隔的每个内容
String[] likeUsers = str.split(",");
7.给每个内容设置ClickableSpan
for (int i = 0; i < likeUsers.length; i++) {
final String name = likeUsers[i];
final int start = str.indexOf(name) + spanStr.length();
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MyHtmlActivity.this, name,
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
//删除下划线,设置字体颜色
ds.setColor(Color.BLUE);
ds.setUnderlineText(false);
}
},start,start + name.length(),0);
}
包括两个重载方法一个处理点击事件,另一个指定样式
8.最后返回这个SpannableStringBuilder
用于setText
return ssb.append("等" + likeUsers.length + "个人觉得很赞");
本篇和上篇 完整代码:
public class MyHtmlActivity extends AppCompatActivity {
private TextView mTv_html;
private TextView mT2;
private TextView mT1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_html);
mTv_html = (TextView) findViewById(R.id.tv_html);
mT2 = (TextView) findViewById(R.id.txtTwo);
// setHtml1();
// setHtml2();
// setHtml3();
setHtml4();
}
private void setHtml4() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append("朋友" + i + ",");
}
String likeUsers = sb.substring(0, sb.lastIndexOf(",")).toString();
mT2.setMovementMethod(LinkMovementMethod.getInstance());
mT2.setText(addClickPart(likeUsers), TextView.BufferType.SPANNABLE);
}
//定义一个点击每个部分文字的处理方法
private SpannableStringBuilder addClickPart(String str) {
//赞的图标
ImageSpan imgspan = new ImageSpan(MyHtmlActivity.this, R.drawable.like);
SpannableString spanStr = new SpannableString("p");
spanStr.setSpan(imgspan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//创建一个SpannableStringBuilder对象,连接多个字符串
SpannableStringBuilder ssb = new SpannableStringBuilder(spanStr);
ssb.append(str);
String[] likeUsers = str.split(",");
if (likeUsers.length > 0) {
for (int i = 0; i < likeUsers.length; i++) {
final String name = likeUsers[i];
final int start = str.indexOf(name) + spanStr.length();
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(MyHtmlActivity.this, name,
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
//删除下划线,设置字体颜色
ds.setColor(Color.BLUE);
ds.setUnderlineText(false);
}
},start,start + name.length(),0);
}
}
return ssb.append("等" + likeUsers.length + "个人觉得很赞");
}
private void setHtml3() {
mT1 = (TextView) findViewById(R.id.txtOne);
mT2 = (TextView) findViewById(R.id.txtTwo);
SpannableString span = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");
//1.设置背景色,setSpan时需要指定的flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)
span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//2.用超链接标记文本
span.setSpan(new URLSpan("tel:4155551212"), 2, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//3.用样式标记文本(斜体)
span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//4.用删除线标记文本
span.setSpan(new StrikethroughSpan(), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//5.用下划线标记文本
span.setSpan(new UnderlineSpan(), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//6.用颜色标记
span.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//7.//获取Drawable资源
Drawable d = getResources().getDrawable(R.drawable.iv_bg);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//8.创建ImageSpan,然后用ImageSpan来替换文本
ImageSpan imgspan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
span.setSpan(imgspan, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
mT1.setText(span);
}
private void setHtml2() {
String s = "<img src='iv_bg'/></br>";
Spanned spanned = Html.fromHtml(s, new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
try {
Field field = R.drawable.class.getField(source);
String stringId = field.get(null).toString();
int resourceId = Integer.parseInt(stringId);
drawable = getResources().getDrawable(resourceId);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return drawable;
}
}, null);
mTv_html.setMovementMethod(LinkMovementMethod.getInstance());
mTv_html.setText(spanned);
}
private void setHtml1() {
String s = "<font color='blue'><b> 百度一下你就知道</b></font><br> ";
s += "<a href = 'http://www.baidu.com'> 百度</a>";
Spanned spanned = Html.fromHtml(s);
mTv_html.setMovementMethod(LinkMovementMethod.getInstance());
mTv_html.setText(spanned);
}
}