2015-08-16 67 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

相關問題