如何使用databinding in kotlin

阅读更多

1.首先关键是配置好gradle:

in build.gradle(project): 

 ext.gradle_version = '3.0.1'

 in build.gradle(app)

apply plugin: 'kotlin-kapt'

android{
....
dataBinding{
        enabled = true
    }
}

dependencies{
...
kapt "com.android.databinding:compiler:$gradle_version"
}

//kapt "com.android.databinding:compiler:$gradle_version" 可以不用的 

 

2.layout:




    
        
    

    

        

        


    

 

3.User:(注意,field不能private,要不会读取不了)

class User( val firstname: String,  val lastname: String)

 

4.activity:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        var user = User("Test", "User")
        binding.user = user
    }

 

你可能感兴趣的:(databinding,kotlin)