当前位置:   article > 正文

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

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

代码:

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

依赖

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

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
private const val KEY_INDEX = "index"
private const val KEY_SOCRE = "socre"
private const val KEY_BUTTONSTATUS="buttonStatus"
class MainActivity : AppCompatActivity() {
    private lateinit var questionText: TextView
    private lateinit var ture_button: Button
    private lateinit var false_button: Button
    private lateinit var last_button: Button
    private lateinit var next_button: Button
    private lateinit var score_button: Button
    private val TAG="MainActivity"

    private val quizViewModel: QuizViewModel by lazy {
        ViewModelProviders.of(this)[QuizViewModel::class.java]
    }


    override fun onCreate(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 = false
            checkAnswer(true)
            closeButton()
        })

        false_button.setOnClickListener(View.OnClickListener {
            quizViewModel.buttonStatus = false
            checkAnswer(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()
        })
    }

    override fun onStart() {
        super.onStart()
        Log.e(TAG, "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.e(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.e(TAG, "onPause")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.e(TAG,"onSaveInstanceState")
        //保存问题索引值
       outState.putInt(KEY_INDEX,quizViewModel.index)
    }

    override fun onStop() {
        super.onStop()
        Log.e(TAG, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "onDestroy")
    }

    private fun updateQuestion() {
        var textId = quizViewModel.questionIndex
        questionText.setText(textId)
    }

    private fun checkAnswer(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()

        }

    }

    private fun closeButton() {
        if (!quizViewModel.buttonStatus) {
            ture_button.isEnabled = quizViewModel.buttonStatus
            false_button.isEnabled = quizViewModel.buttonStatus
        }
    }

    private fun openButton() {
        if (!quizViewModel.buttonStatus) {
            ture_button.isEnabled = true
            false_button.isEnabled = true
        }
    }

    private fun settlementScore() {
        //答完所有题结算成绩
        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()
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153

QuizViewModel

package com.example.geoquiz

import androidx.lifecycle.ViewModel

class QuizViewModel : ViewModel() {
    var index = 0
    private var 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 = 0
    val listSize = questionList.size

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

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

    fun nextQuestion() {
        index = (index + 1) % questionList.size
    }

    fun lastQuestion() {
        //       if (index == 0) {
//            index = questionList.size - 1
//        } else {
//            index -= 1
//        }
        index = if (index == 0) questionList.size - 1 else index - 1
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Question

package com.example.geoquiz

import androidx.annotation.StringRes

data class Question(@StringRes val questionID: Int, val answer: Boolean) {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

知识点

ViewModelProviders类提供了ViewModelProvider实例,调用ViewModelProviders.of(this)创建并返回一个关联了MainActivity的ViewModelProvider实例,ViewModelProvider会提供ViewModel实例给MainActivity

activity被销毁的两种情况:

  1. 用户结束使用activity
  2. 因设备配置发送改变时系统销毁
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/83514
推荐阅读
相关标签
  

闽ICP备14008679号