简体   繁体   中英

How to return a button to its default color after being pressed?

I have created a few buttons in my android application. 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; 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. 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.

You can read about them here .

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