0

所以我在 Udacity 上完成了 Google 的 Kotlin 课程的第一课,然后我继续对应用程序进行自己的更改,使它们看起来更好(只是为了学习,我不关心应用程序)。我进行了一些更改,例如添加暗模式和自定义 UI。

我希望暗模式菜单能够正常工作,比如当我点击light mode它时应该重新创建启用亮模式的活动,并且还应该检查亮模式MenutItem。我还希望应用程序记住设置,这样如果我Follow System在应用程序的一个会话期间选择然后关闭应用程序,即使在打开应用程序后它也应该继续跟随系统。我认为这应该完成,SharedPreferences但我不知道如何正确地做到这一点。正如您在下面附加的视频中看到的那样,当我单击任何暗模式设置选项时,它们不会自动更改设置,我需要重新启动应用程序,然后我才能看到匹配的活动中的任何更改在关闭应用程序之前选择的任何选项。

注意:我想提一下,我对 Android 开发或 Kotlin 不是很有经验,我是 2 个月前才开始学习的。我还想提一下,我不关心应用程序本身,但我更愿意使用这个应用程序来学习如何完成工作。如果我不学习如何做这些事情,我不在乎我的应用程序是否具备我希望它拥有的所有功能。我关心的不是应用程序,而是我从中获得的知识,也就是说,如果您有答案,请解释一下它是如何工作的。

此视频显示了应用程序的行为:https ://drive.google.com/file/d/1SnN0r351OGF4xQNCtI1wtgouSFVROKzg/view?usp=sharing

toolbar我在主要(也是唯一的)活动的布局文件中添加了一个。这是布局资源的 xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/diceRollerActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"

    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/activity_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:title="@string/app_name" />

    </com.google.android.material.appbar.AppBarLayout>

    <ImageView
        android:id="@+id/dice_image"
        android:layout_width="275dp"
        android:layout_height="335dp"
        android:contentDescription="@+id/diceImage_imageView_description"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.395"
        app:srcCompat="@drawable/empty_dice" />

    <Button
        android:id="@+id/roll_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/Container_Button_Padding"
        android:text="@string/roll"
        android:textAlignment="center"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="25sp"
        app:backgroundTint="@color/bold_button_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dice_image"
        app:layout_constraintVertical_bias="0.32"
        app:rippleColor="@color/button_ripple" />


</androidx.constraintlayout.widget.ConstraintLayout>

这是我用于同一活动的 Kotlin 代码:

package com.example.diceroller

import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.*
import androidx.appcompat.app.AppCompatDelegate

class MainActivity : AppCompatActivity() {
    lateinit var diceImage: ImageView
    lateinit var sharedPreferences: SharedPreferences
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.activity_toolbar))
        val rollButton: Button = findViewById(R.id.roll_button)
        diceImage = findViewById(R.id.dice_image)
        rollButton.setOnClickListener {
            rollDice()
        }

        sharedPreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE)

        when (sharedPreferences.getString("Night_Mode_State", getString(R.string.follow_system))) {
            getString(R.string.follow_system) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
            getString(R.string.light_mode) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            getString(R.string.dark_mode) -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        }

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        val inflater: MenuInflater = menuInflater
        inflater.inflate(R.menu.actionbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.follow_system -> sharedPreferences.edit().putString("Night_Mode_State", getString(R.string.follow_system)).apply()
            R.id.light_mode -> sharedPreferences.edit().putString("Night_Mode_State", getString(R.string.light_mode)).apply()
            R.id.dark_mode -> sharedPreferences.edit().putString("Night_Mode_State", getString(R.string.dark_mode)).apply()
        }
        return true
    }
    private fun rollDice() {
        when ((1..6).random()) {
            1 -> diceImage.setImageResource(R.drawable.dice_1)
            2 -> diceImage.setImageResource(R.drawable.dice_2)
            3 -> diceImage.setImageResource(R.drawable.dice_3)
            4 -> diceImage.setImageResource(R.drawable.dice_4)
            5 -> diceImage.setImageResource(R.drawable.dice_5)
            else -> diceImage.setImageResource(R.drawable.dice_6)
        }
    }
}

这个要点包括我认为正确回答这个问题所需的其他文件,即:菜单资源文件、颜色资源文件和样式资源文件:https://gist.github.com/sbeve72/36a71919f1f965c5dcd466db1e099b4f.js"

4

0 回答 0