-
-
Save omidMirrajei/5a60305b097fcf1171fec993de4374bb to your computer and use it in GitHub Desktop.
Initialize view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sample 1 | |
private var button: Button = findViewById(R.id.button) | |
//Sample 2 | |
private var button: Button? = null | |
private fun init(){ button = findViewById(R.id.button) } | |
//Sample 3 (The lateinit keyword stands for late initialization. lateinit modifier is allowed only on mutable properties) | |
private lateinit var button:Button | |
private fun init(){ button = findViewById(R.id.button) } | |
//Sample 4 | |
private val button:Button by lazy { | |
findViewById(R.id.button) as Button | |
// or | |
findViewById<Button>(R.id.button) | |
} | |
// we need use to init button | |
button.setOnClickListener{ } | |
//Sample 5 (use view id directly) | |
button.setOnClickListener {textView.text = "Hello Kotlin"} | |
//Sample 6 | |
var textView: TextView? = null | |
textView = findViewById(R.id.textView) | |
textView?.text = "Sample Text" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment