gpt4 book ai didi

java - 如何将数组的值传递给另一个类?

转载 作者:行者123 更新时间:2023-12-01 22:19:31 25 4
gpt4 key购买 nike

我正在尝试将另一个类中的颜色数组值传递给 Android Studio 中的 FunFactsActivity。现在,我收到错误
View 中的 setBackgroundColor(int) 无法应用于 (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类已经在getColor函数中调用Color.parseColor。只需使用该函数,因为它已经获得了随机颜色。

relativeLayout.setBackgroundColor(mColorWheel.getColor());

或者如果您想使用 mColors 数组:

relativeLayout.setBackgroundColor(Color.parseColor(mColorWheel.mColors[currentColor]));

关于java - 如何将数组的值传递给另一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com