DevLog
Python, TypeScript, etc...
MyPage
Python, TypeScript, etc...
2022/07/15

グローバル変数の使い方【Android, Kotlin】

AndroidKotlin
preview
@shiromisanta
Frontend Engineer
  •  /
目次
クラス定義

クラス定義

Storeとなるクラスを定義

package com.example.cameraxapp.lib import android.app.Application class GlobalStore: Application() { var userId: String = "" companion object { private var instance : GlobalStore? = null fun getInstance(): GlobalStore { if (instance == null) instance = GlobalStore() return instance!! } } }

使い方

ボタンが押されたらeditTextの文字をグローバル変数に格納する例

viewBinding.buttonNext.setOnClickListener { val store = GlobalStore.getInstance() store.userId = viewBinding.editTextUserId.text.toString() }

関連記事

preview
@shiromisanta 2022/08/18
光学カメラAndroidKotlin
preview
@shiromisanta 2022/07/20
Kotlin
preview
@shiromisanta 2022/07/15
AndroidKotlin
preview
@shiromisanta 2022/07/15
AndroidKotlin
2022/07/15

グローバル変数の使い方【Android, Kotlin】

AndroidKotlin

クラス定義

Storeとなるクラスを定義

package com.example.cameraxapp.lib import android.app.Application class GlobalStore: Application() { var userId: String = "" companion object { private var instance : GlobalStore? = null fun getInstance(): GlobalStore { if (instance == null) instance = GlobalStore() return instance!! } } }

使い方

ボタンが押されたらeditTextの文字をグローバル変数に格納する例

viewBinding.buttonNext.setOnClickListener { val store = GlobalStore.getInstance() store.userId = viewBinding.editTextUserId.text.toString() }

関連記事

preview
@shiromisanta
Frontend Engineer
  •  /
©︎Devlog