我正在做一个android simon说应用程序,我在点亮循环,对于这个例子,我已经定义它,因此它会点亮4种颜色,因为具有点亮相应数字的数字的数组是4个单元格长
不幸的是,它点亮了所有的颜色,同时你无法区分顺序我想弄清楚为什么那是什么以及如何使它一次点亮一种颜色这里是我的主要活动和java calss的代码,这有助于它
这是活动:
package com.gabie212.simonsays;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
public class GameActivity extends AppCompatActivity implements View.OnClickListener {
private int i = 0;
private Thread t = new Thread();
private Button greenButton;
private Button redButton;
private Button blueButton;
private Button yellowButton;
private Button startButton;
private ArrayList<Integer> randomColors = new ArrayList<Integer>();
private ArrayList<Integer> userColors = new ArrayList<Integer>();
private GameManger gm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gm = new GameManger(this);
setContentView(R.layout.activity_game);
greenButton = (Button) findViewById(R.id.btnGreen);
redButton = (Button) findViewById(R.id.btnRed);
blueButton = (Button) findViewById(R.id.btnBlue);
yellowButton = (Button) findViewById(R.id.btnYellow);
startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(this);
greenButton.setOnClickListener(this);
redButton.setOnClickListener(this);
blueButton.setOnClickListener(this);
yellowButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int num;
for (i=0;i<3;i++) {
num = gm.getColor(4);
randomColors.add(num);
}
android.os.Handler handler = new android.os.Handler();
//TODO if the start button is pressed multiple times simultaneously it starts the lightup loop multiple times simultaneously
if (v.getId() == startButton.getId()) {
for (i = 0; i < randomColors.size(); i++) //light up loop
{
switch (randomColors.get(i)) {
case 1:
greenButton.setBackgroundResource(R.drawable.greenlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
greenButton.setBackgroundResource(R.drawable.green);
}
}, 2000);
break;
case 2:
redButton.setBackgroundResource(R.drawable.redlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
redButton.setBackgroundResource(R.drawable.red);
}
}, 2000);
break;
case 3:
blueButton.setBackgroundResource(R.drawable.bluelightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
blueButton.setBackgroundResource(R.drawable.blue);
}
}, 2000);
break;
case 4:
yellowButton.setBackgroundResource(R.drawable.yellowlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
yellowButton.setBackgroundResource(R.drawable.yellow);
}
}, 2000);
break;
}
}
/*
for(i=0;i<randomColors.size();i++)
{
if(v.getId()==greenButton.getId())
{
userColors.add(1);
}
else
{
if(v.getId()==redButton.getId()){
userColors.add(2);
}
else
{
if(v.getId()==blueButton.getId())
{
userColors.add(3);
}
else
{
userColors.add(4);
}
}
}
}
for(i=0;i<randomColors.size();i++)
{
if(randomColors.get(i) != userColors.get(i))
{
Intent i = new Intent( GameActivity.this, GameOverActivity.class);
startActivity(i);
}
}
*/
}
}
}
这是java类:
package com.gabie212.simonsays;
import java.util.Random;
/**
* Created by Ronit on 21/02/2018.
*/
public class GameManger {
private GameActivity gActivity;
static Random rnd = new Random();
public GameManger(GameActivity mA)
{
this.gActivity =mA;
}
public int getColor(int size)
{
return rnd.nextInt(4)+1;
}
}
我遇到问题的部分是这部分:
int num;
for (i=0;i<3;i++) {
num = gm.getColor(4);
randomColors.add(num);
}
android.os.Handler handler = new android.os.Handler();
//TODO if the start button is pressed multiple times simultaneously it starts the lightup loop multiple times simultaneously
if (v.getId() == startButton.getId()) {
for (i = 0; i < randomColors.size(); i++) //light up loop
{
switch (randomColors.get(i)) {
case 1:
greenButton.setBackgroundResource(R.drawable.greenlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
greenButton.setBackgroundResource(R.drawable.green);
}
}, 2000);
break;
case 2:
redButton.setBackgroundResource(R.drawable.redlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
redButton.setBackgroundResource(R.drawable.red);
}
}, 2000);
break;
case 3:
blueButton.setBackgroundResource(R.drawable.bluelightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
blueButton.setBackgroundResource(R.drawable.blue);
}
}, 2000);
break;
case 4:
yellowButton.setBackgroundResource(R.drawable.yellowlightup);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
yellowButton.setBackgroundResource(R.drawable.yellow);
}
}, 2000);
break;
}
正如我提到的那样一次点亮4个按钮而不是按顺序执行它我使用睡眠等功能不起作用我使用的延迟铣刀要么似乎也没有工作,我怎么让它变亮按顺序排列颜色(我认为它使用睡眠功能,但我不确定)。
我认为问题是你一次启动多个协同程序(在for循环中)。我猜你对Runnable接口并不是很熟悉:每次运行runnable时,它都会与你的主进程并行运行,这意味着你的代码无论如何都会继续运行。
它运行
greenButton.setBackgroundResource(R.drawable.greenlightup);
并设置一个计时器来运行延迟方法。但是这个方法正在等待for循环继续,并且
redButton.setBackgroundResource(R.drawable.redlightup);
执行,等等。这就是为什么你的按钮都在(几乎)同时着色的原因。你的延迟方法也在(几乎)同时发射!