[英]Using variable from another file Kotlin
if I have 2 Activities and I want to add if condition from a variable on the another activity How can I do that? 如果我有2个活动,并且我想添加另一个活动中变量的条件,我该怎么做? like if i have the variable that contains 9 numbers in the first layout(first activity) and i want to set if condition in the another one using the x variable that is the question. 就像如果我的变量在第一个布局(第一个活动)中包含9个数字,并且我想使用x变量(即问题)在另一个变量中设置条件。 I am using Android Studio with kotlin. 我将Kotlin与Android Studio一起使用。
If the value of your variable doesn't change after you start your second activity, you can use extras to pass the value between them. 如果您的变量值在您开始第二项活动后没有变化,则可以使用Extras在变量之间传递值。
class FirstActivity : Activity() {
var myVariable: Boolean = false
fun gotoSecondActivity() {
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("MyVariable", myVariable)
startActivity(intent)
}
}
class SecondActivity: Activity() {
fun getMyVariable(): Boolean {
if (intent != null) {
if (intent.extras != null) {
return intent.extras.getBoolean("MyVariable")
}
}
return false // default
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.