简体   繁体   English

如何在按下按钮后将按钮恢复为默认颜色?

[英]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作为后台资源应用于视图。

You can read about them here . 你可以在这里阅读它们。

暂无
暂无

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

相关问题 按下后如何保持按钮状态? - How to retain button state after being pressed? 单击后如何更改按钮的文本颜色,并在第二次单击时恢复为默认值? - How to change text color of button after being click,and revert back to default at second clicked? 按下按钮后如何使程序返回ID? - How to make program return ID after the button will be pressed? 按下回车键时,JFrame中的默认按钮不会触发 - Default button in JFrame is not firing when the enter key is being pressed 如何知道按钮是否被按下或释放? - How to know if a button is being pressed or released? 按下按钮时如何更改按钮文本颜色和背景颜色 - How to change button text color and background color while button is pressed 按下时按钮颜色会发生变化,但是当我进行下一次迭代时,按钮颜色不会变为默认背景颜色 - Button color is changing when pressed but when i go for next iteration that button color is not going to default background color 在android上返回页面-发现这是由于按下了后退按钮 - Page return on android - catch that it was due to the back button being pressed 如果按下浏览器后退按钮,如何防止在Spring MVC中成功登录后返回登录表单? - How to prevent going back to login form after successful login in Spring MVC if browser back button being pressed? 在Android中按下按钮时如何设置单选按钮的文本颜色? - How to set text color of a radio button when a button is pressed in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM