本文整理汇总了Java中android.text.Spannable.Factory类的典型用法代码示例。如果您正苦于以下问题:Java Factory类的具体用法?Java Factory怎么用?Java Factory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Factory类属于android.text.Spannable包,在下文中一共展示了Factory类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setText
import android.text.Spannable.Factory; //导入依赖的package包/类
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();
}
示例2: replaceTextsWithImages
import android.text.Spannable.Factory; //导入依赖的package包/类
/**
* 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(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
editText.setText(spannable);
if (oldCursorPosition >= 0) {
editText.setSelection(oldCursorPosition);
}
}