配置模块gradle.build依赖
主要时viewModel相关的声明周期依赖

| plugins { |
| id 'com.android.application' |
| id 'kotlin-android' |
| id 'kotlin-android-extensions' |
| } |
| |
| android { |
| compileSdkVersion 30 |
| buildToolsVersion "30.0.3" |
| |
| defaultConfig { |
| applicationId "com.zjgsu.cxxu.testviewmodel" |
| minSdkVersion 16 |
| targetSdkVersion 30 |
| versionCode 1 |
| versionName "1.0" |
| |
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" |
| } |
| |
| buildTypes { |
| release { |
| minifyEnabled false |
| proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' |
| } |
| } |
| compileOptions { |
| sourceCompatibility JavaVersion.VERSION_1_8 |
| targetCompatibility JavaVersion.VERSION_1_8 |
| } |
| kotlinOptions { |
| jvmTarget = '1.8' |
| } |
| } |
| |
| dependencies { |
| |
| implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" |
| |
| implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" |
| implementation 'androidx.core:core-ktx:1.2.0' |
| implementation 'androidx.appcompat:appcompat:1.1.0' |
| implementation 'com.google.android.material:material:1.1.0' |
| implementation 'androidx.constraintlayout:constraintlayout:1.1.3' |
| testImplementation 'junit:junit:4.+' |
| androidTestImplementation 'androidx.test.ext:junit:1.1.1' |
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' |
| } |
MainActivity.kt
注意使用viewModelProvider()及其方法get()来取得对应的viewModel子类的实例(以取用保存其中的数据)
| package ... |
| |
| import androidx.appcompat.app.AppCompatActivity |
| import android.os.Bundle |
| import androidx.lifecycle.ViewModelProvider |
| import kotlinx.android.synthetic.main.activity_main.* |
| |
| class MainActivity : AppCompatActivity() { |
| |
| |
| private lateinit var viewModel: MainViewModel |
| |
| |
| |
| |
| |
| |
| |
| override fun onCreate(savedInstanceState: Bundle?) { |
| super.onCreate(savedInstanceState) |
| setContentView(R.layout.activity_main) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| viewModel = ViewModelProvider(this).get(MainViewModel::class.java) |
| |
| plusOneBtn.setOnClickListener { |
| viewModel.counter++ |
| refreshCounter() |
| } |
| |
| |
| |
| refreshCounter() |
| } |
| |
| |
| |
| |
| |
| private fun refreshCounter() { |
| infoText.text = viewModel.counter.toString() |
| } |
| } |
MainViewModel.kt
| package ... |
| |
| import androidx.lifecycle.ViewModel |
| |
| class MainViewModel : ViewModel() { |
| var counter=0 |
| } |
main_activity.xml
| |
| <?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=".MainActivity"> |
| |
| |
| <Button |
| android:id="@+id/plusOneBtn" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:text="Button" |
| app:layout_constraintBottom_toBottomOf="parent" |
| app:layout_constraintEnd_toEndOf="parent" |
| app:layout_constraintHorizontal_bias="0.498" |
| app:layout_constraintStart_toStartOf="parent" |
| app:layout_constraintTop_toTopOf="parent" |
| app:layout_constraintVertical_bias="0.207" /> |
| |
| <TextView |
| android:id="@+id/infoText" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:layout_marginTop="72dp" |
| android:layout_marginEnd="196dp" |
| android:layout_marginRight="196dp" |
| android:text="infoText" |
| app:layout_constraintEnd_toEndOf="parent" |
| app:layout_constraintTop_toTopOf="parent" /> |
| </androidx.constraintlayout.widget.ConstraintLayout> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」