本文整理汇总了Java中android.text.Spannable.getSpans方法的典型用法代码示例。如果您正苦于以下问题:Java Spannable.getSpans方法的具体用法?Java Spannable.getSpans怎么用?Java Spannable.getSpans使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Spannable
的用法示例。
在下文中一共展示了Spannable.getSpans方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: spannableToJson
import android.text.Spannable; //导入方法依赖的package包/类
public static JsonObject spannableToJson(CharSequence text) {
JsonObject ret = new JsonObject();
ret.addProperty("text", text.toString());
JsonArray a = new JsonArray();
if (text instanceof Spannable) {
Spannable spannable = (Spannable) text;
for (Object span : spannable.getSpans(0, text.length(), Object.class)) {
JsonObject o = spanToJson(span);
if (o != null) {
o.addProperty("start", spannable.getSpanStart(span));
o.addProperty("end", spannable.getSpanEnd(span));
o.addProperty("flags", spannable.getSpanFlags(span));
a.add(o);
}
}
}
ret.add("spans", a);
return ret;
}
示例2: onTouchEvent
import android.text.Spannable; //导入方法依赖的package包/类
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() == 1) {
this.mLinkClicked = false;
this.mClickedLink = null;
int x = (((int) event.getX()) - widget.getTotalPaddingLeft()) + widget.getScrollX();
int y = (((int) event.getY()) - widget.getTotalPaddingTop()) + widget.getScrollY();
Layout layout = widget.getLayout();
int off = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
ClickableSpan[] link = (ClickableSpan[]) buffer.getSpans(off, off, ClickableSpan
.class);
if (link.length != 0) {
SensibleUrlSpan span = link[0];
this.mLinkClicked = span.onClickSpan(widget);
this.mClickedLink = span.getURL();
return this.mLinkClicked;
}
}
super.onTouchEvent(widget, buffer, event);
return false;
}
示例3: onTouchEvent
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) {
link[0].onClick(widget);
return true;
} else {
Selection.removeSelection(buffer);
}
}
return super.onTouchEvent(widget, buffer, event);
}
示例4: stripUnderlines
import android.text.Spannable; //导入方法依赖的package包/类
public static void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
示例5: clearSpan
import android.text.Spannable; //导入方法依赖的package包/类
private void clearSpan(Spannable spannable, int start, int end) {
if (start == end) {
return;
}
EmoticonSpan[] oldSpans = spannable.getSpans(start, end, EmoticonSpan.class);
for (int i = 0; i < oldSpans.length; i++) {
spannable.removeSpan(oldSpans[i]);
}
}
示例6: getQueryRange
import android.text.Spannable; //导入方法依赖的package包/类
/**
* Returns the current query out of the given Spannable.
* @param text the anchor text
* @return an int[] with query start and query end positions
*/
@Nullable
public static int[] getQueryRange(Spannable text) {
QuerySpan[] span = text.getSpans(0, text.length(), QuerySpan.class);
if (span == null || span.length == 0) return null;
if (span.length > 1) {
// Won't happen
log("getQueryRange: ERR: MORE THAN ONE QuerySpan.");
}
QuerySpan sp = span[0];
return new int[]{text.getSpanStart(sp), text.getSpanEnd(sp)};
}
示例7: addSmiles
import android.text.Spannable; //导入方法依赖的package包/类
/**
* replace existing spannable with smiles
* @param context
* @param spannable
* @return
*/
public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Entry<Pattern, Object> entry : emoticons.entrySet()) {
Matcher matcher = entry.getKey().matcher(spannable);
while (matcher.find()) {
boolean 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) {
hasChanges = true;
Object value = entry.getValue();
if(value instanceof String && !((String) value).startsWith("http")){
File file = new File((String) value);
if(!file.exists() || file.isDirectory()){
return false;
}
spannable.setSpan(new ImageSpan(context, Uri.fromFile(file)),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
spannable.setSpan(new ImageSpan(context, (Integer)value),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
return hasChanges;
}
示例8: possiblyUpdateInlineImageSpans
import android.text.Spannable; //导入方法依赖的package包/类
/**
* For TextInlineImageSpan we need to update the Span to know that the window is attached and
* the TextView that we will set as the callback on the Drawable.
*
* @param spannable The spannable that may contain TextInlineImageSpans
* @param view The view which will be set as the callback for the Drawable
*/
public static void possiblyUpdateInlineImageSpans(Spannable spannable, TextView view) {
TextInlineImageSpan[] spans =
spannable.getSpans(0, spannable.length(), TextInlineImageSpan.class);
for (TextInlineImageSpan span : spans) {
span.onAttachedToWindow();
span.setTextView(view);
}
}
示例9: removeUrlHighlightColor
import android.text.Spannable; //导入方法依赖的package包/类
private void removeUrlHighlightColor(TextView textView) {
if (this.isUrlHighlighted) {
this.isUrlHighlighted = false;
Spannable text = (Spannable) textView.getText();
BackgroundColorSpan[] highlightSpans = text.getSpans(0, text.length(), BackgroundColorSpan.class);
for (BackgroundColorSpan highlightSpan : highlightSpans) {
text.removeSpan(highlightSpan);
}
try {
textView.setText(text);
Selection.removeSelection(text);
} catch (Exception ignored) {}
}
}
示例10: addCustomTabUrlHandler
import android.text.Spannable; //导入方法依赖的package包/类
private static void addCustomTabUrlHandler(Activity activity, TextView textView) {
CharSequence text = textView.getText();
Spannable s = new SpannableString(text);
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new CustomTabUrlSpan(activity, span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
示例11: onTouchEvent
import android.text.Spannable; //导入方法依赖的package包/类
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
int action = event.getAction();
if (action == 1 || action == 0) {
int x = (((int) event.getX()) - widget.getTotalPaddingLeft()) + widget.getScrollX();
int y = (((int) event.getY()) - widget.getTotalPaddingTop()) + widget.getScrollY();
Layout layout = widget.getLayout();
int off = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
MyURLSpan[] link = (MyURLSpan[]) buffer.getSpans(off, off, MyURLSpan.class);
if (link.length != 0) {
if (action == 1) {
if (!this.mHasPerformedLongPress) {
link[0].onClick(widget);
}
this.pressed = false;
this.lastEvent = new float[2];
} else if (action == 0) {
this.pressed = true;
this.lastEvent[0] = event.getX();
this.lastEvent[1] = event.getY();
checkForLongClick(link, widget);
}
return true;
}
} else if (action == 2) {
float[] position = new float[]{event.getX(), event.getY()};
if (Math.sqrt(Math.hypot((double) Math.abs(this.lastEvent[0] - position[0]), (double)
Math.abs(this.lastEvent[1] - position[1]))) > ((double) 6)) {
this.pressed = false;
}
} else if (action == 3) {
this.pressed = false;
this.lastEvent = new float[2];
} else {
this.pressed = false;
this.lastEvent = new float[2];
}
return super.onTouchEvent(widget, buffer, event);
}
示例12: action
import android.text.Spannable; //导入方法依赖的package包/类
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = (widget.getHeight() + areatop) - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
MyURLSpan[] candidates = (MyURLSpan[]) buffer.getSpans(first, last, MyURLSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0 && buffer.getSpanStart(FROM_BELOW) >= 0) {
selEnd = buffer.length();
selStart = selEnd;
}
if (selStart > last) {
selEnd = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
selStart = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
}
if (selEnd < first) {
selEnd = -1;
selStart = -1;
}
int beststart;
int bestend;
int i;
switch (what) {
case 1:
if (selStart != selEnd) {
MyURLSpan[] link = (MyURLSpan[]) buffer.getSpans(selStart, selEnd, MyURLSpan
.class);
if (link.length == 1) {
link[0].onClick(widget);
break;
}
return false;
}
return false;
case 2:
beststart = -1;
bestend = -1;
for (i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if ((end < selEnd || selStart == selEnd) && end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case 3:
beststart = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
bestend = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
for (i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if ((start > selStart || selStart == selEnd) && start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}
示例13: onTouchEvent
import android.text.Spannable; //导入方法依赖的package包/类
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
mClickLinks = buffer.getSpans(off, off, ClickableSpan.class);
if(mClickLinks.length > 0){
// 点击的是Span区域,不要把点击事件传递
setPassToTv(false);
Selection.setSelection(buffer,
buffer.getSpanStart(mClickLinks[0]),
buffer.getSpanEnd(mClickLinks[0]));
//设置点击区域的背景色
mBgSpan = new BackgroundColorSpan(clickableSpanBgClor);
buffer.setSpan(mBgSpan,
buffer.getSpanStart(mClickLinks[0]),
buffer.getSpanEnd(mClickLinks[0]),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
setPassToTv(true);
// textview选中效果
widget.setBackgroundColor(textViewBgColor);
}
}else if(action == MotionEvent.ACTION_UP){
if(mClickLinks.length > 0){
mClickLinks[0].onClick(widget);
if(mBgSpan != null){//移除点击时设置的背景span
buffer.removeSpan(mBgSpan);
}
}else{
}
Selection.removeSelection(buffer);
widget.setBackgroundColor(Color.TRANSPARENT);
}else if(action == MotionEvent.ACTION_MOVE){
//这种情况不用做处理
}else{
if(mBgSpan != null){//移除点击时设置的背景span
buffer.removeSpan(mBgSpan);
}
widget.setBackgroundColor(Color.TRANSPARENT);
}
return Touch.onTouchEvent(widget, buffer, event);
}
示例14: addLinks
import android.text.Spannable; //导入方法依赖的package包/类
/**
* Scans the text of the provided Spannable and turns all occurrences
* of the link types indicated in the mask into clickable links.
* If the mask is nonzero, it also removes any existing URLSpans
* attached to the Spannable, to avoid problems if you call it
* repeatedly on the same text.
*/
public static final boolean addLinks(Spannable text, int mask, ColorStateList linkColor, ColorStateList bgColor, QMUIOnSpanClickListener l) {
if (mask == 0) {
return false;
}
URLSpan[] old = text.getSpans(0, text.length(), URLSpan.class);
for (int i = old.length - 1; i >= 0; i--) {
text.removeSpan(old[i]);
}
ArrayList<LinkSpec> links = new ArrayList<>();
if ((mask & WEB_URLS) != 0) {
gatherLinks(links, text, Patterns.WEB_URL,
new String[]{"http://", "https://", "rtsp://"},
sUrlMatchFilter, null);
}
if ((mask & EMAIL_ADDRESSES) != 0) {
gatherLinks(links, text, Patterns.EMAIL_ADDRESS,
new String[]{"mailto:"},
null, null);
}
if ((mask & PHONE_NUMBERS) != 0) {
gatherPhoneLinks(links, text, WECHAT_PHONE, new Pattern[]{NOT_PHONE},
new String[]{"tel:"}, sPhoneNumberMatchFilter, sPhoneNumberTransformFilter);
}
if ((mask & MAP_ADDRESSES) != 0) {
gatherMapLinks(links, text);
}
pruneOverlaps(links);
if (links.size() == 0) {
return false;
}
for (LinkSpec link : links) {
applyLink(link.url, link.start, link.end, text, linkColor, bgColor, l);
}
return true;
}
示例15: onTouchEvent
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = buffer.getSpans(off, off,
ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - lastClickTime < CLICK_DELAY) {
link[0].onClick(widget);
}
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
lastClickTime = System.currentTimeMillis();
}
if (widget instanceof UILabel) {
((UILabel) widget).linkHit = true;
}
return true;
} else {
Selection.removeSelection(buffer);
}
}
return Touch.onTouchEvent(widget, buffer, event);
}