Android 应用程序,根据图案更改按钮颜色?
Android App, Changing Button Colors from Pattern?
我正在尝试制作一个类似于 Simon Says 的应用程序。我希望每个按钮在播放序列时依次亮起然后变暗。现在,只有当它是序列中的最后一个按钮时,它才会使按钮变亮然后变暗。当单击的按钮不是序列中的最后一个时,它将保持 Alpha 为 1。
This GIF is me playing the first couple turns.
这是我的代码:
public void playSequence() {
for (int i = 0; i < sequence.size(); ++i) {
spot = 0;
cbutton = (Button) sequenceb.get(sequence.get(i));
cbutton.setAlpha((float) 1.0);
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
cbutton.setAlpha((float) 0.35);
}
}, 500);
}
}
我不知道你们是否需要从我的代码中看到任何其他内容,因为我认为这只是一个逻辑问题。变量 'sequence' 是一个 ArrayList,其中包含引用序列中哪个图块的整数,'sequenceb' 是一个 ArrayList,其中引用了面板中的按钮。
此外,我对 android 编程还很陌生,所以这段代码可能是多余的,或者真的很糟糕哈哈。
我最终让它工作了,尽管这可能是一种糟糕的工作方式。仅供参考
public void playSequence() {
System.out.println("Sequence Size: " + sequence.size() + ", Spot: " + spot);
spot = 0;
cbutton = (Button) sequenceb.get(sequence.get(spot));
LightRunnable lRun = new LightRunnable(10);
Thread lt = new Thread(lRun);
lt.start();
toggleButtons(true);
}
public class DarkRunnable implements Runnable {
private int var;
public DarkRunnable(int var) {
this.var = var;
}
@Override
public void run() {
try {
++spot;
Thread.sleep(250);
cbutton.setAlpha((float) 0.35);
System.out.println("Darkening Tile: " + cbutton.getTag());
Thread.sleep(10);
fixAlphas((float) 0.35);
if(spot < sequence.size()) {
cbutton = (Button) sequenceb.get(sequence.get(spot));
LightRunnable lRun = new LightRunnable(10);
Thread lt = new Thread(lRun);
lt.start();
}
} catch(Exception e) {}
}
}
public class LightRunnable implements Runnable {
private int var;
public LightRunnable(int var) {
this.var = var;
}
@Override
public void run() {
try {
Thread.sleep(250);
cbutton.setAlpha((float) 1.0);
System.out.println("Lightening Tile: " + cbutton.getTag());
DarkRunnable dRun = new DarkRunnable(10);
Thread dt = new Thread(dRun);
dt.start();
} catch (Exception e) {}
}
}
我正在尝试制作一个类似于 Simon Says 的应用程序。我希望每个按钮在播放序列时依次亮起然后变暗。现在,只有当它是序列中的最后一个按钮时,它才会使按钮变亮然后变暗。当单击的按钮不是序列中的最后一个时,它将保持 Alpha 为 1。
This GIF is me playing the first couple turns.
这是我的代码:
public void playSequence() {
for (int i = 0; i < sequence.size(); ++i) {
spot = 0;
cbutton = (Button) sequenceb.get(sequence.get(i));
cbutton.setAlpha((float) 1.0);
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
cbutton.setAlpha((float) 0.35);
}
}, 500);
}
}
我不知道你们是否需要从我的代码中看到任何其他内容,因为我认为这只是一个逻辑问题。变量 'sequence' 是一个 ArrayList,其中包含引用序列中哪个图块的整数,'sequenceb' 是一个 ArrayList,其中引用了面板中的按钮。
此外,我对 android 编程还很陌生,所以这段代码可能是多余的,或者真的很糟糕哈哈。
我最终让它工作了,尽管这可能是一种糟糕的工作方式。仅供参考
public void playSequence() {
System.out.println("Sequence Size: " + sequence.size() + ", Spot: " + spot);
spot = 0;
cbutton = (Button) sequenceb.get(sequence.get(spot));
LightRunnable lRun = new LightRunnable(10);
Thread lt = new Thread(lRun);
lt.start();
toggleButtons(true);
}
public class DarkRunnable implements Runnable {
private int var;
public DarkRunnable(int var) {
this.var = var;
}
@Override
public void run() {
try {
++spot;
Thread.sleep(250);
cbutton.setAlpha((float) 0.35);
System.out.println("Darkening Tile: " + cbutton.getTag());
Thread.sleep(10);
fixAlphas((float) 0.35);
if(spot < sequence.size()) {
cbutton = (Button) sequenceb.get(sequence.get(spot));
LightRunnable lRun = new LightRunnable(10);
Thread lt = new Thread(lRun);
lt.start();
}
} catch(Exception e) {}
}
}
public class LightRunnable implements Runnable {
private int var;
public LightRunnable(int var) {
this.var = var;
}
@Override
public void run() {
try {
Thread.sleep(250);
cbutton.setAlpha((float) 1.0);
System.out.println("Lightening Tile: " + cbutton.getTag());
DarkRunnable dRun = new DarkRunnable(10);
Thread dt = new Thread(dRun);
dt.start();
} catch (Exception e) {}
}
}