试图编写高效的代码来使用Kotlin更新背景颜色

下面的代码工作,但我觉得有可能是一个更有效/更清洁的方式。 我是Kotlin和Android开发的新手,所以请放轻松点。 ;-)任何增强将非常赞赏,因为我一直在寻求改善。

fun updateBackgroundColor() { val sharedPref = PreferenceManager.getDefaultSharedPreferences(this) // Gets the text color from the shared preferences file val backgroundColor = sharedPref.getString("background_color", "") val fullscreenView = findViewById(R.id.fullscreen_content) val fullView = fullscreenView as TextView? // Changes the text color based on the color the user has selected in Settings/Preferences if (backgroundColor == "blue") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mBlue)) } else if (backgroundColor == "red") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mRed)) } else if (backgroundColor == "green") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mGreen)) } else if (backgroundColor == "yellow") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mYellow)) } else if (backgroundColor == "purple") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mPurple)) } else if (backgroundColor == "pink") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mPink)) } else if (backgroundColor == "black") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mBlack)) } else if (backgroundColor == "white") { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mWhite)) } else { fullView!!.setBackgroundColor(ContextCompat.getColor(this, mBlue)) } } 

保存颜色的hex值,而不是用颜色保存字符串。 那么你可以避免大量的if / else语句。

 fun updateBackgroundColor() { val sharedPref = PreferenceManager.getDefaultSharedPreferences(this) // Gets the text color from the shared preferences file val backgroundColor = sharedPref.getString("background_color_hex", "0x000000") val fullView = findViewById(R.id.fullscreen_content) as TextView? // Changes the text color based on the color the user has selected in Settings/Preferences int color = Color.parseColor(backgroundColor); fullView?.setBackgroundColor(color) } 

此外,您可以将findViewById作为一个oneliner,如上所示。
使用’!!’ 在Kotlin是非常糟糕的,因为它基本上说“我知道这可以是空的,我不在乎,只是崩溃,如果它是空的”,这是使用Kotlin的要点之一。 所以你应该用’?’ 如上所示,因为它会运行的代码,如果它不为空,如果它为空,它将不会运行代码(这意味着它不会崩溃)

你可以使用when而不使用if&else。 这个when就像是Java的switch

 fun updateBackgroundColor() { val sharedPref = PreferenceManager.getDefaultSharedPreferences(this) // Gets the text color from the shared preferences file val backgroundColor = sharedPref.getString("background_color", "") val fullscreenView = findViewById(R.id.fullscreen_content) val fullView = fullscreenView as TextView? // Changes the text color based on the color the user has selected in Settings/Preferences when(backgroundColor) { "blue" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mBlue)) "red" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mRed)) "green" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mGreen)) "yellow" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mYellow)) "purple" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mPurple)) "pink" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mPink)) "black" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mBlack)) "white" -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mWhite)) else -> fullView!!.setBackgroundColor(ContextCompat.getColor(this, mBlue)) } } 

你可以像这样创建一个map

 var map = mapOf("blue" to mBlue, "red" to mRed, "green" to mGreen, ...) fullView!!.setBackgroundColor(ContextCompat.getColor(this, map[backgroundColor]))