国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

為你展示微博content的封裝和實(shí)現(xiàn)方法的代碼展示

2020-02-21 17:37:37
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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開(kāi)發(fā)過(guò)程中,網(wǎng)頁(yè)代碼的實(shí)現(xiàn)方法是多種多樣的,而當(dāng)普通代碼不能滿足我們的要求時(shí),通常需要定制另外一個(gè)代碼來(lái)實(shí)現(xiàn),下面是武林技術(shù)頻道小編為大家介紹的為你展示微博content的封裝和實(shí)現(xiàn)方法的代碼展示,一起進(jìn)入下文了解一下吧!

可以不用經(jīng)過(guò) Html.fromHtml 因?yàn)槲业臄?shù)據(jù)里面含有一點(diǎn) html的標(biāo)簽。所以經(jīng)過(guò)html轉(zhuǎn)換了。
實(shí)現(xiàn)方法:


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://網(wǎng)址的字體加亮,匹配的表情文字以表情顯示
* @param text
* @param context
* @return*/
public static SpannableString formatContent(CharSequence text,Context context) {
SpannableString spannableString = new SpannableString(text);
/*
* @[^//s::]+[:://s] 匹配@某人
* #([^//#|.]+)# 匹配#某主題 http://t//.cn///w+ 匹配網(wǎng)址
*/
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方法中可以編寫(xiě)單擊鏈接時(shí)要執(zhí)行的動(dòng)作
@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);//跳轉(zhuǎn)到用戶信息界面
}
}, 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方法中可以編寫(xiě)單擊鏈接時(shí)要執(zhí)行的動(dòng)作
@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);//跳轉(zhuǎn)到話題信息界面
}
}, 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://")){ //匹配網(wǎng)址
spannableString.setSpan(new ClickableSpan()
{
// 在onClick方法中可以編寫(xiě)單擊鏈接時(shí)要執(zhí)行的動(dòng)作
@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+ 匹配網(wǎng)址
*/
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://")){ //匹配網(wǎng)址
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://m.survivalescaperooms.com/article/32030.htm

上文是關(guān)于為你展示微博content的封裝和實(shí)現(xiàn)方法的代碼展示的介紹,相信大家都有了一定的了解,想要了解更多的技術(shù)信息,請(qǐng)繼續(xù)關(guān)注武林技術(shù)頻道吧!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清新县| SHOW| 英德市| 南昌市| 龙泉市| 新宁县| 兰考县| 毕节市| 井陉县| 县级市| 渭源县| 饶河县| 灯塔市| 淄博市| 大名县| 广汉市| 宁武县| 永年县| 汨罗市| 秭归县| 鸡泽县| 巴彦县| 赫章县| 瓮安县| 萨嘎县| 务川| 新绛县| 洮南市| 陆川县| 邛崃市| 义马市| 枞阳县| 江油市| 霸州市| 历史| 五原县| 枣强县| 永靖县| 岳阳县| 霍邱县| 建平县|