有關(guān)微博content的封裝實(shí)現(xiàn)詳解
實(shí)現(xiàn)方法:
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://網(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 < c.length(); i++) {
int temp = (int)c.charAt(i);
if (temp > 0 && temp < 127) {
len += 0.5;
}else{
len ++;
}
}
return Math.round(len);
}
}
自己封裝的dialog控件:http://m.fzitv.net/article/32030.htm
相關(guān)文章
Android apk安裝替換卸載廣播的實(shí)現(xiàn)代碼
以下代碼實(shí)現(xiàn)比較簡(jiǎn)單,根據(jù)接收到的Action來(lái)判斷應(yīng)用程序是安裝 卸載還是被替換成其他版本,需要的朋友可以參考下2013-07-07
詳解Android Libgdx中ScrollPane和Actor事件沖突問(wèn)題的解決辦法
這篇文章主要介紹了詳解Android Libgdx中ScrollPane和Actor事件沖突問(wèn)題的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09
使用Kotlin開(kāi)發(fā)Android應(yīng)用的初體驗(yàn)
本篇文章主要介紹了使用Kotlin開(kāi)發(fā)Android應(yīng)用的初體驗(yàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Android布局之LinearLayout自定義高亮背景的方法
這篇文章主要介紹了Android布局之LinearLayout自定義高亮背景的方法,實(shí)例分析了Android中LinearLayout布局參數(shù)設(shè)置技巧,需要的朋友可以參考下2016-01-01
Android線程中設(shè)置控件的值提示報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Android線程中設(shè)置控件的值提示報(bào)錯(cuò)的解決方法,實(shí)例分析了textview報(bào)錯(cuò)的原因以及Handler設(shè)置來(lái)解決錯(cuò)誤的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06
Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn)
最近做了一個(gè)圖片瀏覽小程序,本文主要介紹了Android簡(jiǎn)易圖片瀏覽器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Android RecyclerView添加頭部和底部的方法
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView添加頭部和底部的方法,感興趣的小伙伴們可以參考一下2016-05-05
Android studio 出現(xiàn)錯(cuò)誤Run with --stacktrace option to get the s
這篇文章主要介紹了 Android studio 出現(xiàn)錯(cuò)誤Run with --stacktrace option to get the stack trace. Run with --info or --debu的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android 照片選擇區(qū)域功能實(shí)現(xiàn)示例
這篇文章主要介紹了Android 照片選擇區(qū)域功能實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java程序員轉(zhuǎn)Android開(kāi)發(fā)必讀經(jīng)驗(yàn)一份
小編最近幾日偷偷的發(fā)現(xiàn)部分Java程序員想轉(zhuǎn)安卓開(kāi)發(fā),故此加緊補(bǔ)充知識(shí),為大家搜集資料,積極整理前人的經(jīng)驗(yàn),希望可以給正處于困惑中的你,帶來(lái)些許的幫助。2017-11-11

