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 的使用确保了应用的数据和界面状态的正确性和一致性。