웨 이 보 콘 텐 츠 의 패키지 에 대한 상세 한 설명

Html.from Html 을 거치 지 않 아 도 됩 니 다.제 데이터 에 html 라벨 이 조금 들 어 있 기 때 문 입 니 다.그래서 html 변환 을 거 쳤 습 니 다.실현 방법:
 
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);
구체 적 인 패 키 징 은 다음 과 같다.
 
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 < c.length(); i++) {
int temp = (int)c.charAt(i);
if (temp > 0 && temp < 127) {
len += 0.5;
}else{
len ++;
}
}
return Math.round(len);
}
}
자신 이 패 키 징 한 dialog 컨트롤:https://www.jb51.net/article/32030.htm

좋은 웹페이지 즐겨찾기