Java 如何更改按钮的颜色';背景

Java 如何更改按钮的颜色';背景,java,android,xml,Java,Android,Xml,我的计算器应用程序中有许多按钮。我测试只有一个按钮开始,按钮id是“一”,当我点击蓝色主题按钮时,应该会改变颜色。我尝试过以下方法: blueTheme = (Button) findViewById(R.id.blueTheme); blueTheme.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { one.setBackgroundColo

我的计算器应用程序中有许多按钮。我测试只有一个按钮开始,按钮id是“一”,当我点击蓝色主题按钮时,应该会改变颜色。我尝试过以下方法:

blueTheme = (Button) findViewById(R.id.blueTheme);
blueTheme.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        one.setBackgroundColor(Color.argb(175, 144, 202, 249));
        one.setBackgroundColor(Color.parseColor(/*hex code here*/));
        one.setBackgroundColor(Color.BLUE);

    }

});    
似乎什么也做不了。我试图通过另一个活动中的选项更改一个活动中按钮的颜色。这是一个实际的按钮
one

one = (Button) findViewById(R.id.one);
one.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        result.append("1");
    }
});
activity_main.xml中
one
的xml代码:

<Button android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:background="#CCCCCC"
        android:text="1"
        android:textColor="#FF6600"
        android:textSize="50sp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp" />

这个想法是在另一个意图中有一个选项,我可以改变计算器的颜色,但在一个按钮上测试失败,无法继续。谢谢您的时间。

请使用:

// If you're in an activity:
yourButton.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not: 
yourButton.setBackgroundColor(yourButton.getContext().getResources().getColor(R.color.red));

如果要在不使用预定义颜色资源的情况下设置背景色,请按此操作

one.setBackgroundColor(0xFFFF0000); // Red
one.setBackgroundColor(0xFF00FF00); // Green

在这里,0xFF00FF00相当于#ff00ff00(#aarrggbb)

问题在于,除非您将一个活动的点击传递给另一个活动,否则无法传递到另一个活动

在活动中使用蓝色主题按钮

blueTheme.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        //NOTE: Where I've put MainActivity that should actually be the name
        //      of whatever activity this code is nested in
        Intent intent = new Intent(MainActivity.this, OtherActivity.class); //use your real class name
        intent.putExtra(OtherActivity.EXTRA_COLOR, Color.BLUE);
        startActivity(intent);

    }

});  
在您的其他活动中。上课

public class OtherActivity extends Activity {

    public static String EXTRA_COLOR = "EXTRA_COLOR";

    public void onCreate(...) {

        View one = (Button) findViewById(R.id.one);

        //NOTE: if you add singleTop to this activity in the manifest
        //      you might need to do this on onNewIntent

        Intent intent = getIntent();
        if (intent.hasExtra(EXTRA_COLOR)) {
            int color = intent.getIntExtra(EXTRA_COLOR, Color.WHITE);
            one.setBackgroundColor(color);
        }

    }

}

在第一页中,您选择了一些颜色,并应用了第二个活动中按钮的颜色,对吗?蓝色主题按钮在哪里??在与按钮一相同的活动中???@Shiriram是的,你是对的,首先应用一些颜色,然后我应用另一个活动中的另一种颜色@用户3091574,blueTheme按钮与我的按钮
one
处于不同的活动中。我在
R.color.red
中遇到错误。无法解决颜色问题。请转到resource->values->colors.xml并定义颜色,如:#9b1717然后使用R.color.darkredt值中没有colors.xml创建一个:#9b1717这不能解决实际问题question@ShahbazTalpur噢,在另一项活动中改变颜色,尼克的答案是正确的。在我的OtherActivity.class中,我得到了以下错误:此行有多个标记-类型Intent中的getIntExtra(String,int)方法不适用于参数(String)-无法解析颜色,在我的blueTheme按钮活动中,我遇到以下错误:此行有多个标记-构造函数意图(新视图.OnClickListener(){},类)未定义-构造函数意图(新视图.OnClickListener(){},类)如果未定义,您只需提供要返回的默认值,并确保这是活动。我会更新的answer@ShahbazTalpur对于第一个错误,请尝试删除getIntExtra()周围的Color.valueof。对于第二个错误,您必须使用新的意图(MyCurrentActivityName.this、ActivityToBeStarted.class);为了构造正确的IntentDavid,Color不是枚举,所以valueof是错误的,您需要我做的另外两个更改-get int extra的第二个参数(默认值/颜色)和intent中的类名-我的答案已经更新,所以您可以查看嘿,谢谢!它现在工作了,只有一个问题,我不想使用十六进制代码的颜色,因为默认值是不够的