英文:
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<Infodc>{
val infoList = ArrayList<Infodc>()
val inf1 = Infodc(
1, "header_str", R.drawable.image0,
"string_1", "string_2")
infoList.add(inf1)
return infoList
}
}
I tried R.string.string_header
for header = "header_str"
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("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)
)
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论