下面列出了android.text.Html#escapeHtml ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void setupWelcomeBanner() {
mOverflowButton = findViewById(R.id.landing_user_icon);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mOverflowButton.setClipToOutline(true);
}
mOverflowButton.setOnClickListener(v -> mPopupWindow.show());
new URLImageViewCacheLoader(
Objects.requireNonNull(mUser.getPhotoUrl()).toString(),
mOverflowButton).roundImage(true).execute();
TextView welcomeLine = findViewById(R.id.landing_welcome_line);
String format = getResources().getString(R.string.landing_welcome_line);
format = format.replace("\n", "<br>");
String name = Objects.requireNonNull(mUser.getDisplayName());
name = Html.escapeHtml(name.split(" ", 2)[0]);
String text = String.format(format, "<b>" + name + "</b>");
Spanned spannedText;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
spannedText = Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
} else {
spannedText = Html.fromHtml(text);
}
welcomeLine.setText(spannedText);
}
/**
* Html编码
*
* @param input 要Html编码的字符串
* @return Html编码后的字符串
*/
public static String htmlEncode(CharSequence input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return Html.escapeHtml(input);
} else {
// 参照Html.escapeHtml()中代码
StringBuilder out = new StringBuilder();
for (int i = 0, len = input.length(); i < len; i++) {
char c = input.charAt(i);
if (c == '<') {
out.append("<");
} else if (c == '>') {
out.append(">");
} else if (c == '&') {
out.append("&");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < len) {
char d = input.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < len && input.charAt(i + 1) == ' ') {
out.append(" ");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
return out.toString();
}
}
/**
* Html编码
*
* @param input 要Html编码的字符串
* @return Html编码后的字符串
*/
public static String htmlEncode(String input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return Html.escapeHtml(input);
} else {
// 参照Html.escapeHtml()中代码
StringBuilder out = new StringBuilder();
for (int i = 0, len = input.length(); i < len; i++) {
char c = input.charAt(i);
if (c == '<') {
out.append("<");
} else if (c == '>') {
out.append(">");
} else if (c == '&') {
out.append("&");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < len) {
char d = input.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < len && input.charAt(i + 1) == ' ') {
out.append(" ");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
return out.toString();
}
}
/**
* Html编码
*
* @param input 要Html编码的字符串
* @return Html编码后的字符串
*/
public static String htmlEncode(String input) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
return Html.escapeHtml(input);
} else {
// 参照Html.escapeHtml()中代码
StringBuilder out = new StringBuilder();
for (int i = 0, len = input.length(); i < len; i++) {
char c = input.charAt(i);
if (c == '<') {
out.append("<");
} else if (c == '>') {
out.append(">");
} else if (c == '&') {
out.append("&");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < len) {
char d = input.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < len && input.charAt(i + 1) == ' ') {
out.append(" ");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
return out.toString();
}
}
/**
* Html编码
*
* @param input 要Html编码的字符串
* @return Html编码后的字符串
*/
public static String htmlEncode(CharSequence input) {
if (Build.VERSION_CODES.JELLY_BEAN <= Build.VERSION.SDK_INT) {
return Html.escapeHtml(input);
} else {
// 参照Html.escapeHtml()中代码
StringBuilder out = new StringBuilder();
for (int i = 0, len = input.length(); i < len; i++) {
char c = input.charAt(i);
if (c == '<') {
out.append("<");
} else if (c == '>') {
out.append(">");
} else if (c == '&') {
out.append("&");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < len) {
char d = input.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < len && input.charAt(i + 1) == ' ') {
out.append(" ");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
return out.toString();
}
}
private static String formatString(String value) {
if (isUrl(value)) {
value = Html.escapeHtml(value);
value = "<a href=\"" + value + "\">" + value + "</a>";
} else {
value = Html.escapeHtml(value);
}
return "<font color=\"#006600\">\"" + value + "\"</font>";
}
/**
* Turn this item into HTML text, regardless of the type of data it
* actually contains.
*
* <p>The algorithm for deciding what text to return is:
* <ul>
* <li> If {@link #getHtmlText} is non-null, return that.
* <li> If {@link #getText} is non-null, return that, converting to
* valid HTML text. If this text contains style spans,
* {@link Html#toHtml(Spanned) Html.toHtml(Spanned)} is used to
* convert them to HTML formatting.
* <li> If {@link #getUri} is non-null, try to retrieve its data
* as a text stream from its content provider. If the provider can
* supply text/html data, that will be preferred and returned as-is.
* Otherwise, any text/* data will be returned and escaped to HTML.
* If it is not a content: URI or the content provider does not supply
* a text representation, HTML text containing a link to the URI
* will be returned.
* <li> If {@link #getIntent} is non-null, convert that to an intent:
* URI and return as an HTML link.
* <li> Otherwise, return an empty string.
* </ul>
*
* @param context The caller's Context, from which its ContentResolver
* and other things can be retrieved.
* @return Returns the item's representation as HTML text.
*/
public String coerceToHtmlText(Context context) {
// If the item has an explicit HTML value, simply return that.
String htmlText = getHtmlText();
if (htmlText != null) {
return htmlText;
}
// If this Item has a plain text value, return it as HTML.
CharSequence text = getText();
if (text != null) {
if (text instanceof Spanned) {
return Html.toHtml((Spanned)text);
}
return Html.escapeHtml(text);
}
text = coerceToHtmlOrStyledText(context, false);
return text != null ? text.toString() : null;
}
static String formatPre(String text, boolean quote) {
int level = 0;
StringBuilder sb = new StringBuilder();
String[] lines = text.split("\\r?\\n");
for (String line : lines) {
// Opening quotes
// https://tools.ietf.org/html/rfc3676#section-4.5
if (quote) {
int tlevel = 0;
while (line.startsWith(">")) {
tlevel++;
if (tlevel > level)
sb.append("<blockquote>");
line = line.substring(1); // >
if (line.startsWith(" >"))
line = line.substring(1);
}
if (tlevel > 0)
if (line.length() > 0 && line.charAt(0) == ' ')
line = line.substring(1);
// Closing quotes
for (int i = 0; i < level - tlevel; i++)
sb.append("</blockquote>");
level = tlevel;
}
// Tabs characters
StringBuilder l = new StringBuilder();
for (int j = 0; j < line.length(); j++) {
char kar = line.charAt(j);
if (kar == '\t') {
l.append(' ');
while (l.length() % TAB_SIZE != 0)
l.append(' ');
} else
l.append(kar);
}
line = l.toString();
// Html characters
// This will handle spaces / word wrapping as well
line = Html.escapeHtml(line);
sb.append(line);
sb.append("<br>");
}
// Closing quotes
for (int i = 0; i < level; i++)
sb.append("</blockquote>");
return sb.toString();
}
public static String a(CharSequence charsequence)
{
return Html.escapeHtml(charsequence);
}
public static String escapeHtml(CharSequence html) {
return Html.escapeHtml(html);
}
private static String formatObjectString(String value) {
return "<font color=\"#004488\">\"" + Html.escapeHtml(value)
+ "\"</font>";
}
public static String escapeHtml(CharSequence html) {
return Html.escapeHtml(html);
}
public static String escapeHtml(CharSequence html) {
return Html.escapeHtml(html);
}
public static String escapeHtml(CharSequence html) {
return Html.escapeHtml(html);
}
public static String escapeHtml(CharSequence text) {
return Html.escapeHtml(text);
}