本文整理匯總了Java中android.text.SpannableStringBuilder.getSpanStart方法的典型用法代碼示例。如果您正苦於以下問題:Java SpannableStringBuilder.getSpanStart方法的具體用法?Java SpannableStringBuilder.getSpanStart怎麽用?Java SpannableStringBuilder.getSpanStart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.text.SpannableStringBuilder
的用法示例。
在下文中一共展示了SpannableStringBuilder.getSpanStart方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setHtmlWithLinkClickHandler
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public static void setHtmlWithLinkClickHandler(TextView tv, String html,
Action1<String> linkClickHandler) {
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for (URLSpan span : urls) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
public void onClick(View view) {
linkClickHandler.call(span.getURL());
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
tv.setText(strBuilder);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
示例2: makeLinksClickable
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public static void makeLinksClickable(TextView v) {
SpannableStringBuilder ssb = new SpannableStringBuilder(v.getText());
URLSpan[] spans = ssb.getSpans(0, ssb.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = ssb.getSpanStart(span);
int end = ssb.getSpanEnd(span);
final String url = span.getURL();
ssb.removeSpan(span);
ClickableSpan cSpan = new ClickableSpan() {
@Override
public void onClick(View v2) {
LinkDialogFragment f = LinkDialogFragment.newInstance(url);
FragmentManager fm = ((AppCompatActivity) v2.getContext())
.getSupportFragmentManager();
f.show(fm, f.getUniqueTag());
}
};
ssb.setSpan(cSpan, start, end, 0);
}
v.setText(ssb);
v.setMovementMethod(ArticleMovementMethod.getInstance());
}
示例3: OrgSpannable
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public OrgSpannable(String str) {
SpannableStringBuilder ssb = OrgFormatter.INSTANCE.parse(context, str);
string = ssb.toString();
Object[] allSpans = ssb.getSpans(0, ssb.length() - 1, Object.class);
spans = new OrgSpan[allSpans.length];
for (int i = 0; i < allSpans.length; i++) {
spans[i] = new OrgSpan();
spans[i].start = ssb.getSpanStart(allSpans[i]);
spans[i].end = ssb.getSpanEnd(allSpans[i]);
spans[i].className = allSpans[i].getClass().getSimpleName();
if (allSpans[i] instanceof URLSpan) {
spans[i].url = ((URLSpan)allSpans[i]).getURL();
}
}
}
示例4: isInFangContext
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public boolean isInFangContext() {
SpannableStringBuilder builder = attributedString;
ClickableSpan[] spans = builder.getSpans(0,
builder.length(), ClickableSpan.class);
Map<String, String> dict = SingletonData.getInstance().getFangAliasDict();
if (spans.length > 0) {
ClickableSpan span = spans[0];
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
String unit = builder.subSequence(start, end)
.toString();
String left = dict.get(unit);
if (left == null) {
left = unit;
}
if (SingletonData.getInstance().getAllFang().contains(left)
&& start > 0
&& builder.toString().substring(start - 1, start).equals("、")
&& builder.charAt(0) != '*') {
return true;
}
}
return false;
}
示例5: isInYaoContext
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
public boolean isInYaoContext() {
SpannableStringBuilder builder = attributedString;
ClickableSpan[] spans = builder.getSpans(0,
builder.length(), ClickableSpan.class);
Map<String, String> dict = SingletonData.getInstance().getYaoAliasDict();
if (spans.length > 0) {
ClickableSpan span = spans[0];
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
String unit = builder.subSequence(start, end)
.toString();
String left = dict.get(unit);
if (left == null) {
left = unit;
}
if (SingletonData.getInstance().getAllYao().contains(left)
&& start > 0
&& builder.toString().substring(start - 1, start).equals("、")) {
return true;
}
}
return false;
}
示例6: isInOtherSpan
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
protected <T> boolean isInOtherSpan(SpannableStringBuilder builder, @Nullable Class<T> kind, int length, int startPosition, int endPosition) {
T[] spans = builder.getSpans(0, length, kind);
List<int[]> spanRange = new ArrayList<>();
for (T span : spans) {
int spanStart = builder.getSpanStart(span);
int spanEnd = builder.getSpanEnd(span);
spanRange.add(new int[]{spanStart, spanEnd});
}
boolean result = false;
for (int[] range : spanRange) {
if (startPosition >= range[0] && startPosition <= range[1]) {
result = true;
break;
}
if (endPosition >= range[0] && endPosition <= range[1]) {
result = true;
break;
}
}
return result;
}
示例7: resetLinkSpan
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private void resetLinkSpan(SpannableStringBuilder ssb, RichTextConfig config, URLSpan urlSpan) {
int start = ssb.getSpanStart(urlSpan);
int end = ssb.getSpanEnd(urlSpan);
ssb.removeSpan(urlSpan);
LinkHolder linkHolder = new LinkHolder(urlSpan.getURL(), accentColor);
if (config.linkFixCallback != null) {
config.linkFixCallback.fix(linkHolder);
}
LongClickableURLSpan longClickableURLSpan = new LongClickableURLSpan(linkHolder, config.onUrlClickListener, config.onUrlLongClickListener);
ssb.setSpan(longClickableURLSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
示例8: addSpacesAroundSpansUntilFixed
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private FixingResult addSpacesAroundSpansUntilFixed(SpannableStringBuilder builder,
int widthMeasureSpec, int heightMeasureSpec) {
Object[] spans = builder.getSpans(0, builder.length(), Object.class);
List<Object> spansWithSpacesBefore = new ArrayList<>(spans.length);
List<Object> spansWithSpacesAfter = new ArrayList<>(spans.length);
for (Object span : spans) {
int spanStart = builder.getSpanStart(span);
if (isNotSpace(builder, spanStart - 1)) {
builder.insert(spanStart, " ");
spansWithSpacesBefore.add(span);
}
int spanEnd = builder.getSpanEnd(span);
if (isNotSpace(builder, spanEnd)) {
builder.insert(spanEnd, " ");
spansWithSpacesAfter.add(span);
}
try {
setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
return FixingResult.fixed(spansWithSpacesBefore, spansWithSpacesAfter);
} catch (IndexOutOfBoundsException ignored) {
}
}
if (HtmlTextView.DEBUG) {
Log.d(HtmlTextView.TAG, "Could not fix the Spanned by adding spaces around spans");
}
return FixingResult.notFixed();
}
示例9: removeUrlFromEditable
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private static Editable removeUrlFromEditable(Editable editable, @Nullable URLSpan urlSpan) {
if (urlSpan == null) {
return editable;
}
SpannableStringBuilder builder = new SpannableStringBuilder(editable);
int start = builder.getSpanStart(urlSpan);
int end = builder.getSpanEnd(urlSpan);
if (start != -1 && end != -1) {
builder.delete(start, end);
}
return builder;
}
示例10: setEmoteSpans
import android.text.SpannableStringBuilder; //導入方法依賴的package包/類
private void setEmoteSpans(SpannableStringBuilder builder) {
for (URLSpan span : builder.getSpans(0, builder.length(), URLSpan.class)) {
setLargeLinks(builder, span);
File emoteDir = new File(Environment.getExternalStorageDirectory(), "RedditEmotes");
File emoteFile = new File(emoteDir, span.getURL().replace("/", "").replaceAll("-.*", "")
+ ".png"); //BPM uses "-" to add dynamics for emotes in browser. Fall back to original here if exists.
boolean startsWithSlash = span.getURL().startsWith("/");
boolean hasOnlyOneSlash = StringUtils.countMatches(span.getURL(), "/") == 1;
if (emoteDir.exists() && startsWithSlash && hasOnlyOneSlash && emoteFile.exists()) {
//We've got an emote match
int start = builder.getSpanStart(span);
int end = builder.getSpanEnd(span);
CharSequence textCovers = builder.subSequence(start, end);
//Make sure bitmap loaded works well with screen density.
BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = new DisplayMetrics();
((WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
options.inDensity = 240;
options.inScreenDensity = metrics.densityDpi;
options.inScaled = true;
//Since emotes are not directly attached to included text, add extra character to attach image to.
builder.removeSpan(span);
if (builder.subSequence(start, end).charAt(0) != '.') {
builder.insert(start, ".");
}
Bitmap emoteBitmap = BitmapFactory.decodeFile(emoteFile.getAbsolutePath(), options);
builder.setSpan(new ImageSpan(getContext(), emoteBitmap), start, start + 1,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//Check if url span has length. If it does, it's a spoiler/caption
if (textCovers.length() > 1) {
builder.setSpan(new URLSpan("/sp"), start + 1, end + 1,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
builder.setSpan(new StyleSpan(Typeface.ITALIC), start + 1, end + 1,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
builder.append("\n"); //Newline to fix text wrapping issues
}
}
}