Kotlin硬编码字符串

huangapple go评论59阅读模式
英文:

Kotlin hardcoded String

问题

我想使用Android Studio的翻译编辑器为我的应用添加多语言支持。但它只支持.xml字符串。我在数据类中有一些字符串,并且我使用数据类来创建ArrayList。我真的希将这些字符串从.xml中调用出来,但我无法弄清楚如何做到。

我的数据类:

data class Infodc (
        val id: Int,
        val header: String,
        val image: Int,
        val infoOne: String,
        val infoTwo: String
)

我的列表:

object Constants{
    fun getInfo(): ArrayList<Infodc>{
        val infoList = ArrayList<Infodc>()

        val inf1 = Infodc(
            1, "header_str", R.drawable.image0,
            "string_1", "string_2")
        infoList.add(inf1)
        return infoList
    }
}

我尝试过将R.string.string_header用于header = "header_str",但只显示了10位数字,而不是字符串本身。

之后,我尝试了getString,但我又失败了。

英文:

I want to add multilingual support to my app using the Android Studio Translations editor. But it only supports .xml strings. I have some strings in data class and I use data class for making ArrayList. I really want to call these strings from .xml but I couldn't figure out how.

my data class:

data class Infodc (
        val id: Int,
        val header: String,
        val image: Int,
        val infoOne: String,
        val infoTwo: String
)

my list

object Constants{
    fun getInfo(): ArrayList&lt;Infodc&gt;{
        val infoList = ArrayList&lt;Infodc&gt;()

        val inf1 = Infodc(
            1, &quot;header_str&quot;, R.drawable.image0,
            &quot;string_1&quot;, &quot;string_2&quot;)
        infoList.add(inf1)
        return infoList
    }
}

I tried R.string.string_header for header = &quot;header_str&quot; but that only show 10 digits, not the string itself.

After that, I tried getString but again I failed.

答案1

得分: 2

你可以这样做
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val infodc = Infodc(
            1,
            R.string.header,
            R.drawable.ic_launcher_foreground,
            R.string.info_one,
            R.string.info_two,
            this
        )

        Log.d("MyTag", "$infodc") // D/MyTag: Infodc(id=1, header=Header, image=2131099744, infoOne=Info One, infoTwo=Info Two)
    }
}

data class Infodc(
    val id: Int,
    val header: String,
    val image: Int,
    val infoOne: String,
    val infoTwo: String
) {
    constructor(
        id: Int,
        @StringRes header: Int,
        @DrawableRes image: Int,
        @StringRes infoOne: Int,
        @StringRes infoTwo: Int,
        context: Context
    ) : this(
        id,
        context.getString(header),
        image,
        context.getString(infoOne),
        context.getString(infoTwo)
    )
}
现在你可以将一个字符串的 id 和图片作为构造函数的参数但作为类字段时你将获得一个字符串

另一种方法是创建一个仅包含字符串 id 的类然后在使用时使用该字段的 `getString` 方法
英文:

You can do it like this:

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val infodc = Infodc(
            1,
            R.string.header,
            R.drawable.ic_launcher_foreground,
            R.string.info_one,
            R.string.info_two,
            this
        )

        Log.d(&quot;MyTag&quot;, &quot;$infodc&quot;) // D/MyTag: Infodc(id=1, header=Header, image=2131099744, infoOne=Info One, infoTwo=Info Two)


    }
}

data class Infodc(
    val id: Int,
    val header: String,
    val image: Int,
    val infoOne: String,
    val infoTwo: String
)
{
    constructor(
        id: Int,
        @StringRes header: Int,
        @DrawableRes image: Int,
        @StringRes infoOne: Int,
        @StringRes infoTwo: Int,
        context: Context
    ) : this(
        id,
        context.getString(header),
        image,
        context.getString(infoOne),
        context.getString(infoTwo)
    )
}

Now You can pass to a constructor id of String and image but as a class field You will get String

<hr>

Another way is to have a class that only contains an id of string and then when You want to use it use getString of that field.

huangapple
  • 本文由 发表于 2020年9月20日 19:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63978302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定