2015-08-16 61 views
2

我的计算器应用程序中有很多按钮。我正在测试只有一个按钮启动,该按钮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在activity_main.xml中

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

    @Override 
    public void onClick(View v) { 

     result.append("1"); 
    } 
}); 

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" /> 

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

+0

在第一页你是选择一些颜色和应用的颜色按钮在第二项活动中对吗? – Shriram

+0

哪里是blueTheme按钮?在与你的按钮相同的活动? – user3091574

+0

@Shiriram是的,你是对的,首先应用某种颜色,然后再应用另一种颜色。 @ user3091574,blueTheme按钮处于不同的活动状态,因为我在'R.color.red'颜色获取错误时无法解析我的按钮'one' –

回答

1

问题是从一个活动的点击不能通过其他活动,除非你通过它。

在与蓝色主题按钮活动

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); 

    } 

}); 

在你OtherActivity.class

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); 
     } 

    } 

} 
+0

在我的其他活动。类我得到这些错误:在该行 \t 多个标记 - 在型意图的方法getIntExtra(字符串,整数)不适用于参数 \t(字符串) \t - 颜色不能得到解决 和我与blueTheme按钮我得到这些错误行为: 多个标记在该行 \t - 构造意图(新View.OnClickListener(){},类)是 \t未定义 \t - 构造意图(新的View.OnClickListener(){},Class )是 \t undefined –

+0

您只需提供要返回的默认值并确保这是活动。我会更新回答 –

+0

好吧,等你来更新答案。 –

0

使用此:

// 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)); 
+0

。 –

+0

转到资源 - > values - > colors.xml并定义你的颜色:#9b1717然后使用R.color.DarkRed – Ehsan

+0

值中没有colors.xml –

0

如果你想设置背景颜色,而无需使用预先定义的颜色资源,做到像这样

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

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

+0

不工作,没有颜色变化。 –

+0

@ShahbazTalpur哦,为了改变另一项活动的颜色,Nick的答案是正确的。去吧。 – DavidH

相关问题