Details about the encapsulation of weibo content
- 2020-05-05 11:51:45
- OfStack
I don't have to go through Html.fromHtml because my data has a little bit of html in it. So it's converted by html.
implementation method:
The specific packaging of is as follows:
Own encapsulation dialog controls: / / www jb51. net article / 32030. htm
implementation method:
TextView content = (TextView) convertView.findViewById(R.id.content);
content.setText(Html.fromHtml("<html><head>"+temp.get(position).getContent()+"</html></head>"));
CharSequence str = content.getText();
SpannableString spann = WeiboUtils.formatContentNoClick(str);
content.setText(spann);
The specific packaging of is as follows:
package com.lizheng.little.yiqu.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.lizheng.little.yiqu.ui.ActWeiBoInfo;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
public class WeiboUtils {
/**
* will text In the @ A person, # A theme, http:// The font of the address is highlighted, and the matching emoji is displayed as an emoji
* @param text
* @param context
* @return*/
public static SpannableString formatContent(CharSequence text,Context context) {
SpannableString spannableString = new SpannableString(text);
/*
* @[^\\s: : ]+[: : \\s] matching @ Sb.
* #([^\\#|.]+)# matching # A theme http://t\\.cn/\\w+ Match the url
*/
Pattern pattern = Pattern.compile("@[^\\s: : ]+[: : \\s]|#([^\\#|.]+)#|http://t\\.cn/\\w");
Matcher matcher = pattern.matcher(spannableString);
final Context mcontext = context;
while (matcher.find()) {
final String match=matcher.group();
if(match.startsWith("@")){ //@ Somebody, highlight the font
spannableString.setSpan(new ClickableSpan()
{
// in onClick Method, you can write actions to perform when a link is clicked
@Override
public void onClick(View widget)
{
String username = match;
username = username.replace("@", "");
username = username.replace(":", "");
username = username.trim();
Intent intent = new Intent(mcontext,XXX.class);
ConstantsUtil.clickName = username;
mcontext.startActivity(intent);// Jump to the user information interface
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("#")){ //# A theme
spannableString.setSpan(new ClickableSpan()
{
// in onClick Method, you can write actions to perform when a link is clicked
@Override
public void onClick(View widget)
{
String theme = match;
theme = theme.replace("#", "");
theme = theme.trim();
ConstantsUtil.clickName = theme;
Intent intent = new Intent(mcontext,XXX.class);
mcontext.startActivity(intent);// Skip to the topic information interface
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("http://")){ // Match the url
spannableString.setSpan(new ClickableSpan()
{
// in onClick Method, you can write actions to perform when a link is clicked
@Override
public void onClick(View widget)
{
Uri uri = Uri.parse(match);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mcontext.startActivity(intent);
}
}, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return spannableString;
}
public static SpannableString formatContentNoClick(CharSequence text) {
SpannableString spannableString = new SpannableString(text);
/*
* @[^\\s: : ]+[: : \\s] matching @ Sb.
* #([^\\#|.]+)# matching # A theme http://t\\.cn/\\w+ Match the url
*/
Pattern pattern = Pattern.compile("@[^\\s: : ]+[: : \\s]|#([^\\#|.]+)#|http://t\\.cn/\\w");
Matcher matcher = pattern.matcher(spannableString);
while (matcher.find()) {
final String match=matcher.group();
if(match.startsWith("@")){ //@ Somebody, highlight the font
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("#")){ //# A theme
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("http://")){ // Match the url
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return spannableString;
}
public static long calculateWeiboLength(CharSequence c) {
double len = 0;
for (int i = 0; i < c.length(); i++) {
int temp = (int)c.charAt(i);
if (temp > 0 && temp < 127) {
len += 0.5;
}else{
len ++;
}
}
return Math.round(len);
}
}
Own encapsulation dialog controls: / / www jb51. net article / 32030. htm