Button - Change background color on click

前端 未结 5 641
再見小時候
再見小時候 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:21

    I know it is very late but I hope some one would find useful. I have very simple solution, I have used in my app and works awesome.

    Let me explain the logic, 1. Keep track of 2 button clicks - previous button clicked and current button clicked. I am using ArrayList 2. for every button click keep updating the previous and current button clicks value in ArrayList. 3. change the background color of previous button clicked. 4. change the background color of current button clicked.

    I hope the logic is simple and straightforward.

    Here is the implementation

    xml

    the OnClick method is defined for every button in XML, in the example it is onClickRosterDay

    Java

     //buttons 
    
        Button rosterMonday;
        Button rosterTuesday;
        Button rosterWednesday;
        Button rosterThursday;
        Button rosterFriday;
        Button rosterSaturday;
    
        //ArrayList to track button clicks
        private ArrayList

提交回复
热议问题