Keep Pressing A Button So That A Counter Keeps Adding By 1 Every Time
I was thinking if there was a simple way to keep pressing the same button so that the counter keeps adding by 1 every time. So that if the button is clicked two times it will have
Solution 1:
First, make this global within your class:
int counter = 0;
Then implement the button click event:
finalButtonbutton= (Button) findViewById(R.id.Spinbtn);
button.setOnClickListener(newView.OnClickListener() {
publicvoidonClick(View view) {
if(view.getId() == R.id.Spinbtn){
if(counter == 2){
showcalcuation();
}elseif(counter < 2){
counter += 1;
}
}
}
});
Solution 2:
Two problems with your code.
- You reset the counter on each click, it seems
- You have a single equals in your if statement, which is not a comparison
You can try this instead
publicclassMainActivityextendsActivityimplementsView.OnClickListener {
privateint counter;
@OverridepublicvoidonClick(View v) {
if (view.getId() == R.id.Spinbtn) { // change to your button id
counter++;
}
if (counter >= 2) {
showcalcuation();
} else {
// hide calculation?
}
}
...
// in onCreate
button.setOnClickListener(this);
Post a Comment for "Keep Pressing A Button So That A Counter Keeps Adding By 1 Every Time"