本文整理汇总了Java中android.text.Spanned.getSpanEnd方法的典型用法代码示例。如果您正苦于以下问题:Java Spanned.getSpanEnd方法的具体用法?Java Spanned.getSpanEnd怎么用?Java Spanned.getSpanEnd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Spanned
的用法示例。
在下文中一共展示了Spanned.getSpanEnd方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: linkifyHtml
import android.text.Spanned; //导入方法依赖的package包/类
/**
* Taken from Stack Overflow - https://stackoverflow.com/a/17201376/6052295
* Adds links to a HTML string
*
* @param html the HTML string to add links to
* @param linkifyMask the link type
* @return The spannable text with clickable links
*/
public static Spannable linkifyHtml(String html, int linkifyMask) {
Spanned text = fromHtml(fromHtml(html).toString());
URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
SpannableString buffer = new SpannableString(text);
Linkify.addLinks(buffer, linkifyMask);
for (URLSpan span : currentSpans) {
int end = text.getSpanEnd(span);
int start = text.getSpanStart(span);
buffer.setSpan(span, start, end, 0);
}
return buffer;
}
示例2: recursiveGetSizeWithReplacementSpan
import android.text.Spanned; //导入方法依赖的package包/类
/**
* Do not support cross Span.
*
* @param text text
* @param parentSpan parentSpan
* @param start start index of parentSpan
* @param end end index of parentSpan
* @param paint TextPaint
* @return recursive calculated width
*/
public int recursiveGetSizeWithReplacementSpan(CharSequence text, ReplacementSpan parentSpan, @IntRange(from = 0) int start, @IntRange(from = 0) int end, Paint paint) {
if (text instanceof Spanned) {
Spanned spannedText = (Spanned) text;
List<ReplacementSpan> spans = getSortedReplacementSpans(spannedText, start, end);
if (!spans.isEmpty()) {
int lastIndexCursor = 0;
int width = 0;
for (ReplacementSpan span : spans) {
if (span == parentSpan) {
continue;
}
int spanStart = spannedText.getSpanStart(span);
int spanEnd = spannedText.getSpanEnd(span);
width += parentSpan.getSize(paint, text, lastIndexCursor, spanStart, null);
width += span.getSize(paint, text, spanStart, spanEnd, null);
lastIndexCursor = spanEnd;
}
if (lastIndexCursor < end) {
width += parentSpan.getSize(paint, text, lastIndexCursor, end, null);
}
return width;
}
}
return parentSpan.getSize(paint, text, start, end, null);
}
示例3: copyNonParagraphSuggestionSpansFrom
import android.text.Spanned; //导入方法依赖的package包/类
/**
* Copies the spans from the region <code>start...end</code> in
* <code>source</code> to the region
* <code>destoff...destoff+end-start</code> in <code>dest</code>.
* Spans in <code>source</code> that begin before <code>start</code>
* or end after <code>end</code> but overlap this range are trimmed
* as if they began at <code>start</code> or ended at <code>end</code>.
* Only SuggestionSpans that don't have the SPAN_PARAGRAPH span are copied.
*
* This code is almost entirely taken from {@link TextUtils#copySpansFrom}, except for the
* kind of span that is copied.
*
* @throws IndexOutOfBoundsException if any of the copied spans
* are out of range in <code>dest</code>.
*/
public static void copyNonParagraphSuggestionSpansFrom(Spanned source, int start, int end,
Spannable dest, int destoff) {
Object[] spans = source.getSpans(start, end, SuggestionSpan.class);
for (int i = 0; i < spans.length; i++) {
int fl = source.getSpanFlags(spans[i]);
// We don't care about the PARAGRAPH flag in LatinIME code. However, if this flag
// is set, Spannable#setSpan will throw an exception unless the span is on the edge
// of a word. But the spans have been split into two by the getText{Before,After}Cursor
// methods, so after concatenation they may end in the middle of a word.
// Since we don't use them, we can just remove them and avoid crashing.
fl &= ~Spanned.SPAN_PARAGRAPH;
int st = source.getSpanStart(spans[i]);
int en = source.getSpanEnd(spans[i]);
if (st < start)
st = start;
if (en > end)
en = end;
dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
fl);
}
}
示例4: onClick
import android.text.Spanned; //导入方法依赖的package包/类
@Override
public void onClick(View widget) {
CharSequence text = ((TextView) widget).getText();
Spanned s = (Spanned) text;
int start = s.getSpanStart(this);
int end = s.getSpanEnd(this);
mOnHashTagClickListener.onHashTagClicked(text.subSequence(start, end).toString());
}
示例5: getSpanLength
import android.text.Spanned; //导入方法依赖的package包/类
private static int getSpanLength(Spanned sp, int start, int end, Context context) {
// TODO: there's a situation where the span can lose its annotations:
// - add an auto-complete contact
// - add another auto-complete contact
// - delete that second contact and keep deleting into the first
// - we lose the annotation and can no longer get the span.
// Need to fix this case because it breaks auto-complete contacts with commas in the name.
Annotation[] a = sp.getSpans(start, end, Annotation.class);
if (a.length > 0) {
return sp.getSpanEnd(a[0]);
}
return 0;
}
示例6: onClick
import android.text.Spanned; //导入方法依赖的package包/类
@Override
public void onClick(View textView) {
TextView tv = (TextView) textView;
Spanned s = (Spanned) tv.getText();
int start = s.getSpanStart(this);
int end = s.getSpanEnd(this);
clickListener.onClick(type, s.subSequence(start, end).toString().trim());
}
示例7: ofSpan
import android.text.Spanned; //导入方法依赖的package包/类
static BetterLinkMovementExtended.ClickableSpanWithText ofSpan(TextView textView, ClickableSpan span) {
Spanned s = (Spanned) textView.getText();
String text;
if (span instanceof URLSpan) {
text = ((URLSpan) span).getURL();
} else {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
text = s.subSequence(start, end).toString();
}
return new BetterLinkMovementExtended.ClickableSpanWithText(span, text);
}
示例8: onTouchEvent
import android.text.Spanned; //导入方法依赖的package包/类
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
//http://stackoverflow.com/questions/1697084/handle-textview-link-click-in-my-android-app
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
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);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length > 0) {
URLSpan span = link[0];
String url = span.getURL();
Spanned s = (Spanned) widget.getText();
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
//http://stackoverflow.com/a/19750721/895797
String title = null;
if (start != -1 && end != -1) {
CharSequence wordThatWasClicked = s.subSequence(start, end);
title = wordThatWasClicked.toString();
}
onLinkClicked(widget, url, title);
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
示例9: reactTagForTouch
import android.text.Spanned; //导入方法依赖的package包/类
@Override
public int reactTagForTouch(float touchX, float touchY) {
Spanned text = (Spanned) getText();
int target = getId();
int x = (int) touchX;
int y = (int) touchY;
Layout layout = getLayout();
if (layout == null) {
// If the layout is null, the view hasn't been properly laid out yet. Therefore, we can't find
// the exact text tag that has been touched, and the correct tag to return is the default one.
return target;
}
int line = layout.getLineForVertical(y);
int lineStartX = (int) layout.getLineLeft(line);
int lineEndX = (int) layout.getLineRight(line);
// TODO(5966918): Consider extending touchable area for text spans by some DP constant
if (x >= lineStartX && x <= lineEndX) {
int index = layout.getOffsetForHorizontal(line, x);
// We choose the most inner span (shortest) containing character at the given index
// if no such span can be found we will send the textview's react id as a touch handler
// In case when there are more than one spans with same length we choose the last one
// from the spans[] array, since it correspond to the most inner react element
ReactTagSpan[] spans = text.getSpans(index, index, ReactTagSpan.class);
if (spans != null) {
int targetSpanTextLength = text.length();
for (int i = 0; i < spans.length; i++) {
int spanStart = text.getSpanStart(spans[i]);
int spanEnd = text.getSpanEnd(spans[i]);
if (spanEnd > index && (spanEnd - spanStart) <= targetSpanTextLength) {
target = spans[i].getReactTag();
targetSpanTextLength = (spanEnd - spanStart);
}
}
}
}
return target;
}
示例10: getSuggestionSpansAtWord
import android.text.Spanned; //导入方法依赖的package包/类
/**
* Gets the suggestion spans that are put squarely on the word, with the exact start
* and end of the span matching the boundaries of the word.
* @return the list of spans.
*/
public SuggestionSpan[] getSuggestionSpansAtWord() {
if (!(mTextAtCursor instanceof Spanned && mWord instanceof Spanned)) {
return new SuggestionSpan[0];
}
final Spanned text = (Spanned)mTextAtCursor;
// Note: it's fine to pass indices negative or greater than the length of the string
// to the #getSpans() method. The reason we need to get from -1 to +1 is that, the
// spans were cut at the cursor position, and #getSpans(start, end) does not return
// spans that end at `start' or begin at `end'. Consider the following case:
// this| is (The | symbolizes the cursor position
// ---- ---
// In this case, the cursor is in position 4, so the 0~7 span has been split into
// a 0~4 part and a 4~7 part.
// If we called #getSpans(0, 4) in this case, we would only get the part from 0 to 4
// of the span, and not the part from 4 to 7, so we would not realize the span actually
// extends from 0 to 7. But if we call #getSpans(-1, 5) we'll get both the 0~4 and
// the 4~7 spans and we can merge them accordingly.
// Any span starting more than 1 char away from the word boundaries in any direction
// does not touch the word, so we don't need to consider it. That's why requesting
// -1 ~ +1 is enough.
// Of course this is only relevant if the cursor is at one end of the word. If it's
// in the middle, the -1 and +1 are not necessary, but they are harmless.
final SuggestionSpan[] spans = text.getSpans(mWordAtCursorStartIndex - 1,
mWordAtCursorEndIndex + 1, SuggestionSpan.class);
int readIndex = 0;
int writeIndex = 0;
for (; readIndex < spans.length; ++readIndex) {
final SuggestionSpan span = spans[readIndex];
// The span may be null, as we null them when we find duplicates. Cf a few lines
// down.
if (null == span) continue;
// Tentative span start and end. This may be modified later if we realize the
// same span is also applied to other parts of the string.
int spanStart = text.getSpanStart(span);
int spanEnd = text.getSpanEnd(span);
for (int i = readIndex + 1; i < spans.length; ++i) {
if (span.equals(spans[i])) {
// We found the same span somewhere else. Read the new extent of this
// span, and adjust our values accordingly.
spanStart = Math.min(spanStart, text.getSpanStart(spans[i]));
spanEnd = Math.max(spanEnd, text.getSpanEnd(spans[i]));
// ...and mark the span as processed.
spans[i] = null;
}
}
if (spanStart == mWordAtCursorStartIndex && spanEnd == mWordAtCursorEndIndex) {
// If the span does not start and stop here, ignore it. It probably extends
// past the start or end of the word, as happens in missing space correction
// or EasyEditSpans put by voice input.
spans[writeIndex++] = spans[readIndex];
}
}
return writeIndex == readIndex ? spans : Arrays.copyOfRange(spans, 0, writeIndex);
}