[英]Why can't i bind the result of a function to my TextView in Android Studio?
I'm trying to self-teach Kotlin with the Google Android Dev Courses (I started a week ago and have very little experience in coding).我正在尝试通过 Google Android 开发课程自学 Kotlin(我一周前开始学习,几乎没有编码经验)。
At the end of the course that thaught me to build a working Tip Calculator, there was an optional exercise to create a similar app.在教我构建一个有效的小费计算器的课程结束时,有一个可选练习来创建一个类似的应用程序。
I chose to create an Animal Age Calculator but i can't manage to bind the result to its TextView.我选择创建一个动物年龄计算器,但我无法将结果绑定到它的 TextView。
The output that i get is Animal age: %s
.我得到的输出是
Animal age: %s
。
The ouput that i want is the result of either my catAgeFormula(age)
or my dogAgeFormula(age)
我想要的输出是我的
catAgeFormula(age)
或我的dogAgeFormula(age)
I understand it's showing me this : <string name="animal_age">Animal age: %s</string>
我知道它向我展示了这个:
<string name="animal_age">Animal age: %s</string>
But the thing i don't understand is why my binding doesn't work binding.ageResult.text = getString(R.string.animal_age)
但我不明白的是为什么我的绑定不起作用
binding.ageResult.text = getString(R.string.animal_age)
Here is the complete function i'm not sure about :这是我不确定的完整功能:
private fun calculateAge() {
val stringInTextField = binding.userAge.text.toString()
val age = stringInTextField.toIntOrNull()
if (age == null || age == 0) {
binding.ageResult.text = "0"
return
}
when (binding.animalOptions.checkedRadioButtonId) {
R.id.option_cat -> catAgeFormula(age)
else -> dogAgeFormula(age)
}
binding.ageResult.text = getString(R.string.animal_age)
}
And the output TextView :和输出 TextView :
<TextView
android:id="@+id/age_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/calculate_button"
tools:text="Animal age : 42 years" />
This is my first post on Stack Overflow so i'm really sorry if messed up something.这是我在 Stack Overflow 上的第一篇文章,所以如果搞砸了我真的很抱歉。
Thanks in advance for you help :)预先感谢您的帮助:)
You are missing an argument on the getString
method您在
getString
方法上缺少一个参数
private fun calculateAge() {
val stringInTextField = binding.userAge.text.toString()
//...
binding.ageResult.text = getString(R.string.animal_age, stringInTextField)
}
Your string is:你的字符串是:
<string name="animal_age">Animal age: %s</string>
The %s
means it expects to receive 1 String
type argument. %s
表示它期望接收 1 个String
类型参数。 The method getString
can receive 2 arguments.方法
getString
可以接收 2 个参数。 The first is the id of the resource string and the second a vararg
that can be any number of other arguments.第一个是资源字符串的 id,第二个是
vararg
,可以是任意数量的其他参数。 This is because a string defined on the resources can take up any number of other arguments, by example:这是因为在资源上定义的字符串可以占用任意数量的其他参数,例如:
<string name="example">Example %1$s %2$d</string>
Where %2$d
it would be a number expected as a second argument其中
%2$d
将是一个预期作为第二个参数的数字
val example = getString(R.string.example, "one", 2)
//Example one 2
You can refer to the official documentation for further explanations进一步的解释可以参考官方文档
Thanks to cutiko, i found out i was missing a variable for the code to work properly.多亏了 cutiko,我发现我缺少一个让代码正常工作的变量。
I added my when
statement into a variable named animalAge
and used it as a second argument to my getString
method.我将
when
语句添加到名为animalAge
的变量中,并将其用作getString
方法的第二个参数。
Here is the final working function :这是最终的工作功能:
private fun calculateAge() {
val stringInTextField = binding.userAge.text.toString()
val age = stringInTextField.toIntOrNull()
//...
val animalAge = when (binding.animalOptions.checkedRadioButtonId) {
R.id.option_cat -> catAgeFormula(age)
else -> dogAgeFormula(age)
}
binding.ageResult.text = getString(R.string.animal_age, animalAge)
}
And the final working string resource :以及最终的工作字符串资源:
<string name="animal_age">Animal age: %d</string>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.