简体   繁体   English

Android Button背景图片更改回XML状态

[英]Android Button background Image changes back to state in XML

I have a simple soundboard which uses Buttons to represent the sounds. 我有一个简单的音板,它使用Button来代表声音。 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. buttons_on是一个int[]表示按钮的可绘制对象。

This all works perfect, but I also use a FragmentActivity to create a paged App. 这一切都完美, 我也使用FragmentActivity创建页面应用程序。 The App has 4 different pages which you can swipe through. 该应用程序有4个不同的页面,您可以在其中滑动。 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. 按钮的图像已更改回page.xml中定义的默认图像。

How can I prevent this behavior? 如何防止这种行为?

  • change the Buttons to ImageButtons and setting the src? 将按钮更改为ImageButtons并设置src?
  • somehow prevent the page from reloading 以某种方式阻止页面重新加载

Instead of doing that you should look at how to use a StateListDrawable. 而不是这样做,您应该看看如何使用StateListDrawable。 It will simplify your approach. 它将简化您的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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