kotlin使用Viewmodel和LiveData制作一个篮球计分App。(1)单击红色的加分按钮给A队加分,分别可以加1、2、3分,单击蓝色的加分按钮给B队加分。 (2)撤销按钮可以撤销最后一次加分。 (3)刷新按钮重新从0开始计分。

时间: 2023-11-27 19:50:09 浏览: 36
好的,下面是一个简单的实现过程: 首先,在 build.gradle 文件中添加以下依赖: ``` implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' ``` 接着,在布局文件中添加如下代码: ``` <Button android:id="@+id/btn_undo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="撤销" /> <Button android:id="@+id/btn_refresh" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刷新" /> <TextView android:id="@+id/tv_score_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> <Button android:id="@+id/btn_add_1_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+1" /> <Button android:id="@+id/btn_add_2_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+2" /> <Button android:id="@+id/btn_add_3_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+3" /> <TextView android:id="@+id/tv_score_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" /> <Button android:id="@+id/btn_add_1_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+1" /> <Button android:id="@+id/btn_add_2_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+2" /> <Button android:id="@+id/btn_add_3_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+3" /> ``` 然后,在 MainActivity 中定义 ViewModel 和 LiveData 对象: ```kotlin class MainActivity : AppCompatActivity() { private lateinit var viewModel: ScoreViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) viewModel = ViewModelProvider(this).get(ScoreViewModel::class.java) // 绑定 A 队分数 val tvScoreA: TextView = findViewById(R.id.tv_score_a) viewModel.scoreA.observe(this, Observer { score -> tvScoreA.text = score.toString() }) // 绑定 B 队分数 val tvScoreB: TextView = findViewById(R.id.tv_score_b) viewModel.scoreB.observe(this, Observer { score -> tvScoreB.text = score.toString() }) // 绑定加分按钮 findViewById<Button>(R.id.btn_add_1_a).setOnClickListener { viewModel.addScoreA(1) } findViewById<Button>(R.id.btn_add_2_a).setOnClickListener { viewModel.addScoreA(2) } findViewById<Button>(R.id.btn_add_3_a).setOnClickListener { viewModel.addScoreA(3) } findViewById<Button>(R.id.btn_add_1_b).setOnClickListener { viewModel.addScoreB(1) } findViewById<Button>(R.id.btn_add_2_b).setOnClickListener { viewModel.addScoreB(2) } findViewById<Button>(R.id.btn_add_3_b).setOnClickListener { viewModel.addScoreB(3) } // 绑定撤销和刷新按钮 findViewById<Button>(R.id.btn_undo).setOnClickListener { viewModel.undo() } findViewById<Button>(R.id.btn_refresh).setOnClickListener { viewModel.refresh() } } } class ScoreViewModel : ViewModel() { private var scoreAValue = 0 private var scoreBValue = 0 private val scoreAHistory = Stack<Int>() private val scoreBHistory = Stack<Int>() val scoreA: MutableLiveData<Int> by lazy { MutableLiveData<Int>().also { it.value = scoreAValue } } val scoreB: MutableLiveData<Int> by lazy { MutableLiveData<Int>().also { it.value = scoreBValue } } fun addScoreA(score: Int) { scoreAHistory.push(scoreAValue) scoreAValue += score scoreA.value = scoreAValue } fun addScoreB(score: Int) { scoreBHistory.push(scoreBValue) scoreBValue += score scoreB.value = scoreBValue } fun undo() { if (!scoreAHistory.isEmpty()) { scoreAValue = scoreAHistory.pop() scoreA.value = scoreAValue } if (!scoreBHistory.isEmpty()) { scoreBValue = scoreBHistory.pop() scoreB.value = scoreBValue } } fun refresh() { scoreAValue = 0 scoreBValue = 0 scoreAHistory.clear() scoreBHistory.clear() scoreA.value = scoreAValue scoreB.value = scoreBValue } } ``` 最后,运行程序,即可看到一个简单的篮球计分 App。用户可以点击不同的按钮加分、撤销和刷新分数。同时,ViewModel 和 LiveData 的使用确保了应用的数据和界面状态的正确性和一致性。

相关推荐

最新推荐

recommend-type

Android中使用Kotlin实现一个简单的登录界面

Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源。接下来本文通过实例代码给大家讲解Android中使用Kotlin实现一个简单的登录界面,一起看看吧
recommend-type

kotlin和Java的相互调用示例详解

Kotlin 的设计过程中就...在 Kotlin 中可以直接调用既有的 Java 代码, 反过来在 Java 中也可以很流畅地使用 Kotlin 代码,下面这篇文章主要给大家介绍了关于kotlin和Java的相互调用的相关资料,需要的朋友可以参考下。
recommend-type

Kotlin中双冒号::使用方法

主要给大家介绍了关于Kotlin中双冒号::使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Kotlin具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

Android在Kotlin中更好地使用LitePal

今天小编就为大家分享一篇关于Android在Kotlin中更好地使用LitePal,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
recommend-type

Android 使用Kotlin自定义View的方法教程

最近想加强一下自定义view方面的学习,正好也在学习Kotlin,所以就尝试着用Kotlin写一下简单的自定义view,下面这篇文章主要给大家介绍了关于Android使用Kotlin自定义View的方法教程,需要的朋友可以参考下。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。