2017-08-01 180 views
2

下面的代码有效,但我觉得可能会有更高效/更清晰的方式。我是Kotlin和Android开发人员的新手,所以请对我轻松点。 ;-)任何增强将非常赞赏,因为我一直在寻求改进。试图编写高效的代码来使用Kotlin更新背景颜色

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)) 


    } 

} 
+1

是你正在寻找一个代码审查这方面的工作代码?如果是这样,它可能更适合我们的[CodeReview.se]网站。 – EJoshuaS

+1

感谢您的注意。我不知道那是存在的。我会在那里发帖。谢谢! – Dallas

+0

我的第一个想法,顺便说一句,这似乎像Kotlin的'switch'语句的等价物在这里可能更容易阅读。 – EJoshuaS

回答

2

与使用颜色保存字符串不同,保存颜色的十六进制值。然后你可以避免大量的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的一个要点。所以你应该使用'?'如上所示,因为它会运行的代码,如果它不为空,如果它为空,它将不会运行代码(这意味着它不会崩溃)

+0

愚蠢的问题 - 最初以十六进制存储它的最简单方法是什么? – EJoshuaS

+0

非常感谢。你的代码和评论帮了我很多。非常干净!我能够通过一些小的调整来获取代码建议,并在我的应用中使用它。我现在正在寻找!并会尝试修复我的代码的其他部分。 – Dallas

1

您可以使用when而不使用if &其他。这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)) 
    } 

} 
+0

另一件你可以做的事情是使用'when'来设置一个带有颜色的变量,然后在最后调用'fullView !!。setBackgroundColor(ContextCompat.getColor(this,whateverVariable)') - 这是更多的代码行但我认为它更清晰一点,因为这是'when'声明中实际上不同的东西。 – EJoshuaS

+0

感谢您的建议! – Dallas

0

您可以创建一个map这样的:

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