简体   繁体   English

如何在xml / java中使按钮颜色可变(android studio)

[英]how to make button color variable in xml/java (android studio)

I am very very sorry if this question is too broad to answer in specific code, but i am just starting out and want to know how to tackle a specific function. 如果这个问题过于广泛而无法在特定代码中回答,我感到非常抱歉,但是我只是刚开始,想知道如何解决特定功能。 I have been trying to set a buttons xml color as a variable the same way that i am setting it below, where i set the button id in my layout to the variable Team1button then use that to set its text. 我一直在尝试将按钮xml颜色设置为变量,就像我在下面进行设置一样,在我的布局中将按钮ID设置为变量Team1button,然后使用它来设置其文本。

Team1button = findViewById(R.id.button); Team1button = findViewById(R.id.button);

String Teamname = "tree"; 字符串Teamname =“ tree”;

Team1button.setText(Teamname); Team1button.setText(团队名称);

Essentially i want to do the same thing as .setText, but with color because i am wanting color to change through other events happening in my code. 本质上,我想与.setText做相同的事情,但是要使用颜色,因为我希望颜色可以通过代码中发生的其他事件来改变。 In the end i want it to be the user choosing the color of each button, but that is a different question i will answer myself after being able to make the buttons color changeable in either xml or java. 最后,我希望用户选择每个按钮的颜色,但这是一个不同的问题,在能够使按钮的颜色在xml或java中可变之后,我将回答自己。 If you know what type of function i would need to do in either my layout files or my .java files it would be greatly appreciated if you gave me any input on how to do this. 如果您知道我需要在布局文件或.java文件中执行哪种类型的功能,那么如果您提供有关如何执行此操作的任何输入,将不胜感激。 This is for school project. 这是用于学校项目。

Check out Button class documentation : https://developer.android.com/reference/android/widget/Button.html 查看Button类文档: https : //developer.android.com/reference/android/widget/Button.html

Basically, if I understend you correctly you want to call : 基本上,如果我正确地理解了您,您可以致电:

button.setBackgroundColor(int colorID);

eg 例如

button.setBackgroundColor(Color.RED);

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

相关问题 Android Studio (Java):直接使用 java 更改按钮的颜色 not.xml - Android Studio (Java): Changing Color of a Button directly with java not .xml 如何在Android Studio的xml中使用java中的变量? - How to use a variable in java within xml in Android Studio? 如何使 Java 从 XML 文件中调用字符串(在 Android Studio 中) - How to make the Java to call a String from a XML file(In Android Studio) Android Studio Java:如何使按钮中断while循环 - Android Studio Java: How to make a button break a while loop 当按钮具有特定的当前文本时,如何更改按钮上的文本(Android Studio / Java和XML) - How to change text on button when the button has a particular current text (Android Studio/Java&XML) 如何使用JAVA更改android中按钮的颜色? - How to change the color of a button in android using JAVA? 如何在 Android Studio 中制作“如果单击按钮,则执行此操作”? - How to make 'if button clicked, do this' in Android Studio? 如何在Android Studio中使TextView充当按钮? - How to make a TextView act as a button in Android Studio? 如何使用 java 代码而不是 xml 代码将按钮添加到 android 工作室中的另一个活动 - How do I add button to another activity in android studio using java code rather than xml code 有一个带有 3 个不同背景图像的按钮。 Android Studio、XML、Java - Have a Button with 3 different background images. Android Studio, XML, Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM