0

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.

Community
  • 1
  • 1
Fabian Montossi
  • 189
  • 3
  • 16
  • Start with the first error. What is it? What line of code causes it? – Code-Apprentice May 14 '17 at 22:22
  • "when i try to fix something i break 10 more somehow" this is a common experience for all programmers, even the most experienced of us – Code-Apprentice May 14 '17 at 22:23
  • "i need to understand how exactly Thread or Handlers works" Threads are used for take which take a long time to finish, such as reading a file or downloading an image. Handlers are for communicating between threads. You do not need either of these to accomplish the task at hand. – Code-Apprentice May 14 '17 at 22:25
  • Refer back to my first comment and [edit] your question to show what errors you get. Solving compiler errors is part of the learning process. – Code-Apprentice May 14 '17 at 22:27

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491