如何连接声明和 If 语句?
How to connect declarations and If statements?
我想通过几个 if 语句更改多个 textView 颜色。除了最后一部分以“....价格”结尾外,所有这些都有不同的 ID。我试过将 if 语句粘贴到所有这些语句上,但构建失败。
我已经做了这样的声明。
textView1Price = ("+10.00")
textView2Price = ("-10.00")
textView3Price = ("0.00")
和类似的 if 语句。
if (colorID.text.startsWith("-")) {
colorID.setTextColor(Color.RED)
}
if (colorID.text.startsWith("+")) {
colorID.setTextColor(Color.GREEN)
}
if (colorID.text.startsWith("0.00")) {
colorID.text = "_"
colorID.setTextColor(Color.DKGRAY)
我无法弄清楚如何将声明连接到 If 语句。我已经尝试过类似的方法并取得了部分成功,但不能列出更多的声明。
val colorID = textView1Price
我也试图找到一种方法来引用
text.contains("Price")
但没能做到。任何帮助表示赞赏。感谢您的宝贵时间。
编辑:跟进解决方案
//Declarations
//metal location A price
Price1 = ("+10.00")
//metal location B price
Price2 = ("-10.00")
//metal location C price
Price3 = ("0.00")
Android Studio 建议 when 语句超过 if
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
tv.text.startsWith("0.00") -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
}
对于具有像 textView?Price
这样的 ID 的 TextView,您可以使用 getIdentifier()
方法在循环中获取它们的整数 ID,然后应用条件,就像这样(对于 9 个 TextView):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
对于 resources
和 packageName
,您可能需要提供有效的 Context
,例如:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
如果您的代码不在 activity 中。
其他的TextView,如果id有这样的相似度,可以用同样的方法。
我想通过几个 if 语句更改多个 textView 颜色。除了最后一部分以“....价格”结尾外,所有这些都有不同的 ID。我试过将 if 语句粘贴到所有这些语句上,但构建失败。
我已经做了这样的声明。
textView1Price = ("+10.00")
textView2Price = ("-10.00")
textView3Price = ("0.00")
和类似的 if 语句。
if (colorID.text.startsWith("-")) {
colorID.setTextColor(Color.RED)
}
if (colorID.text.startsWith("+")) {
colorID.setTextColor(Color.GREEN)
}
if (colorID.text.startsWith("0.00")) {
colorID.text = "_"
colorID.setTextColor(Color.DKGRAY)
我无法弄清楚如何将声明连接到 If 语句。我已经尝试过类似的方法并取得了部分成功,但不能列出更多的声明。
val colorID = textView1Price
我也试图找到一种方法来引用
text.contains("Price")
但没能做到。任何帮助表示赞赏。感谢您的宝贵时间。
编辑:跟进解决方案
//Declarations
//metal location A price
Price1 = ("+10.00")
//metal location B price
Price2 = ("-10.00")
//metal location C price
Price3 = ("0.00")
Android Studio 建议 when 语句超过 if
(1..912).forEach {
val id = resources.getIdentifier("Price$it", "id", packageName)
val tv = findViewById<TextView>(id)
when {
tv.text.startsWith("-") -> tv.setTextColor(Color.RED)
tv.text.startsWith("+") -> tv.setTextColor(Color.GREEN)
tv.text.startsWith("0.00") -> {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
}
对于具有像 textView?Price
这样的 ID 的 TextView,您可以使用 getIdentifier()
方法在循环中获取它们的整数 ID,然后应用条件,就像这样(对于 9 个 TextView):
(1..9).forEach {
val id = resources.getIdentifier("textView${it}Price", "id", packageName)
val tv = findViewById<TextView>(id)
if (tv.text.startsWith("-")) {
tv.setTextColor(Color.RED)
} else if (tv.text.startsWith("+")) {
tv.setTextColor(Color.GREEN)
} else if (tv.text.startsWith("0.00")) {
tv.text = "_"
tv.setTextColor(Color.DKGRAY)
}
}
对于 resources
和 packageName
,您可能需要提供有效的 Context
,例如:
val id = context.resources.getIdentifier("textView${it}Price", "id", context.packageName)
如果您的代码不在 activity 中。
其他的TextView,如果id有这样的相似度,可以用同样的方法。