简体   繁体   中英

Android Button background Image changes back to state in XML

I have a simple soundboard which uses Buttons to represent the sounds. When a sound button is clicked. The background image is changed using the view.

Button butt = (Button)view;
butt.setBackgroundResource(buttons_on[media]);

buttons_on is a int[] representing the drawables of the buttons.

This all works perfect, but I also use a FragmentActivity to create a paged App. The App has 4 different pages which you can swipe through. When I change a button using the above code and swipe two pages to the right and than swipe back. The image of the button has changed back to it's default defined in the page.xml.

How can I prevent this behavior?

  • change the Buttons to ImageButtons and setting the src?
  • somehow prevent the page from reloading

Instead of doing that you should look at how to use a StateListDrawable. It will simplify your approach.

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