Browse Source

🎉新增:ViewBinding 视图绑定

xinggang 3 years ago
parent
commit
55fe513fcd

+ 5 - 1
app/build.gradle

@@ -29,6 +29,10 @@ android {
     kotlinOptions {
         jvmTarget = '11'
     }
+
+    buildFeatures {
+        viewBinding true
+    }
 }
 
 dependencies {
@@ -39,4 +43,4 @@ dependencies {
     implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
     androidTestImplementation 'androidx.test.ext:junit:1.1.3'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
-}
+}

+ 5 - 1
app/src/main/AndroidManifest.xml

@@ -9,14 +9,18 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/Theme.MyApplication">
+        <activity
+            android:name=".DemoActivity"
+            android:exported="false" />
         <activity
             android:name=".MainActivity"
             android:exported="true">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
+
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>
 
-</manifest>
+</manifest>

+ 11 - 0
app/src/main/java/cn/com/lttc/myapplication/DemoActivity.kt

@@ -0,0 +1,11 @@
+package cn.com.lttc.myapplication
+
+import android.os.Bundle
+import androidx.appcompat.app.AppCompatActivity
+
+class DemoActivity : AppCompatActivity() {
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_demo)
+    }
+}

+ 25 - 3
app/src/main/java/cn/com/lttc/myapplication/MainActivity.kt

@@ -1,11 +1,33 @@
 package cn.com.lttc.myapplication
 
-import androidx.appcompat.app.AppCompatActivity
 import android.os.Bundle
+import android.view.View
+import android.widget.Toast
+import androidx.appcompat.app.AppCompatActivity
+import cn.com.lttc.myapplication.databinding.ActivityMainBinding
+
+class MainActivity : AppCompatActivity(), View.OnClickListener {
+
+    private val tag: String = "ifu25"
+    private lateinit var v: ActivityMainBinding
 
-class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
+
+        v = ActivityMainBinding.inflate(layoutInflater)
+        setContentView(v.root)
+
+        initView()
+    }
+
+    private fun initView() {
+        v.tvMain.setOnClickListener(this)
+    }
+
+    override fun onClick(view: View?) {
+        if (view!!.id == v.tvMain.id) {
+            Toast.makeText(this, "好", Toast.LENGTH_SHORT).show()
+        }
     }
-}
+}

+ 18 - 0
app/src/main/res/layout/activity_demo.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".DemoActivity">
+
+    <Button
+        android:id="@+id/button2"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Button2"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toTopOf="parent" />
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 1 - 0
app/src/main/res/layout/activity_main.xml

@@ -7,6 +7,7 @@
     tools:context=".MainActivity">
 
     <TextView
+        android:id="@+id/tvMain"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Hello World!"