得到的EditText Spannable字符串 [英] Get Spannable String from EditText

查看:473
本文介绍了得到的EditText Spannable字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置SpannableString为EditText上,现在我想从这个的EditText文本和获得它的标记信息。我想这样的

I have set SpannableString to EditText, now i want to get this text from EditText and get its markup information. I tried like this

SpannableStringBuilder spanStr = (SpannableStringBuilder) et.getText().;
int boldIndex   = spanStr.getSpanStart(new StyleSpan(Typeface.BOLD));
int italicIndex     = spanStr.getSpanStart(new StyleSpan(Typeface.ITALIC));

但它给指数-1为粗体和斜体的,虽然它是显示文本​​斜体和大胆。

but it gives index -1 for both bold and italic, although it is showing text with italic and bold.

请帮忙。

推荐答案

从code您已经发布,你传递新的跨越,以spanStr,并要求它找到他们。你需要有一个对实际应用的跨越的实例。如果这不可行,或者你不想直接跟踪跨度,你可以简单地调用 <一href="http://developer.android.com/reference/android/text/SpannableStringBuilder.html#getSpans%28int,%20int,%20java.lang.Class%3CT%3E%29%20%22getSpans"相对=nofollow> getSpans 以获得所有跨应用。然后,可以过滤该数组你想要的东西。

From the code you've posted, you're passing new spans to spanStr and asking it to find them. You'll need to have a reference to the instances of those spans that are actually applied. If that's not feasible or you don't want to track spans directly, you can simply call getSpans to get all the spans applied. You can then filter that array for what you want.

如果你不关心跨度特别,你也可以只叫<一href="http://developer.android.com/reference/android/text/Html.html#toHtml%28android.text.Spanned%29"相对=nofollow称号=toHtml> Html.toHtml(spanStr)得到一个HTML标记的版本。

If you don't care about the spans in particular, you can also just call Html.toHtml(spanStr) to get an HTML tagged version.

修改:添加code例如

这将抓住所有应用StyleSpans这是你想要的。

This will grab all applied StyleSpans which is what you want.

    /* From the Android docs on StyleSpan: "Describes a style in a span. 
     * Note that styles are cumulative -- both bold and italic are set in 
     * separate spans, or if the base is bold and a span calls for italic, 
     * you get bold italic. You can't turn off a style from the base style."*/
StyleSpan[] mSpans = et.getText().getSpans(0, et.length(), StyleSpan.class);

下面是一个链接 StyleSpan 文档。

Here's a link to the StyleSpan docs.

要挑选出你想,如果你有混入到收集/阵列不同跨度,您可以使用跨度的instanceof 来找出你已经什么类型的跨度得到的。这个片段将检查特定范围 mSpan 是StyleSpan的一个实例,然后打印的开始/结束的指标和标志。该标志是描述如何跨度结束的行为如常量:它们是否包含并且将样式应用到文本的开始/结束索引或只是开始/结束范围内的索引文本输入)

To pick out the spans you want if you have various spans mixed in to a collection/array, you can use instanceof to figure out what type of spans you've got. This snippet will check if a particular span mSpan is an instance of StyleSpan and then print its start/end indices and flags. The flags are constants that describe how the span ends behave such as: Do they include and apply styling to the text at the start/end indices or only to text input at an index inside the start/end range).

 if (mSpan instanceof StyleSpan) { 
        int start = et.getSpanStart(mSpan);
        int end = et.getSpanEnd(mSpan);
        int flag = et.getSpanFlags(mSpan);
        Log.i("SpannableString Spans", "Found StyleSpan at:\n" +
                "Start: " + start + 
                 "\n End: " + end + 
                 "\n Flag(s): " + flag);
 }

这篇关于得到的EditText Spannable字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