0


Android编程权威指南(第四版)- 第 4 章 UI状态的保存与恢复

文章目录

代码:

大体是一样的,修改了一些

依赖

implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")

MainActivity

package com.example.geoquiz

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModelProviders
privateconstval KEY_INDEX ="index"privateconstval KEY_SOCRE ="socre"privateconstval KEY_BUTTONSTATUS="buttonStatus"class MainActivity :AppCompatActivity(){privatelateinitvar questionText: TextView
    privatelateinitvar ture_button: Button
    privatelateinitvar false_button: Button
    privatelateinitvar last_button: Button
    privatelateinitvar next_button: Button
    privatelateinitvar score_button: Button
    privateval TAG="MainActivity"privateval quizViewModel: QuizViewModel by lazy {
        ViewModelProviders.of(this)[QuizViewModel::class.java]}overridefunonCreate(savedInstanceState: Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val index= savedInstanceState?.getInt(KEY_INDEX,0)?:0
        quizViewModel.index=index

        questionText =findViewById(R.id.question)
        ture_button =findViewById(R.id.ture_button)
        false_button =findViewById(R.id.false_button)
        last_button =findViewById(R.id.last_button)
        next_button =findViewById(R.id.next_button)
        next_button =findViewById(R.id.next_button)
        score_button =findViewById(R.id.score_button)updateQuestion()//点击问题跳转下一题
        questionText.setOnClickListener(View.OnClickListener{
            quizViewModel.nextQuestion()updateQuestion()})

        ture_button.setOnClickListener(View.OnClickListener{
            quizViewModel.buttonStatus =falsecheckAnswer(true)closeButton()})

        false_button.setOnClickListener(View.OnClickListener{
            quizViewModel.buttonStatus =falsecheckAnswer(false)closeButton()})

        last_button.setOnClickListener(View.OnClickListener{openButton()
            quizViewModel.lastQuestion()updateQuestion()})

        next_button.setOnClickListener(View.OnClickListener{openButton()
            quizViewModel.nextQuestion()updateQuestion()})

        score_button.setOnClickListener(View.OnClickListener{settlementScore()})}overridefunonStart(){super.onStart()
        Log.e(TAG,"onStart")}overridefunonResume(){super.onResume()
        Log.e(TAG,"onResume")}overridefunonPause(){super.onPause()
        Log.e(TAG,"onPause")}overridefunonSaveInstanceState(outState: Bundle){super.onSaveInstanceState(outState)
        Log.e(TAG,"onSaveInstanceState")//保存问题索引值
       outState.putInt(KEY_INDEX,quizViewModel.index)}overridefunonStop(){super.onStop()
        Log.e(TAG,"onStop")}overridefunonDestroy(){super.onDestroy()
        Log.e(TAG,"onDestroy")}privatefunupdateQuestion(){var textId = quizViewModel.questionIndex
        questionText.setText(textId)}privatefuncheckAnswer(answer: Boolean){var questionanswer = quizViewModel.questionAnswer
        if(answer == questionanswer){
            quizViewModel.score +=20
            Toast.makeText(this, R.string.ture_toast, Toast.LENGTH_SHORT).show()}else{
            Toast.makeText(this, R.string.false_toast, Toast.LENGTH_SHORT).show()}}privatefuncloseButton(){if(!quizViewModel.buttonStatus){
            ture_button.isEnabled = quizViewModel.buttonStatus
            false_button.isEnabled = quizViewModel.buttonStatus
        }}privatefunopenButton(){if(!quizViewModel.buttonStatus){
            ture_button.isEnabled =true
            false_button.isEnabled =true}}privatefunsettlementScore(){//答完所有题结算成绩if(quizViewModel.index == quizViewModel.listSize -1){
            Toast.makeText(this,"得分为${quizViewModel.score}", Toast.LENGTH_SHORT).show()
            quizViewModel.score =0}else{
            Toast.makeText(this,"题目没有答完哦", Toast.LENGTH_SHORT).show()}}}

QuizViewModel

package com.example.geoquiz

import androidx.lifecycle.ViewModel

class QuizViewModel :ViewModel(){var index =0privatevar questionList =listOf(Question(R.string.question,true),Question(R.string.question1,true),Question(R.string.question2,true),Question(R.string.question3,false),Question(R.string.question4,false),Question(R.string.question5,false))var buttonStatus =true//按钮可以按var score =0val listSize = questionList.size

    val questionIndex: Int
        get()= questionList[index].questionID

    val questionAnswer: Boolean
        get()= questionList[index].answer

    funnextQuestion(){
        index =(index +1)% questionList.size
    }funlastQuestion(){//       if (index == 0) {//            index = questionList.size - 1//        } else {//            index -= 1//        }
        index =if(index ==0) questionList.size -1else index -1}}

Question

package com.example.geoquiz

import androidx.annotation.StringRes

dataclassQuestion(@StringResval questionID: Int,val answer: Boolean){}

知识点

ViewModelProviders

类提供了

ViewModelProvider

实例,调用

ViewModelProviders.of(this)

创建并返回一个关联了MainActivity的

ViewModelProvider

实例,

ViewModelProvider

会提供ViewModel实例给MainActivity

activity被销毁的两种情况:

  1. 用户结束使用activity
  2. 因设备配置发送改变时系统销毁
标签: android ui

本文转载自: https://blog.csdn.net/weixin_74239923/article/details/136081871
版权归原作者 运气真不错! 所有, 如有侵权,请联系我们删除。

“Android编程权威指南(第四版)- 第 4 章 UI状态的保存与恢复”的评论:

还没有评论