Android:如何将Spannable.setSpan与String.format结合起来?

Android:如何将Spannable.setSpan与String.format结合起来?,android,resources,spannable,Android,Resources,Spannable,我正在将Span设置为文本的一部分。Span本身运行良好。但是,文本是由String.format从Resources创建的,我不知道将Span设置为的文本部分的start和end 我尝试在strings.xml中使用自定义HTML标记,但是getText或getString将它们删除。我可以使用这样的getString(R.string.text,“+arg+”),然后使用Html.fromHtml(),因为arg正是我想要设置跨度的地方 我看到使用了格式化的文本“普通文本###span he

我正在将
Span
设置为文本的一部分。Span本身运行良好。但是,文本是由
String.format
Resources
创建的,我不知道将Span设置为的文本部分的
start
end

我尝试在
strings.xml
中使用自定义HTML标记,但是
getText
getString
将它们删除。我可以使用这样的
getString(R.string.text,“+arg+”)
,然后使用
Html.fromHtml()
,因为
arg
正是我想要设置跨度的地方

我看到使用了格式化的文本
“普通文本###span here##普通文本”
。它解析字符串,删除标记并设置跨度


我的问题是,是否有更好的方法将Span设置为格式化字符串,如
“something%s something”
,或者我应该使用上述方法之一?

我通过引入
标记达格
类解决了这个问题,该类的实例扩展为
值。然后我创建了一个对象,该对象负责读取包含标记的文本,并用跨距替换这些标记。不同的跨距在地图标签->工厂中注册

有一个小小的惊喜。如果您有类似于
“something something”
,则
Html.fromHtml
将此文本读取为
“something something”
。我必须在整个文本周围添加标记以防止出现这种情况。

getText()
将返回
跨距字符串
对象,这些对象包含strings.xml中定义的格式。我已经创建了一个将保留格式字符串中的任何跨距的函数,即使它们包含格式说明符(
span-dstring
参数中的跨距也会保留)。像这样使用它:

Spanned toDisplay = SpanFormatter.format(getText(R.string.foo), bar, baz, quux);
乔治认为通过
getText()
的方式很有趣。但是没有必要写一个额外的类
getText()
返回一个
CharSequence
。因此,使用
SpannableStringBuilder
将其设置为文本视图:

