简体   繁体   中英

Android studio ImageButton change from one image to another

I was having some problems with the ImageButton . I have put 3 images in the drawable folder. The images are named e1 , e2 and e3 .

I have made it so that when you click a button the button image changes from e1 to e2 . Can I make it so that the button changes to e3 if its already on e2 ?

Try this

int count = 0;
    e1.setOnClickListener(new View.OnClickListener()   {             
                   public void onClick(View v)  {               
                                 if(count == 0)
                                    {
                                //Set Image 2
                                  count++;
                                   } 
                        else if(count == 1)
                         {
                             // set Image 3
                         }

                   }  
                 });

I think the easiest way is to handle this manually in the button click event.

Set the current drawble id for example in the button tag (button.setTag), and check the tag value on click (getTag()) to change it to another value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM