Java 在Android中,如何在运行时更改按钮的背景色?

Java 在Android中,如何在运行时更改按钮的背景色?,java,android,button,colors,Java,Android,Button,Colors,下面的代码将背景颜色更改为黑色,而不管我将什么颜色作为颜色传递 争论 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b = (Button)findViewById(R.id.Button01); b.setBackgroundColor(R.color.red); } 有什

下面的代码将背景颜色更改为黑色,而不管我将什么颜色作为颜色传递 争论

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    b = (Button)findViewById(R.id.Button01);
    b.setBackgroundColor(R.color.red);
}
有什么帮助吗?

使用以下方法:

getResources().getColor()
例如:

b.setBackgroundColor(getResources().getColor(R.color.red));

我知道这是一个很晚才回答的问题,但我只是偶然发现了你的问题

你可以用

b.setBackgroundColor(android.R.color.white)

b.setBackgroundColor(Color.RED);