簡體   English   中英

使用 Kotlin 在 Android Studio 中以編程方式更改開關顏色

[英]Change switch color programmatically in Android Studio using Kotlin

我正在嘗試使用 Kotlin 為 Android Studio 中的開關添加顏色 我在這個論壇上嘗試了幾個答案,但無法讓它工作

是否有可能以編程方式完成這項工作?

切換顏色

我修改了答案@vishnu中提到的代碼

完整代碼如下:

class MainActivity : AppCompatActivity() {
    @SuppressLint("SetTextI18n", "ResourceType", "UseSwitchCompatOrMaterialCode")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        switchButton1.setOnCheckedChangeListener{_, isChecked ->
            if(isChecked) {
                switchButton1.text = "Switch 1 ON"
                switchButton1.thumbDrawable.setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN)
                Toast.makeText(this, "First Switch Button: ON", Toast.LENGTH_LONG).show()
            }
            else {
                switchButton1.text = "Switch 1 OFF"
                switchButton1.thumbDrawable.setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN)
                Toast.makeText(this, "First Switch Button: OFF", Toast.LENGTH_LONG).show()
            }
        }
        switchButton2.setOnCheckedChangeListener{_, isChecked ->
            if(isChecked) {
                switchButton2.text = "Switch 2 ON"
                Toast.makeText(this, "Second Switch Button: ON", Toast.LENGTH_LONG).show()
            }
            else {
                switchButton2.text = "Switch 2 OFF"
                Toast.makeText(this, "Second Switch Button: OFF", Toast.LENGTH_LONG).show()
            }
        }
        buttonResetSwitch.setOnClickListener{
            switchButton1.isChecked = false
            switchButton2.isChecked = false
        }
    }
}

加載應用程序時我沒有看到顏色。 這種顏色變化只有在激活開關后才會發生。 如何在加載應用程序時獲得顏色變化(如“關閉”位置所示)

開關顏色變化

您可以將此條件用於 kotlin。 你應該試試這個。

// condiotion while switch is on 
if(mySwitch.isChecked){
    mySwitch.setThumbResource(getColor(R.color.yourcolor))
    mySwitch.setTrackResource(getColor(R.color.yourcolor))
} 
// Condition while switch is off
else {
    mySwitch.setThumbResource(getColor(R.color.yourcolor))
    mySwitch.setTrackResource(getColor(R.color.yourcolor))
}

開關顏色遵循textColor屬性。 您可以使用setSwitchTextAppearance更改textColor

在同一個Switch文檔中, the textAppearance and the related setTypeface() methods control the typeface and style of label text, whereas the switchTextAppearance and the related setSwitchTypeface() methods control that of the thumb.

如果它是一個材料開關

mySwitch.setTrackTintList("your_color")
mySwitch.setThumbTintList("your_color")

或者像這樣

if (isChecked) {
   mySwitch.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN);
} else {
   mySwitch.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM