本文整理汇总了Java中android.text.style.URLSpan类的典型用法代码示例。如果您正苦于以下问题:Java URLSpan类的具体用法?Java URLSpan怎么用?Java URLSpan使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
URLSpan类属于android.text.style包,在下文中一共展示了URLSpan类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setHtmlWithLinkClickHandler
import android.text.style.URLSpan; //导入依赖的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: getText
import android.text.style.URLSpan; //导入依赖的package包/类
/**
* @param context
* @param spannable
* @param title
* @return
*/
public static SpannableStringBuilder getText(Activity context, Spannable spannable, String title) {
if (spannable == null || spannable.length() <= 0) {
Log.e(TAG, "getText spannable == null || spannable.length() <= 0 >> return new SpannableStringBuilder();");
return new SpannableStringBuilder();
}
SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
builder.clearSpans();// should clear old spans
URLSpan[] urls = spannable.getSpans(0, spannable.length(), URLSpan.class);
if (urls != null) {
for (URLSpan urlSpan : urls) {
if (urlSpan != null) {
MyURLSpan myURLSpan = new MyURLSpan(context, urlSpan.getURL(), title);
builder.setSpan(myURLSpan, spannable.getSpanStart(urlSpan), spannable
.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
}
}
return builder;
}
示例3: linkifyHtml
import android.text.style.URLSpan; //导入依赖的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;
}
示例4: dealUnderLinesData
import android.text.style.URLSpan; //导入依赖的package包/类
/**
* 解析link
*
* @param tv_content
* @param context
*/
private static void dealUnderLinesData(TextView tv_content, Context context) {
CharSequence charSequence = tv_content.getText();
if (charSequence instanceof Spannable) {
tv_content.setText("");
Spannable s = EmojiDisplay.spannableFilter(tv_content.getContext(),
new SpannableStringBuilder(charSequence),
charSequence,
EmoticonsKeyboardUtils.getFontHeight(tv_content));
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
String clickText = s.subSequence(start, end).toString();
URLSpanNoUnderline urlSpanNoUnderline = new URLSpanNoUnderline(context, span.getURL(), clickText);
s.setSpan(urlSpanNoUnderline, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv_content.append(s);
}
}
示例5: dealAILink
import android.text.style.URLSpan; //导入依赖的package包/类
/**
* 解析机器人对话link
*
* @param textView
*/
private static void dealAILink(TextView textView, String type) {
CharSequence charSequence = textView.getText();
if (charSequence instanceof Spannable) {
textView.setText("");
Spannable s = (Spannable) charSequence;
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
String clickContent = s.subSequence(start, end).toString();
AIURLSpan myURLSpan = new AIURLSpan(span.getURL(), type, clickContent, textView.getContext());
s.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.append(s);
}
}
示例6: dealCustomLink
import android.text.style.URLSpan; //导入依赖的package包/类
/**
* 解析富文本link
*
* @param textView
*/
private static void dealCustomLink(TextView textView) {
CharSequence charSequence = textView.getText();
if (charSequence instanceof Spannable) {
textView.setText("");
Spannable s = (Spannable) charSequence;
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
CustomClickSpan myURLSpan = new CustomClickSpan(textView.getContext(), span.getURL());
s.setSpan(myURLSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.append(s);
}
}
示例7: onLongPress
import android.text.style.URLSpan; //导入依赖的package包/类
@Override
protected void onLongPress() {
if (pressedLink instanceof URLSpanNoUnderline) {
URLSpanNoUnderline url = (URLSpanNoUnderline) pressedLink;
if (url.getURL().startsWith("/")) {
delegate.didPressedUrl(currentMessageObject, pressedLink, true);
return;
}
} else if (pressedLink instanceof URLSpan) {
delegate.didPressedUrl(currentMessageObject, pressedLink, true);
return;
}
resetPressedLink(-1);
if (buttonPressed != 0 || pressedBotButton != -1) {
buttonPressed = 0;
pressedBotButton = -1;
invalidate();
}
if (delegate != null) {
delegate.didLongPressed(this);
}
}
示例8: makeLinksClickable
import android.text.style.URLSpan; //导入依赖的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());
}
示例9: OrgSpannable
import android.text.style.URLSpan; //导入依赖的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();
}
}
}
示例10: setHtmlFromHelpFile
import android.text.style.URLSpan; //导入依赖的package包/类
private void setHtmlFromHelpFile(String file) {
htmltv.setHtml(getHelpFileContents(file));
htmltv.setText(RichTextUtils.replaceAll((Spanned) htmltv.getText(), URLSpan.class, new RichTextUtils.URLSpanConverter(), new RichTextUtils.ClickSpan.OnClickListener() {
@Override
public void onClick(String url) {
if(mListener == null) {
return;
}
if (url.startsWith("help:")) {
mListener.onHelpLinkClicked(url.substring(5));
} else if (url.startsWith("station:")) {
mListener.onStationLinkClicked(url.substring(8));
} else if (url.startsWith("mailto:")) {
mListener.onMailtoLinkClicked(url.substring(7));
} else {
mListener.onLinkClicked(url);
}
}
}));
}
示例11: setApplication
import android.text.style.URLSpan; //导入依赖的package包/类
private void setApplication(@Nullable Status.Application app) {
if (app != null) {
timestampInfo.append(" • ");
if (app.website != null) {
URLSpan span = new CustomURLSpan(app.website);
SpannableStringBuilder text = new SpannableStringBuilder(app.name);
text.setSpan(span, 0, app.name.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
timestampInfo.append(text);
timestampInfo.setMovementMethod(LinkMovementMethod.getInstance());
} else {
timestampInfo.append(app.name);
}
}
}
示例12: onUploadSuccess
import android.text.style.URLSpan; //导入依赖的package包/类
private void onUploadSuccess(final QueuedMedia item, Attachment media) {
item.id = media.id;
item.preview.setProgress(-1);
item.readyStage = QueuedMedia.ReadyStage.UPLOADED;
/* Add the upload URL to the text field. Also, keep a reference to the span so if the user
* chooses to remove the media, the URL is also automatically removed. */
item.uploadUrl = new URLSpan(media.textUrl);
int end = 1 + media.textUrl.length();
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(' ');
builder.append(media.textUrl);
builder.setSpan(item.uploadUrl, 1, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
int cursorStart = textEditor.getSelectionStart();
int cursorEnd = textEditor.getSelectionEnd();
textEditor.append(builder);
textEditor.setSelection(cursorStart, cursorEnd);
waitForMediaLatch.countDown();
}
示例13: onCreate
import android.text.style.URLSpan; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invite_friends);
ButterKnife.bind(this);
Intent intent = getIntent();
String addresseeName = intent.getStringExtra("contactName");
addresseePhone = intent.getStringExtra("contactPhone");
mAddresseeName.setText(addresseeName);
SpannableString inviteMessage = new SpannableString(this.getResources().getString(R.string.mine_send_message));
message = this.getResources().getString(R.string.mine_send_message);
inviteMessage.setSpan(new ForegroundColorSpan(Color.BLACK), 0, 54, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
inviteMessage.setSpan(new URLSpan("http://www.xingka.cc"), 54, 74,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
inviteMessage.setSpan(new ForegroundColorSpan(Color.BLACK), 74, 88, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mInviteMessage.setText(inviteMessage);
mInviteMessage.setMovementMethod(LinkMovementMethod.getInstance());
}
示例14: convertToLinkSpan
import android.text.style.URLSpan; //导入依赖的package包/类
public static void convertToLinkSpan(Context context, 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 LinkSpan(context, span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
示例15: getTransformation
import android.text.style.URLSpan; //导入依赖的package包/类
@Override
public CharSequence getTransformation(CharSequence src, View view) {
if (view instanceof TextView) {
TextView textView = (TextView) view;
Linkify.addLinks(textView, Linkify.WEB_URLS);
if (textView.getText() != null && textView.getText() instanceof Spannable) {
Spannable text = (Spannable) textView.getText();
URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
for (int i = spans.length - 1; i >= 0; i--) {
URLSpan oldSpan = spans[i];
int start = text.getSpanStart(oldSpan), end = text.getSpanEnd(oldSpan);
String url = oldSpan.getURL();
text.removeSpan(oldSpan);
text.setSpan(new CustomTabsURLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return text;
}
}
return src;
}