Save values and make a comparison

Asked

Viewed 33 times

1

I’m having a hard time making a comparison logic or something better that you can point out to me.

I’m developing an app that contains several buttons in mega sena game format.

I need to make sure that if a ball is already selected the color changes to green and if it receives a new click goes back to white (I am already able to do this ), but I need that if you already have a ball selected should not have the possibility of it selecting a new.

Imagem da tela de jogo

My code:

public void buttonClick(View button){
    String tag = (String)button.getTag();
    int numeroInteiro = Integer.parseInt(tag);

    switch (numeroInteiro){
        default:
            if (ballsColor()){
                button.setBackgroundResource(R.mipmap.ball_verde);
            }else {
                button.setBackgroundResource(R.mipmap.ball);
            }
            break;
    }
}

private boolean ballsColor(){
    if (count == 0){
        count++;
        return true;
    } else {
        count--;
        return false;
    }
}

Is there any way to save a value and not be overwritten in a new button action?

1 answer

1


What you can do is add a tag for each button, create a variable selectedTag to store the tag from the selected button and use this to make the logic you need. Note the following code.

private String selectedTag = null;

public void buttonClick(View button){
    String tag = (String) button.getTag();

    if(this.selectedTag == null){
        button.setBackgroundResource(R.mipmap.ball_verde);
        selectedTag = tag;
    }else if(this.selectedTag.equals(tag)){
        button.setBackgroundResource(R.mipmap.ball);
        selectedTag = null;
    }
}

I’m considering that the method buttonClick will be used to handle the click of all buttons.

I hope I’ve helped.

  • It worked fine, thank you very much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.