Change color of a radio button

前端 未结 11 1878
死守一世寂寞
死守一世寂寞 2020-12-10 02:11

I am developing an quiz based app. There will be 1 question and 4 option(radio buttons). If user select any wrong answer then I want to turn that radio button color to Red.

11条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 02:42

    For kotlin user

    Create an extension

    fun RadioButton.setCircleColor() {
        val colorStateList = ColorStateList(
            arrayOf(
                intArrayOf(-android.R.attr.state_checked), // unchecked
                intArrayOf(android.R.attr.state_checked)  // checked
            ), intArrayOf(
               Color.RED, // unchecked color
               Color.GREEN  // checked color
            )
        )
    
        // finally set button tint list
        buttonTintList = colorStateList
    
        // optionally tint mode or tint blend
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            buttonTintBlendMode = BlendMode.SRC_IN
        }else{
            buttonTintMode = PorterDuff.Mode.SRC_IN
        }
    
        invalidate() // could not be necessary
    }
    

    Now call it

       radioButton.setCircleColor()
    

    done

提交回复
热议问题