spannablestring

Android SpannableString用法

只谈情不闲聊 提交于 2019-12-02 10:48:00
TextView算是Android开发中最最常用的控件了,有的时候,我们要给一个TextView中的显示的文字设置不同的样式或者响应事件,比如同一个TextView中,有的字是红色,有的字是蓝色,有的字点击之后有响应事件,有的点击之后没有响应事件,甚至我们想在TextView中显示一个数学公式等等。 那么对于形形色色的需求我们有没有解决方案呢?当然有,一种是使用HTML来解决,另一种就是使用SpannableString。 SpannableString 其实和String一样,都是一种字符串类型,同样TextView也可以直接设置SpannableString作为显示文本,不同的是SpannableString可以通过使用其方法setSpan方法实现字符串各种形式风格的显示,重要的是可以指定设置的区间,也就是为字符串指定下标区间内的子字符串设置格式。 setSpan(Object what, int start, int end, int flags) 方法需要用户输入四个参数, what 表示设置的格式是什么,可以是前景色、背景色也可以是可点击的文本等等, start 表示需要设置格式的子字符串的起始下标,同理 end 表示终了下标, flags 属性就有意思了,共有四种属性: Spanned.SPAN_INCLUSIVE_EXCLUSIVE 从起始下标到终了下标,包括起始下标

Remove style on spans

天大地大妈咪最大 提交于 2019-12-02 03:58:52
I have used this StyleSpanRemover to remove style from selected text, but i have a problem. I want to have a bold button that toggle bold style of selected text. it means if the selected text (or part of that) is not bold, bold all of them; and if all of selected text is bold, remove bold style from it. But when i use this code, after one removing bold style on a text and again bolding it, getStyle() always returns Typeface.NORMAL Here is the code i have used: boolean isAllSpansBold = true; /** * Bold selected text */ Spannable spannable = new SpannableString(mEditTextContent.getText());

Using SpannableString[/Builder] and Rich-text

≡放荡痞女 提交于 2019-12-02 03:36:29
I'm attempting to construct rich-text where the first part says "Looking for: " and is bolded, but the rest is NOT bolded. This is my code: SpannableStringBuilder lookingForString = new SpannableStringBuilder("Looking for: "); lookingForString.setSpan(new StyleSpan(Typeface.BOLD), 0, lookingForString.length(), 0); int start = (lookingForString.length() - 1); for (int i = 0; i < looking_for_names.length(); ++i) { // No comma before the first and after the last 'Looking for' value if (i > 0) { lookingForString.append(", "); lookingForString.setSpan(new StyleSpan(Typeface.NORMAL), start,

Spannable String Error

筅森魡賤 提交于 2019-12-02 01:55:48
I am trying to display some text in an alert dialog box as a hyperlink. Part of the process requires me to use SpannableString to format some text. The problem is that my application experiences a runtime error on the SpannableString part of my code. TextView Tv= (TextView) findViewById(R.id.textView3); SpannableString s = new SpannableString("www.google.com"); Linkify.addLinks(s, Linkify.WEB_URLS); Tv.setText(s); Tv.setMovementMethod(LinkMovementMethod.getInstance()); I looked in the DDMS and the Error says Java.Lang.NullPointerException. Has anyone experienced this? I Should be able to pass

Use SpannableString in Android Home Screen widgets?

安稳与你 提交于 2019-12-01 12:07:35
Can I use SpannableStrings in a widget's textView? I tried and all it rendered was plain text. I don't know if it is something I'm doing wrong (most likely), or if it just isn't possible for some reason. Here's the code I'm using (nothing special really...) public static void updateWidgetState(Context paramContext, String paramString, Integer appWidgetId) { SpannableString majorLabel = new SpannableString(""); SpannableString minorLabel = new SpannableString(""); if (position > -1) { majorLabel = GetParsedMajorLabel(paramContext); minorLabel = GetParsedMinorLabel(paramContext); } RemoteViews

Justifying text inside a TextView in android

一笑奈何 提交于 2019-12-01 07:36:41
So, as most of you know, there is no text justifying inside a TextView in Android. So, I built a custom TextView to get around the problem. However, for some reason, sometimes punctuation marks break the line for some reason in some devices. I tested on an LG G3 and emulator (Nexus 4 running latest version) and a comma "," for instance breaks the justification on the LG G3 but not on the emulator. If I add a Padding start and end (or left and right) of at least 2, the problem is solved. This looks very arbitrary to me. Basically, my logic was that in order to justify the text, I would need to

How can I make my custom ReplacementSpan wrap?

守給你的承諾、 提交于 2019-12-01 05:01:32
问题 I'm trying to apply a background with a border to specific text in a TextView . A BackgroundColorSpan works great, but it doesn't have a border. To get the border, I instead use a custom ReplacementSpan and override draw . It looks good for short words/sentences, but it fails to wrap lines. How can I make my custom ReplacementSpan wrap? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_replacement_span); final

java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList

回眸只為那壹抹淺笑 提交于 2019-12-01 03:19:20
I'm using the phonegap Android plugin: EmailComposerwithAttachments https://github.com/phonegap/phonegap-plugins/tree/master/Android/EmailComposerWithAttachments and it occurs the following error when executing startActivitywithResult function. I'm using Android 4.2 with Cordova 2.5.0 java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to java.util.ArrayList // setting attachments try { JSONArray attachments = parameters.getJSONArray("attachments"); if (attachments != null && attachments.length() > 0) { ArrayList<Uri> uris = new ArrayList<Uri>(); //convert from

SpannableString regex in a ListView

守給你的承諾、 提交于 2019-12-01 01:12:19
I have a ListView that I'm binding a collection of strings to, using a custom adapter. I'm also underlining certain keywords in the text. I'm using a SpannableString and a regular expression to underline the words, but I'm wondering if this is the most efficient way to do it? I'm noticing a lot of allocations in the Allocation Tracker of the java.util.regex.Matcher and the regex.util.regex.Pattern classes, which may be causing memory leaks in my app. I know regex's can be expensive, but I'm not sure another way to do what I need to do. public class Main extends ListActivity { private static

How can I prevent the cursor from resizing in an EditText (MultiAutoCompleteTextView) after I add an imagespan using a SpannableStringBuilder?

拟墨画扇 提交于 2019-12-01 00:26:29
Here is what it looks like in the beginning when I haven't added any imagespan chips - As you can tell there the cursor is placed at the right size and the gravity is respected. Then when I add an imagespan, the cursor all of a sudden becomes bigger like this - I don't understand why this happens and I also don't know how to fix it, i.e. keep the cursor the same size. Finally when I start typing again, the cursor is all wierd while maintaining the size of the of the font and the span also moves a little to the bottom. I really want to the keep the cursor the same size and keep it centered I'm