Android开发初体验

创建项目geoquiz
选择EmptyActivity
编程语言选择Kotlin

修改app/src/main/java/MainActivity.kt

package com.bignerdranch.android.geoquiz

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : AppCompatActivity() {

    private lateinit var trueButton:Button
    private lateinit var falseButton:Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        trueButton = findViewById(R.id.true_button)
        falseButton = findViewById(R.id.false_button)

        trueButton.setOnClickListener{
            view: View ->
            //do something
            Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show()
        }

        falseButton.setOnClickListener{
            view:View->
            //do somethng
            Toast.makeText(this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show()
        }
    }
}

修改app/src/main/res/layout/activity_main.xml




    
    

        

修改app/src/main/res/values/strings.xml


    GeoQuiz
    问题1
    
    
    正确
    错误

设置AVD模拟器,运行项目

添加更多的题目

新增app/src/main/java/Question.kt

package com.bignerdranch.android.geoquiz

import androidx.annotation.StringRes

data class Question(@StringRes val textResId:Int,val answer:Boolean)

修改app/src/main/res/layout/activity_main.xml





    
    

        

修改app/src/main/res/values/strings.xml


    GeoQuiz
    问题1
    问题2
    问题3
    问题4
    下一题
    
    
    正确
    错误

修改app/src/main/java/MainActivity.kt

package com.bignerdranch.android.geoquiz

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : AppCompatActivity() {

    private lateinit var trueButton:Button
    private lateinit var falseButton:Button
    private lateinit var nextButton:Button
    private lateinit var questionTextView: TextView

    private val questionBank = listOf(
        Question(R.string.question_1,true),
        Question(R.string.question_2,false),
        Question(R.string.question_3,true),
        Question(R.string.question_4,false),
    )

    private var currentIndex = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        trueButton = findViewById(R.id.true_button)
        falseButton = findViewById(R.id.false_button)
        nextButton = findViewById(R.id.next_button)
        questionTextView = findViewById(R.id.question_text_view)

        trueButton.setOnClickListener{
            view: View ->
            //Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show()
            checkAnswer(true)
        }

        falseButton.setOnClickListener{
            view:View->
            //Toast.makeText(this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show()
            checkAnswer(false)
        }

        nextButton.setOnClickListener{
            currentIndex = (currentIndex + 1) % questionBank.size
            updateQuestion()
        }

        updateQuestion()

    }

    private fun updateQuestion(){
        val questionTextResId = questionBank[currentIndex].textResId
        questionTextView.setText(questionTextResId)
    }

    private fun checkAnswer(userAnswer:Boolean){
        val correctAnswer = questionBank[currentIndex].answer
        val messageResId = if(userAnswer == correctAnswer){
            R.string.correct_toast
        }else{
            R.string.incorrect_toast
        }

        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show()
    }
}

运行项目

添加图标资源

Activity的生命周期

引入Jetpack库的ViewModel

为什么需要ViewModel

Jetpack库

jetpack官方

第6章 创建第二个Activity

你可能感兴趣的:(Android,android)