[英]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.