通过正则表达式,为指定文本添加点击操作和定义样式。

通过正则表达式,为指定文本添加点击操作和定义样式。

packagecom.example.haozai.myapplication;

importandroid.graphics.Color;

importandroid.support.v7.app.AppCompatActivity;

importandroid.os.Bundle;

importandroid.text.SpannableString;

importandroid.text.Spanned;

importandroid.text.TextPaint;

importandroid.text.method.LinkMovementMethod;

importandroid.text.style.ClickableSpan;

importandroid.text.style.ForegroundColorSpan;

importandroid.view.View;

importandroid.widget.TextView;

importandroid.widget.Toast;

importjava.util.ArrayList;

importjava.util.List;

importjava.util.regex.Matcher;

importjava.util.regex.Pattern;

importorg.jsoup.Jsoup;

importorg.jsoup.nodes.Document;

importorg.jsoup.select.Elements;

public classMainActivityextendsAppCompatActivity {

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

String text ="Hi~\n"+"\n"+

"我这是测试例子~\n"+"\n"+

"巴啦啦(新增):\n"+"\n"

+"更多查看  藏宝箱。";

TextView tv= (TextView)findViewById(R.id.textview);

tv.setMovementMethod(LinkMovementMethod.getInstance());

tv.setText(getStyle(text));

}

privateSpannableString getStyle(finalString content){

String result="";

//是完全通配的意思,\s是指空白,包括空格、换行、tab缩进等所有的空白,而\S刚好相反

//这样一正一反下来,就表示所有的字符,完全的,一字不漏的。

String pattern="[\\s\\S]*?<\\/a>";

Pattern p=Pattern.compile(pattern);

Matcher m=p.matcher(content);

List htmlDatas=newArrayList<>();

intstart=0;

while(m.find()){

String group=m.group();

result+=content.substring(start, m.start());

Document document = Jsoup.parse(group);

Elements elements=document.select("a");

String text=elements.text();

String type=elements.attr("type");

String id=elements.attr("id");

String name=elements.attr("name");

String icon=elements.attr("icon");

String subType=elements.attr("sub_type");

String url=elements.attr("url");

HtmlData data=newHtmlData();

data.setType(type);

data.setId(id);

data.setName(name);

data.setIcon(icon);

data.setSubType(subType);

data.setUrl(url);

data.start=result.length();

result+=text;

data.end=result.length();

htmlDatas.add(data);

start=m.end();

}

result+=content.substring(start,content.length());

SpannableString msp=newSpannableString(result);

intsize=htmlDatas.size();

for(inti=0;i

finalHtmlData data=htmlDatas.get(i);

msp.setSpan(getSpan(data), data.start, data.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

msp.setSpan(newForegroundColorSpan(Color.parseColor("#2e9fff")), data.start, data.end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}

returnmsp;

}

privateClickableSpan getSpan(finalHtmlData data){

ClickableSpan span=newClickableSpan() {

@Override

public voidupdateDrawState(TextPaint ds) {

super.updateDrawState(ds);

ds.setUnderlineText(true);

}

@Override

public voidonClick(View widget) {

Toast.makeText(MainActivity.this,"type :"+data.getType()+"  name :"+data.getName(),Toast.LENGTH_SHORT).show();

}

};

returnspan;

}

public static classHtmlData{

privateStringtype;

privateStringsubType;

privateStringid;

privateStringname;

privateStringicon;

private intstart;

private intend;

privateStringurl;

publicString getType() {

returntype;

}

public voidsetType(String type) {

this.type= type;

}

publicString getSubType() {

returnsubType;

}

public voidsetSubType(String subType) {

this.subType= subType;

}

publicString getId() {

returnid;

}

public voidsetId(String id) {

this.id= id;

}

publicString getName() {

returnname;

}

public voidsetName(String name) {

this.name= name;

}

publicString getIcon() {

returnicon;

}

public voidsetIcon(String icon) {

this.icon= icon;

}

public intgetStart() {

returnstart;

}

public voidsetStart(intstart) {

this.start= start;

}

public intgetEnd() {

returnend;

}

public voidsetEnd(intend) {

this.end= end;

}

publicString getUrl() {

returnurl;

}

public voidsetUrl(String url) {

this.url= url;

}

}

}

你可能感兴趣的:(通过正则表达式,为指定文本添加点击操作和定义样式。)