Issue
I have a Button that has a few IF statements that change its function depending on which Toggle button is checked. All this is working fine. I now want to create a statement that will change functionality depending on whether a certain button is clicked before it. Any ideas would be great.
Heres the code i have so far for the button
// YellowButton
player1Counter= 0;
player2Counter= 0;
breakCounter= 0;
yellowAdd2 = (Button) findViewById(R.id.yellowButton);
player1 = (ToggleButton) findViewById(R.id.player1Button);
player2 = (ToggleButton) findViewById(R.id.player2Button);
player1ScoreDisplay= (TextView) findViewById(R.id.player1Score);
player2ScoreDisplay= (TextView) findViewById(R.id.player2Score);
breakDisplay= (TextView) findViewById(R.id.breakCounterNum);
yellowAdd2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (toggle1.isChecked()) {
if (player1.isChecked()) {
player1Counter += 2;
breakCounter += 2;
player1ScoreDisplay.setText("" + player1Counter);
breakDisplay.setText("" +breakCounter);
}
if (player2.isChecked()) {
player2Counter += 2;
breakCounter += 2;
player2ScoreDisplay.setText("" + player2Counter);
breakDisplay.setText("" +breakCounter);
}
}
if (toggle2.isChecked()) {
if (player1.isChecked()) {
breakCounter = 0;
breakDisplay.setText("" +breakCounter);
player1.setChecked(false);
player2.setChecked(true);
toggle2.setChecked(false);
toggle1.setChecked(true);
}
else if (player2.isChecked()) {
breakCounter = 0;
breakDisplay.setText("" +breakCounter);
player2.setChecked(false);
player1.setChecked(true);
toggle2.setChecked(false);
toggle1.setChecked(true);
}
}
if (toggle3.isChecked()) {
if (player1.isChecked()) {
breakCounter = 0;
breakDisplay.setText("" +breakCounter);
player1.setChecked(false);
player2.setChecked(true);
}
else if (player2.isChecked()) {
breakCounter = 0;
breakDisplay.setText("" +breakCounter);
player2.setChecked(false);
player1.setChecked(true);
}
}
if (toggle4.isChecked()) {
if (player1.isChecked()) {
player2Counter += 4;
player2ScoreDisplay.setText("" + player2Counter);
breakCounter = 0;
breakDisplay.setText("" +breakCounter);
player2.setChecked(true);
player1.setChecked(false);
toggle4.setChecked(false);
toggle1.setChecked(true);
}
else if (player2.isChecked()) {
player1Counter += 4;
player1ScoreDisplay.setText("" + player1Counter);
breakCounter = 0;
breakDisplay.setText("" +breakCounter);
player1.setChecked(true);
player2.setChecked(false);
toggle4.setChecked(false);
toggle1.setChecked(true);
}
}
}
});
Solution
You can set a flag when the button is pressed:
boolean preButtonWasPressed = false;
// snip
// snap
preButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
preButtonWasPressed = true;
}
});
and later on check this flag:
if ( preButtonWasPressed ) {
// do some stuff
// clear the flag
preButtonWasPressed = false;
}
alternatively, you can change the onClickListener
if a button is pressed:
changeBehaviorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yellowAdd2.setOnClickListener(aDifferentOnClickListener);
}
});
Answered By - MByD
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.