C
C
Chvalov2015-09-18 13:46:37
Android
Chvalov, 2015-09-18 13:46:37

How to implement a button with a timer in Android?

There is a button, for example button_1
, you need to implement the following: When pressed, we display the text, after that there is a countdown of 10 seconds and we clear the field with the text.
If 10 seconds have not passed and the user presses the button again, then clear the field and turn off the timer.
Is it possible to do this without the use of handlers and abstruse designs??

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan, 2015-09-18
@LenLord

Set the output of the text and raise the flag that the text is displayed on the screen.
Next, run an asynctask in which to wait 10 seconds in the background and then, if the flag is raised, then clear the field.
If the user presses the button again, then we check the flag, if it is up, then we lower it and remove the text.
You can add a singleton pattern to the task so as not to produce entities when you click multiple times.
Also add to the task that if they try to create a new task again (that is, they displayed the text, removed it and displayed it again), then reset the counter of ten seconds.
Surely there are much more convenient methods, this is what came to mind at the very beginning

O
one pavel, 2015-09-18
@onepavel

developer.android.com/intl/en/reference/android/os...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question