Im doing speed game where Thread changes focus on 4 different buttons. I want only the thread to change focuses on the buttons. The problem I am having is that when the thread for example chooses blue button to focus and if I press red button then the blue button loses focus. I can see this clearly because my buttons have different pictures when default, pressed and focused. So is there anyway to force keep focus on the button I have chosen even tho I press other button. Here is my code:
Thread:
public void run() {
while (aika > 0 && isKaynnissa()) {
try {
Thread.sleep(aika);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
debug.setText(Integer.toString(aika));
if (isKaynnissa()) {
arvoNappi();
}
}
});
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
if (aika < 550) {
uusiaika = 3;
} else if (aika < 450) {
uusiaika = 2;
} else if (aika < 350) {
uusiaika = 1;
}
aika = aika - uusiaika;
}
aika = aloitusaika;
unfocusButtons();
arvotutLuvut.clear();
}
Random focus chooser:
public void arvoNappi() {
Random random = new Random();
int uusirandom = (int) (4 * random.nextDouble() + 1);
while (uusirandom == painettavaNappi) {
uusirandom = (int) (4 * random.nextDouble() + 1);
}
painettavaNappi = uusirandom;
tvrandom.setText(Integer.toString(painettavaNappi));
if (painettavaNappi == 1) {
arvotutLuvut.add(1);
btnPun.setFocusable(true);
btnPun.requestFocusFromTouch();
} else if (painettavaNappi == 2) {
arvotutLuvut.add(2);
btnVio.setFocusable(true);
btnVio.requestFocusFromTouch();
} else if (painettavaNappi == 3) {
arvotutLuvut.add(3);
btnVih.setFocusable(true);
btnVih.requestFocusFromTouch();
} else {
arvotutLuvut.add(4);
btnSin.setFocusable(true);
btnSin.requestFocusFromTouch();
}
}