Skip to content

Commit

Permalink
替换系统级别的URL匹配正则
Browse files Browse the repository at this point in the history
  • Loading branch information
MZCretin committed Aug 29, 2018
1 parent 95b431f commit c1e179e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {

setTips();

String yourText = " 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。";
String yourText = " hello...world 我所认识的中国,强大、友好。@奥特曼 “一带一路”经济带带动了沿线国家的经济发展,促进我国与他国的友好往来和贸易发展,可谓“双赢”。http://www.baidu.com 自古以来,中国以和平、友好的面孔示人。汉武帝派张骞出使西域,开辟丝绸之路,增进与西域各国的友好往来。http://www.baidu.com 胡麻、胡豆、香料等食材也随之传入中国,汇集于中华美食。@RNG 漠漠古道,驼铃阵阵,这条路奠定了“一带一路”的基础,让世界认识了中国。";
views[0].setContent(yourText);
views[0].setLinkClickListener(linkClickListener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -33,6 +34,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.support.v4.util.PatternsCompat.AUTOLINK_WEB_URL;

/**
* @date: on 2018-08-24
* @author: cretin
Expand All @@ -47,8 +50,12 @@ public class ExpandableTextView extends AppCompatTextView {
public static final String TEXT_TARGET = "网页链接";
public static final String IMAGE_TARGET = "图";
public static final String TARGET = IMAGE_TARGET + TEXT_TARGET;
/**
* http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?
*/

public static final String regexp = "((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)|[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
// public static final String regexp = "((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)|[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
public static final String regexp = "http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?";

public static final String regexp_mention = "@[\\w\\p{InCJKUnifiedIdeographs}-]{1,26}";

Expand Down Expand Up @@ -144,6 +151,8 @@ public ExpandableTextView(Context context, @Nullable AttributeSet attrs, int def
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
setMovementMethod(LocalLinkMovementMethod.getInstance());

Linkify.addLinks(this, Linkify.WEB_URLS);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
Expand Down Expand Up @@ -188,7 +197,7 @@ private SpannableStringBuilder setRealContent(CharSequence content) {
mFormatData = formatData(content);
//用来计算内容的大小
mDynamicLayout =
new DynamicLayout(mFormatData.formatedContent, mPaint, mWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 0.0f,
new DynamicLayout(mFormatData.formatedContent, mPaint, mWidth, Layout.Alignment.ALIGN_NORMAL, 1.2f, 0.0f,
true);
//获取行数
mLineCount = mDynamicLayout.getLineCount();
Expand Down Expand Up @@ -342,53 +351,40 @@ public void updateDrawState(TextPaint ds) {
for (FormatData.PositionData data : positionDatas) {
if (ssb.length() >= data.getEnd()) {
if (data.getType().equals(LinkType.LINK_TYPE)) {
int fitPosition = ssb.length() - getHideEndContent().length();
if (data.getStart() < fitPosition) {
if (mNeedExpend && ignoreMore) {
int fitPosition = ssb.length() - getHideEndContent().length();
if (data.getStart() < fitPosition) {
SelfImageSpan imageSpan = new SelfImageSpan(mLinkDrawable, ImageSpan.ALIGN_BASELINE);
//设置链接图标
ssb.setSpan(imageSpan, data.getStart(), data.getStart() + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//设置链接文字样式
int endPosition = data.getEnd();
if (fitPosition > data.getStart() + 1 && fitPosition < data.getEnd()) {
endPosition = fitPosition;
}
if (data.getStart() + 1 < fitPosition) {
addUrl(ssb, data, endPosition);
}
}
} else {
SelfImageSpan imageSpan = new SelfImageSpan(mLinkDrawable, ImageSpan.ALIGN_BASELINE);
//设置链接图标
ssb.setSpan(imageSpan, data.getStart(), data.getStart() + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
//设置链接文字样式
int endPosition = data.getEnd();
if (fitPosition > data.getStart() + 1 && fitPosition < data.getEnd()) {
endPosition = fitPosition;
}
if (data.getStart() + 1 < fitPosition) {
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
if (linkClickListener != null)
linkClickListener.onLinkClickListener(LinkType.LINK_TYPE, data.getUrl());
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(mLinkTextColor);
ds.setUnderlineText(false);
}
}, data.getStart() + 1, endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
addUrl(ssb, data, data.getEnd());
}
} else {
int fitPosition = ssb.length() - getHideEndContent().length();
if (data.getStart() < fitPosition) {
int endPosition = data.getEnd();
if (fitPosition < data.getEnd()) {
endPosition = fitPosition;
}
//关注
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
if (linkClickListener != null)
linkClickListener.onLinkClickListener(LinkType.MENTION_TYPE, data.getUrl());
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(mLinkTextColor);
ds.setUnderlineText(false);
//如果需要展开
if (mNeedExpend && ignoreMore) {
int fitPosition = ssb.length() - getHideEndContent().length();
if (data.getStart() < fitPosition) {
int endPosition = data.getEnd();
if (fitPosition < data.getEnd()) {
endPosition = fitPosition;
}
}, data.getStart(), endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
addMention(ssb, data, endPosition);
}
} else {
addMention(ssb, data, data.getEnd());
}
}
}
Expand All @@ -400,6 +396,53 @@ public void updateDrawState(TextPaint ds) {
return ssb;
}


/**
* 添加@用户的Span
*
* @param ssb
* @param data
* @param endPosition
*/
private void addMention(SpannableStringBuilder ssb, FormatData.PositionData data, int endPosition) {
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
if (linkClickListener != null)
linkClickListener.onLinkClickListener(LinkType.MENTION_TYPE, data.getUrl());
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(mLinkTextColor);
ds.setUnderlineText(false);
}
}, data.getStart(), endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}

/**
* 添加链接的span
*
* @param ssb
* @param data
* @param endPosition
*/
private void addUrl(SpannableStringBuilder ssb, FormatData.PositionData data, int endPosition) {
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
if (linkClickListener != null)
linkClickListener.onLinkClickListener(LinkType.LINK_TYPE, data.getUrl());
}

@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(mLinkTextColor);
ds.setUnderlineText(false);
}
}, data.getStart() + 1, endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}

/**
* 执行展开和收回的动作
*/
Expand Down Expand Up @@ -467,7 +510,8 @@ private FormatData formatData(CharSequence content) {
FormatData formatData = new FormatData();
List<FormatData.PositionData> datas = new ArrayList<>();
//对链接进行正则匹配
Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
// Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
Pattern pattern = AUTOLINK_WEB_URL;
Matcher matcher = pattern.matcher(content);
StringBuffer newResult = new StringBuffer();
int start = 0;
Expand Down

0 comments on commit c1e179e

Please sign in to comment.