Button - Change background color on click

前端 未结 5 644
再見小時候
再見小時候 2021-02-03 10:48

I have 8 buttons in my activity. What I am looking for is, The buttons have a default background and when a button is clicked, the background color should change to some other c

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 11:28

    I suggest you this selector.

    Just create a simple selector.xml file in drawable folder and then add the selector to your button as android:background="@drawable/selector" or by code like this: yourButton.setBackground(getResources().getDrawable(R.drawable.selector));

    selector.xml:

    
    
        
        
        
        
    
    

    The first item is for pressed, the second for focused and the last is for selected state.

提交回复
热议问题