前言:确保你的as在3.6或者以上版本
1、在工程build.gradle中设置
android {
...
buildFeatures {
viewBinding true
}
}
2、
package com.shellway.lifedemo
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.shellway.lifedemo.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.button01.setOnClickListener {
val intent = Intent(this,SecondActivity::class.java)
startActivity(intent)
}
binding.button02.setOnClickListener {
val intent = Intent(this,ThirdActivity::class.java)
startActivity(intent)
}
}
}