spannablestring

How to have a SpannableStringBuilder append a span that's inside a formatted string?

雨燕双飞 提交于 2019-12-05 07:31:04
问题 Background Suppose I use SpannableStringBuilder to append multiple stuff into it, and one of them is string that I format from the strings.xml file, which has a span inside: SpannableStringBuilder stringBuilder = new SpannableStringBuilder (); stringBuilder.append(...)... final SpannableString span = new SpannableString(...); span.setSpan(new BackgroundColorSpan(0xff990000), ...,...,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); stringBuilder.append(getString(R.string.string_to_format, span));

Span的四个常用flag的用法

半世苍凉 提交于 2019-12-05 01:28:27
最近在做聊天表情输入的时候,遇到了一个bug:在已经输入的两个表情中间输入文字时,文字不显示出来,但是实际发送的时候却发送出去了这些文字。 比如:我要在下面的两个表情中间插入文字“abc”。 下图是编辑时候的情形,定位输入焦点到两个表情的中间处,输入“abc”,但是没显示出来 下图是发送完毕后,显示在消息列表的内容。 原因是:表情部分是用 SpannableString实现的,关键是在 setSpan 时指定了错误的flag。 一般有以下四种: Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点 Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点 Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点 Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点 其实这4个flag在用于不可编辑的控件中时,效果是一样的,可以随便用。 但是在EditText等可以编辑的控件中使用时,就会有区别。 对一个 SpannableString的编辑有两种情况:在它的开始或结尾处插入新的内容。 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE

SpannableStringBuilder replace content with Regex

偶尔善良 提交于 2019-12-05 00:31:59
问题 I have the following code in which I am going to mark the contents between the curly braces with SpannableString and remove the curly braces but it gives wrong result. String text = "the {quic}k brown {fox} jumps {over} the lazy dog. {A Quick} {brow}nfoxjumpsoverthelazydog"; tv.setText(makeSpannable(text, "\\{.*?\\}")); public SpannableStringBuilder makeSpannable(String text, String regex) { SpannableStringBuilder spannable = new SpannableStringBuilder(text); Pattern pattern = Pattern.compile

How can i have EditText with Clickable Spannables and still selectable by longClick?

亡梦爱人 提交于 2019-12-04 18:42:14
问题 I have TextView with spans of type ClickableStringSpan defined as below: public class ClickableStringSpan extends ClickableSpan { private View.OnClickListener mListener; int color; public ClickableStringSpan(View.OnClickListener listener,int color) { mListener = listener; this.color = color; } @Override public void onClick(View v) { mListener.onClick(v); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); ds.setUnderlineText(false); ds.setColor(color); } } I set

How to display each clicked words from TextView

寵の児 提交于 2019-12-04 16:14:22
I am planned to develop an App with very simple concept.The requirement is I want to add a text file from phone( Dynamic, so set span with clickabble position not possible for spannable string ) with help of intent chooser. And need to display the selected file content in textView (any view if u suggested). Once I clicked any of the word from textview content i need display that word in Toast. button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showFileChooser(); } }); private void showFileChooser() { Intent intent = new Intent(Intent.ACTION_GET

Android create Spannable which does not wrap

被刻印的时光 ゝ 提交于 2019-12-04 14:47:33
I have a following issue with laying out text on Android. I'm basically trying to have two lines of text with minimal spacing and each should be styled differently. I've had quite good working solution with two singlelined TextViews one placed below the other, but I've been still getting a little bit cropped text on certain devices.. So I decided to switch to just one TextView an use Spannables instead which should be generally a better solution in all circumstances. That means I needed to remove the single line property from my TextView -> in order to be able to wrap the line before starting

How to add red wavy line below text in Android's TextView

最后都变了- 提交于 2019-12-04 14:19:37
I'm trying to add red wavy line below errors in texts, such as: Unfortunately I can't find a proper *Span class to wrap the error text with. How should I implement such a feature in Android? I've solved the problem by implementing a custom Span: Add error_underline.png to your resources: <-- tiny 6x3 pixels here Then use this class to create spans: static class ErrorSpan extends DynamicDrawableSpan { private BitmapDrawable mRedWavy; private int mWidth; private int mBmpHeight; ErrorSpan(Resources resources) { super(DynamicDrawableSpan.ALIGN_BASELINE); mRedWavy = new BitmapDrawable(resources,

Android TextView format multiple words

梦想的初衷 提交于 2019-12-04 13:34:35
Original String: Lorem ##ipsum## dolar ##sit## atem. Lorem ipsum dolar sit ##atem##. After formating: Lorem #ipsum dolar #sit atem. Lorem ipsum dolar sit #atem. But only the last one has the Formating i want. See image below. CODE private void format() { CharSequence text = editContent.getText(); MovementMethod movementMethod = editContent.getMovementMethod(); if ((movementMethod == null) || !(movementMethod instanceof LinkMovementMethod)) { editContent.setMovementMethod(LinkMovementMethod.getInstance()); } text = setSpanBetweenTokens(text, "##", new ForegroundColorSpan(0xFF0099FF), new

TextView with ImageSpan messes up line height

自古美人都是妖i 提交于 2019-12-04 10:53:29
I have a TextView filled with text that should contain some ImageSpan objects. The images can be taller than the normal line height which causes the following problem: if the image is the last object of a line, the following lines' height is correct if the last object is not an image, the following lines' height is set to the image-containing line's height Here is the correct situation: This is the wrong situation: What's more interesting is that if there's a new-line character in the text, the line height is good from that point on again. The TextView is just a pretty basic one: <TextView

Combining Spannable with String.format()

こ雲淡風輕ζ 提交于 2019-12-04 07:52:06
问题 Suppose you have the following string: String s = "The cold hand reaches for the %1$s %2$s Ellesse's"; String old = "old"; String tan = "tan"; String formatted = String.format(s,old,tan); //"The cold hand reaches for the old tan Ellesse's" Suppose you want to end up with this string, but also have a particular Span set for any word replaced by String.format . For instance, we also want to do the following: Spannable spannable = new SpannableString(formatted); spannable.setSpan(new