build.gradle(app)

android {
    buildFeatures {
        viewBinding true
    }
}

使う時

class MainActivity : AppCompatActivity() {
    lateinit var bind : ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivityMainBinding.inflate(layoutInflater)
        init()
        setContentView(bind.root)
    }

    private fun init() {
        bind.button.setOnClickListener{
            Toast.makeText(this, "on click", Toast.LENGTH_LONG).show()
            bind.textViewDice.text = "6"
        }
    }

}
findViewById は、もう不要、DataBinding より軽量でシンプル
© 2024 Falco Tech Blog Suffusion theme by Sayontan Sinha