我想傳遞一個數組值到另一個類的顏色到Android Studio中的FunFactsActivity。現在,我得到一個錯誤setBackgroundColor(int) in View cannot be applied to (java.lang.String).
relativeLayout.setBackgroundColor(mColorWheel.mColors[currentColor]);
如何將數組的值傳遞給另一個類?
據我瞭解,我不能在一個int進入它,因爲它是一個字符串,但我只是想使顏色我已經有在該數組中,每隔幾秒鐘/間隔就會淡入線程中的relativeLayout背景,並且無法正確顯示。我究竟做錯了什麼?
FunFactsActivity.java
// update background color of relativeLayout every few seconds.
private void updateColor()
{
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
int color = mColorWheel.getColor();
runOnUiThread(new Runnable()
{
@Override
public void run()
{
if (currentColor > mColorWheel.mColors.length - 1)
{
currentColor = 0;
}
relativeLayout.setBackgroundColor(mColorWheel.mColors[currentColor]);
currentColor++;
}// end of run
});
}
ColorWheel.java
public class ColorWheel {
// Member variable (properties about the object)
public String[] mColors = {
"#39add1",
"#3079ab",
"#c25975",
"#e15258",
"#f9845b",
"#838cc7",
"#7d669e",
"#53bbb4",
"#e0ab18",
"#637a91",
"#f092b0",
"#b7c0c7",
"#FAEBD7",
"#00FFFF",
"#7FFFD4",
"#0000FF",
"#8A2BE2",
"#A52A2A",
"#DEB887",
"#5F9EA0",
"#7FFF00",
"#D2691E",
"#6495ED",
"#DC143C",
"#B8860B",
"#A9A9A9",
"#006400",
"#FF8C00",
"#8B0000",
"#FFD700",
"#FF69B4",
"#4B0082",
"#F08080",
"#90EE90",
"#87CEFA",
"#FF4500",
"#DA70D6",
"#FA8072",
"#9ACD32",
"#00FF7F",
"#4682B4",
};
// Method (abilities: things the object can do)
public int getColor(){
String color = "";
// Randomly select a fact
Random randomGenerator = new Random(); // Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mColors.length);
color = mColors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;
}
}
簡單的創建類ColorWheel的對象。 像 'ColorWheel CW =新ColorWheel()' 和訪問諸如 'cw.mColors [I]數組;' – Universe
你爲什麼不只是mColors'的'字符串數組更改爲int數組。例如#39add1會變成0x39add1等 –
嘗試'relativeLayout.setBackgroundColor(Color.parseColor(mColorWheel.mColors [currentColor]));' –