为你展示微博content的封装和实现方法的代码展示

来源:爱站网时间:2018-10-08编辑:网友分享
在Android开发过程中,网页代码的实现方法是多种多样的,而当普通代码不能满足我们的要求时,通常需要定制另外一个代码来实现,下面是爱站技术频道小编为大家介绍的为你展示微博content的封装和实现方法的代码展示,一起进入下文了解一下吧!

an style="color:#002FD9">HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

在Android开发过程中,网页代码的实现方法是多种多样的,而当普通代码不能满足我们的要求时,通常需要定制另外一个代码来实现,下面是爱站技术频道小编为大家介绍的为你展示微博content的封装和实现方法的代码展示,一起进入下文了解一下吧!

可以不用经过 Html.fromHtml 因为我的数据里面含有一点 html的标签。所以经过html转换了。
实现方法:


TextView content = (TextView) convertView.findViewById(R.id.content);
content.setText(Html.fromHtml(""+temp.get(position).getContent()+""));
CharSequence str = content.getText();
SpannableString spann = WeiboUtils.formatContentNoClick(str);
content.setText(spann);


具体的封装如下:

 

 


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 {
/**
* 将text中@某人、#某主题、http://网址的字体加亮,匹配的表情文字以表情显示
* @param text
* @param context
* @return*/
public static SpannableString formatContent(CharSequence text,Context context) {
SpannableString spannableString = new SpannableString(text);
/*
* @[^\\s::]+[::\\s] 匹配@某人
* #([^\\#|.]+)# 匹配#某主题 http://t\\.cn/\\w+ 匹配网址
*/
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("@")){ //@某人,加亮字体
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@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);//跳转到用户信息界面
}
}, 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("#")){ //#某主题
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@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);//跳转到话题信息界面
}
}, 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://")){ //匹配网址
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以编写单击链接时要执行的动作
@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] 匹配@某人
* #([^\\#|.]+)# 匹配#某主题 http://t\\.cn/\\w+ 匹配网址
*/
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("@")){ //@某人,加亮字体
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("#")){ //#某主题
spannableString.setSpan(new ForegroundColorSpan(0xff0077ff),
matcher.start(), matcher.end(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if(match.startsWith("http://")){ //匹配网址
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 int temp = (int)c.charAt(i);
if (temp > 0 && temp len += 0.5;
}else{
len ++;
}
}
return Math.round(len);
}
}


自己封装的dialog控件:http://www.aizhan.com/article/32030.htm

上文是关于为你展示微博content的封装和实现方法的代码展示的介绍,相信大家都有了一定的了解,想要了解更多的技术信息,请继续关注爱站技术频道吧!

上一篇:android开发时Activity中传递变量的参数

下一篇:android 9PNG图片制作教程

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载