What is the “state_focused” state for a Button?

前端 未结 5 1044
我在风中等你
我在风中等你 2020-12-13 01:00

I would like a Button background to remain a certain color after the button is clicked, and change colors again when some other button is pressed. I thought this was the \"

5条回答
  •  一个人的身影
    2020-12-13 01:15

    To change a Button's background color and make it persistent after being tapped you simply have to:

    1. Create a ColorStateSelector XML file
    2. Set the "backgroundTint" property of the button to the aforementioned XML file
    3. Set the button's state to the one desired and defined in the ColorState file

    As an example in Kotlin and Material Buttons:

    ColorStateSelector file (res/color/buttons_color_state.xml):

    
    
    
        
        
    
        
        
    
        
        
    
    
    

    Layout XML:

    
    

    Kotlin file:

    val buttonOption = view?.findViewById(R.id.buttonOption)
    
    // Implement any "if" checks or other control checks here if necessary
      buttonOption.isSelected = true
    // now the button is Green!
    
    

    Best of lucks!

提交回复
热议问题