项目:Bimoid-IM-Android
文件:MyTextView.java
public void setText(CharSequence text,boolean detect_links){
this.text = text;
if(detect_links){
Spannable s = null;
if(this.text instanceof Spannable){
s = (Spannable)this.text;
}else{
s = Factory.getInstance().newSpannable(this.text);
}
Linkify.addLinks(s,Linkify.WEB_URLS);
this.text = s;
}
//relayout();
invalidate();
}
项目:Android-Commons
文件:UI.java
/**
* Replaces the given texts with the given image (as a `Spannable`) in the specified `EditText` instance
*
* @param context a context reference
* @param editText the `EditText` instance to operate on
* @param searchTexts the texts to replace
* @param replacementimages the resource IDs of the images to insert
*/
public static void replaceTextsWithImages(final Context context,final EditText editText,final String[] searchTexts,final int[] replacementimages) {
if (searchTexts.length != replacementimages.length) {
throw new RuntimeException("Number of search texts must match the number of replacement images");
}
final int oldCursorPosition = editText.getSelectionStart();
final Factory spannableFactory = Spannable.Factory.getInstance();
final Spannable spannable = spannableFactory.newSpannable(editText.getText().toString());
for (int i = 0; i < searchTexts.length; i++) {
final Pattern pattern = Pattern.compile(Pattern.quote(searchTexts[i]));
final Matcher matcher = pattern.matcher(spannable);
boolean set;
while (matcher.find()) {
set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(),matcher.end(),ImageSpan.class)) {
if (spannable.getSpanStart(span) >= matcher.start() && spannable.getSpanEnd(span) <= matcher.end()) {
spannable.removeSpan(span);
}
else {
set = false;
break;
}
}
if (set) {
spannable.setSpan(new ImageSpan(context,replacementimages[i]),matcher.start(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
editText.setText(spannable);
if (oldCursorPosition >= 0) {
editText.setSelection(oldCursorPosition);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。