I have a problem that I cannot solve. I have a button that, when clicked, changes the text view. Then it activates the process postdelayed, which returns textviewto the source text after 2 seconds.
If I press the button once, and then again during this 2-second interval, it postdelaywill continue to count down from the first press and not restart from the second press. This causes the source text to appear when I want the text to be changed.
Each time the button is pressed, a delay is created from this instance. I want him to undo the previous one postdelayand launch a new one. This is my code so far, but it is not finished because I cannot figure out how to finish it (therefore it does not work).
p1AddL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter1 ++;
count1 ++;
Handler h = new Handler();
if ('PREVIOUS_DELAY_HAS_STARTED') {
h.removeCallbacks(clickButton);
h.postDelayed(clickButton, 2000);
} else {
h.postDelayed(clickButton, 2000);
}
if (count1 > 0) {
lifepointsP1.setText("+" + count1);
lifepointsP1.setTextColor(Color.argb(220, 0, 188, 0));
}
}
});
Runnable clickButton = new Runnable() {
@Override
public void run() {
count1 = 0;
lifepointsP1.setTextColor(Color.WHITE);
lifepointsP1.setText(counter1);
}
};
the text PREVIOUS_DELAY_HAS_STARTEDshould be some kind of verification method, and I'm sure that I need something between the teams h.removeCallbacksand h.postDelayedin this text.
If this is an easier way / better way to write this method to make it work, please let me know. I have tried so many ways, and I feel that I am very close here.
source
share