textViewCell.setText(new SpannableStringBuilder(getText(R.string.foo));
在您的strings.xml中,您可以使用html标记编写它(不要忘记文本前后的引号):

“二氧化碳”

这种标记对我来说很有效,可以手动将其设置为文本视图(如上所述),也可以将其分配到布局中。

我决定编写George提供的Kotlin版本,以防有一天链接消失:

/*
* Copyright © 2014 George T. Steel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//https://github.com/george-steel/android-utils/blob/master/src/org/oshkimaadziig/george/androidutils/SpanFormatter.java
/**
 * Provides [String.format] style functions that work with [Spanned] strings and preserve formatting.
 *
 * @author George T. Steel
 */
object SpanFormatter {
    private val FORMAT_SEQUENCE: Pattern = Pattern.compile("%([0-9]+\\$|<?)([^a-zA-z%]*)([[a-zA-Z%]&&[^tT]]|[tT][a-zA-Z])")

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter. If there are
     * more arguments than required by `format`,
     * additional arguments are ignored.
     * @return the formatted string (with spans).
     */
    fun format(format: CharSequence?, vararg args: Any?): SpannedString {
        return format(java.util.Locale.getDefault(), format, *args)
    }

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param locale
     * the locale to apply; `null` value means no localization.
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter.
     * @return the formatted string (with spans).
     * @see String.format
     */
    fun format(locale: java.util.Locale, format: CharSequence?, vararg args: Any?): SpannedString {
        val out = SpannableStringBuilder(format)
        var i = 0
        var argAt: Int = -1
        while (i < out.length) {
            val m: java.util.regex.Matcher = FORMAT_SEQUENCE.matcher(out)
            if (!m.find(i))
                break
            i = m.start()
            val exprEnd: Int = m.end()
            val argTerm: String? = m.group(1)
            val modTerm: String? = m.group(2)
            val typeTerm: String? = m.group(3)
            var cookedArg: CharSequence
            when (typeTerm) {
                "%" -> cookedArg = "%"
                "n" -> cookedArg = "\n"
                else -> {
                    val argIdx: Int = when (argTerm) {
                        "" -> ++argAt
                        "<" -> argAt
                        else -> argTerm!!.substring(0, argTerm.length - 1).toInt() - 1
                    }
                    val argItem: Any? = args[argIdx]
                    cookedArg = if ((typeTerm == "s") && argItem is Spanned) {
                        argItem
                    } else {
                        String.format(locale, "%$modTerm$typeTerm", argItem)
                    }
                }
            }
            out.replace(i, exprEnd, cookedArg)
            i += cookedArg.length
        }
        return SpannedString(out)
    }
}
/*
*版权所有©2014乔治T.斯蒂尔
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
//https://github.com/george-steel/android-utils/blob/master/src/org/oshkimaadziig/george/androidutils/SpanFormatter.java
/**
*提供[String.format]样式的函数,这些函数使用[span]字符串并保留格式。
*
*@作者乔治·T·斯蒂尔
*/
对象格式化程序{

private val FORMAT_SEQUENCE:Pattern=Pattern.compile(“%([0-9]+\\$| George的解决方案工作得很好。与这里的其他一些解决方案不同,George的解决方案解决了尝试使用常用格式设置程序替换字符串的问题,例如可以在strings.xml文件中找到的
%1$s
。他的SpanFormatter类与String.format一样容易工作,但会返回用于.W的跨距字符串eird,对于这个简单但仍然重要的功能,android仍然没有官方支持。如果您有字符串的参数呢?例如,strings.xml文件中的“Hello%s”意思是什么?
/*
* Copyright © 2014 George T. Steel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//https://github.com/george-steel/android-utils/blob/master/src/org/oshkimaadziig/george/androidutils/SpanFormatter.java
/**
 * Provides [String.format] style functions that work with [Spanned] strings and preserve formatting.
 *
 * @author George T. Steel
 */
object SpanFormatter {
    private val FORMAT_SEQUENCE: Pattern = Pattern.compile("%([0-9]+\\$|<?)([^a-zA-z%]*)([[a-zA-Z%]&&[^tT]]|[tT][a-zA-Z])")

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter. If there are
     * more arguments than required by `format`,
     * additional arguments are ignored.
     * @return the formatted string (with spans).
     */
    fun format(format: CharSequence?, vararg args: Any?): SpannedString {
        return format(java.util.Locale.getDefault(), format, *args)
    }

    /**
     * Version of [String.format] that works on [Spanned] strings to preserve rich text formatting.
     * Both the `format` as well as any `%s args` can be Spanned and will have their formatting preserved.
     * Due to the way [android.text.Spannable]s work, any argument's spans will can only be included **once** in the result.
     * Any duplicates will appear as text only.
     *
     * @param locale
     * the locale to apply; `null` value means no localization.
     * @param format the format string (see [java.util.Formatter.format])
     * @param args
     * the list of arguments passed to the formatter.
     * @return the formatted string (with spans).
     * @see String.format
     */
    fun format(locale: java.util.Locale, format: CharSequence?, vararg args: Any?): SpannedString {
        val out = SpannableStringBuilder(format)
        var i = 0
        var argAt: Int = -1
        while (i < out.length) {
            val m: java.util.regex.Matcher = FORMAT_SEQUENCE.matcher(out)
            if (!m.find(i))
                break
            i = m.start()
            val exprEnd: Int = m.end()
            val argTerm: String? = m.group(1)
            val modTerm: String? = m.group(2)
            val typeTerm: String? = m.group(3)
            var cookedArg: CharSequence
            when (typeTerm) {
                "%" -> cookedArg = "%"
                "n" -> cookedArg = "\n"
                else -> {
                    val argIdx: Int = when (argTerm) {
                        "" -> ++argAt
                        "<" -> argAt
                        else -> argTerm!!.substring(0, argTerm.length - 1).toInt() - 1
                    }
                    val argItem: Any? = args[argIdx]
                    cookedArg = if ((typeTerm == "s") && argItem is Spanned) {
                        argItem
                    } else {
                        String.format(locale, "%$modTerm$typeTerm", argItem)
                    }
                }
            }
            out.replace(i, exprEnd, cookedArg)
            i += cookedArg.length
        }
        return SpannedString(out)
    }
}