Reputation: 189
first of all, i already tried this: Continuously increase integer value as the button is pressed
But i had 59 errors, yep, 59, and as i used to use Eclipse which told you CLEARLY what kind of error you had, how to fix it, and Android Studio looks that was made for people with experience... I can't even understand what the hell to do, to fix all errors (btw, when i try to fix something i break 10 more somehow).
So... Given a Button and a TextView how do i do to increase the textview (like a Clicker game for example) and make it stop pressing the same button again: And how do i put the intervals between each "click"
TextView score = (TextView) findViewById(R.id.textView);
score.setText(Integer.toString(i));
Button click = (Button) findViewById(R.id.button2);
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
i++;
score.setText(Integer.toString(i));
}
});
By the way... I don't need the solution, i need to understand how exactly Thread or Handlers works, yes everybody will recommend me the Documentation, but i need to see a SIMPLE example explained part by part and i will understand way more than i already do by reading the documentation.
Upvotes: 0
Views: 466
Reputation: 1006644
Given a Button and a TextView how do i do to increase the textview (like a Clicker game for example) and make it stop pressing the same button again: And how do i put the intervals between each "click"
Given your score
and click
widgets from your question:
Step #1: Add a Runnable
field to your activity or fragment. Here, I'll call it incrementer
.
Step #2: Define a static final int DELAY
field in your activity or fragment, with the delay period you want ("intervals") in milliseconds.
Step #3: Have your Button
use postDelayed()
and removeCallbacks()
, based on the state of incrementer
:
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (incrementer==null) {
incrementer=new Runnable() {
@Override
public void run() {
i++;
score.setText(Integer.toString(i));
v.postDelayed(incrementer, DELAY);
}
};
incrementer.run();
}
else {
v.removeCallbacks(incrementer);
incrementer=null;
}
}
}
The incrementer
field serves two roles. It tracks whether we are incrementing the TextView
content or not, and it is the actual code that does the incrementing.
If incrementer
is null
, we are not presently incrementing the TextView
. So, we assign incrementer
a Runnable
that can increment the TextView
. The Runnable
also calls postDelayed()
to say "hey, run this Runnable
again after DELAY
milliseconds". We run()
the Runnable
ourselves the first time, to both populate the TextView
at the outset and to trigger the postDelayed()
call to schedule the next increment.
That will then continue to "loop" (run()
calling postDelayed()
, scheduling a future call to run()
) until the user clicks the button again. Then, we see that incrementer
is not null
, so we must be incrementing the TextView
and need to stop. removeCallbacks()
unschedules the last postDelayed()
call, stopping the "loop". We set incrementer
to null
mostly to prepare ourselves for the next button click.
Upvotes: 1