使用 Kotlin 在 Android Studio 中以编程方式更改开关颜色

Change switch color programmatically in Android Studio using Kotlin

我正在尝试使用 Kotlin 为 Android Studio 中的开关添加颜色 我从这个论坛尝试了几个答案,但无法正常工作

是否有可能以编程方式完成这项工作?

Switch color

我修改了我在回答@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
        }
    }
}

我在加载应用程序时没有看到颜色。 这种颜色变化仅在激活开关后发生。 如何在加载应用程序时获得颜色变化(如“关闭”位置所示)

Switch Color Change

开关颜色遵循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.

您可以将此条件用于 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))
}

如果它是一个 material 开关

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