在这里花了几个小时计算如何改变生成数字的颜色
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
//Test if numbers generated are correct
builder.append(value + " ");
textTest.setText(builder.toString());
NumbersArray[rolls] = value;
System.out.println(NumbersArray[rolls]);
我设法做到了,现在,我有一个问题,他们所有人同时改变颜色,我怎么能这样做所以生成的第一个数字首先亮起,等待几秒然后下一个生成的数字改变颜色? 也许我错了,因为即使没有生成颜色,所有颜色都会改变
if (rolls == 1) {
red.setBackgroundColor(Color.parseColor("#FF0000"));
//Wait t amount of time here
red.postDelayed(new Runnable() {
public void run() {
red.setBackgroundColor(Color.parseColor("#FF7D0000")); /* Changes Color */
}
},3*100 /* millisecond to wait */);
}
else if (rolls == 2) {
yellow.setBackgroundColor(Color.parseColor("#FFF000"));
yellow.postDelayed(new Runnable() {
public void run() {
yellow.setBackgroundColor(Color.parseColor("#FF7F7500"));
}
}, 3 * 100);
}
else if (rolls == 3) {
green.setBackgroundColor(Color.parseColor("#0FFF00"));
green.postDelayed(new Runnable() {
public void run() {
green.setBackgroundColor(Color.parseColor("#FF087D00"));
}
}, 3 * 100 );
}
else {
blue.setBackgroundColor(Color.parseColor("#0000FF"));
blue.postDelayed(new Runnable() {
public void run() {
blue.setBackgroundColor(Color.parseColor("#FF00007D"));
}
}, 3 * 100 );
}
整个循环:
for (int rolls = 0; rolls < 2 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
builder.append(value + " ");
textTest.setText(builder.toString());
NumbersArray[rolls] = value;
System.out.println(NumbersArray[rolls]);
if (value == 1) {
red.setBackgroundColor(Color.parseColor("#FF0000"));
red.postDelayed(new Runnable() {
public void run() {
red.setBackgroundColor(Color.parseColor("#FF7D0000"));
}
},3000);
}
else if (value == 2) {
yellow.setBackgroundColor(Color.parseColor("#FFF000"));
yellow.postDelayed(new Runnable() {
public void run() {
yellow.setBackgroundColor(Color.parseColor("#FF7F7500"));
}
}, 3000);
}
else if (value == 3) {
green.setBackgroundColor(Color.parseColor("#0FFF00"));
green.postDelayed(new Runnable() {
public void run() {
green.setBackgroundColor(Color.parseColor("#FF087D00"));
}
}, 3000 );
}
else {
blue.setBackgroundColor(Color.parseColor("#0000FF"));
blue.postDelayed(new Runnable() {
public void run() {
blue.setBackgroundColor(Color.parseColor("#FF00007D"));
}
}, 3000 );
}
答案 0 :(得分:0)
您可以尝试使用此代码暂停内容更新和检查条件的临时时间。此处理程序将首次运行runnable而不会有任何时间延迟,之后需要1000毫秒才能更新内容并将像循环一样运行。
po[0]