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 より軽量でシンプル

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

© 2024 Falco Tech Blog Suffusion theme by Sayontan Sinha