[英]How to return a button to its default color after being pressed?
I have created a few buttons in my android application. 我在Android应用程序中创建了几个按钮。 When they are pressed the color changes to green and purple.
按下它们时,颜色变为绿色和紫色。 I have also created a reset button that when pressed will return the pressed buttons to their original or default color.
我还创建了一个重置按钮,按下该按钮会将按下的按钮恢复为原始或默认颜色。
How do I get the default color to appear? 如何显示默认颜色?
first get the default background of Button b; 首先得到Button b的默认背景; using
运用
Drawable d = b.getBackground();
then set another background of your choice 然后设置您选择的另一个背景
b.setBackgroundResource(R.drawable.custom_button1);
if you need default background again use this 如果您需要再次使用默认背景,请使用此
b.setBackgroundDrawable(d);
manage your code using these lines. 使用这些行管理您的代码。
it will helpful to you. 它会对你有所帮助。
Android provides state list drawables which can be defined via xml files. Android提供状态列表drawables,可以通过xml文件定义。 You can define various states like default, pressed, focused, etc in your state list drawable xml file and then apply that xml to the view as a background resource.
您可以在状态列表drawable xml文件中定义各种状态,如default,pressed,focused等,然后将该xml作为后台资源应用于视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.