简体   繁体   English

使用另一个文件Kotlin中的变量

[英]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.

相关问题 如何从Kotlin中的另一个类获取变量? - How to get a variable from another class in kotlin? 从 kotlin 中的另一个 class 文件访问 textview - Access a textview from another class file in kotlin Kotlin 从一个类到另一个类获取变量/值 - Kotlin get a variable/value from one class to another Kotlin 中未从一种状态传送到另一种状态的变量的值 - value of a variable not carried from one state to another in Kotlin 访问另一个文件中的变量 - Accesing a variable from another file 从Android中的另一个模块导入Kotlin文件时出现“未解析的引用…”错误 - 'Unresolved reference…' errors for import a kotlin file from another module in android 如何从Kotlin中的其他文件访问变量? - How can I access a variable from different file in kotlin? 如何从另一个活动中获取活动中的变量并在从那里初始化后使主要活动使用变量?(kotlin) - how to get a variable in an activity from another activity and make the main activity to use variable after initializing from there ?(kotlin ) 使用Kotlin将Image从ImageView传递到Android中的另一个Activity - Using Kotlin to pass Image from ImageView to another Activity in Android 如何使用 Kotlin 从 Android 中的片段访问另一个片段? - How access another fragment from a fragment in Android using Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM