Mike Garo
Mike Garo

Reputation: 5

Preventing Negative Numbers in TextView

I have a TextView that displays a number (in this case, the number is 10).

Now, i have a button so the user has to press that button 10 times in order for the number to hit 0, resulting in moving on to the next level. However,

I can keep hitting that button and it starts its way back into the negatives (-1, -2, etc.).

I tried to think of how I would prevent this but I'm at loss.. any ideas?

-----------------------------------EDIT-----------------------------------------

Okay, so here is my update (I got it to stop at 0):

public void onClick(View v) {
        // TODO Auto-generated method stub

        if (scr >= 1) {

            scr = scr - 1;
           TextView Score = (TextView) findViewById(R.id.Score);
           Score.setText(String.valueOf(scr));
        }

        if (scr == 10) 
        {
               aCounter.start();
        }

        if (scr == 1) 
        {
            aCounter.cancel();
            new AlertDialog.Builder(Tap_Game_Activity.this)
             .setTitle("Congratulations!").setMessage("Go to Level 2?")
             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface AlertDialog, int PositiveButton) {
                  setContentView(R.layout.activity_level02_activity);
              }
               })
             .setNegativeButton("No", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface AlertDialog, int NegativeButton) {
                   }
                   }).show();
        }

    }});
}

(EDIT!): I fixed the AlertDialog by changing the 2 'Else If' statements to just 'If' statements. However, my timer still doesn't work :(, any help?

Upvotes: 0

Views: 3652

Answers (4)

Mehul Ranpara
Mehul Ranpara

Reputation: 4255

int i=10; 

b1.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View arg0) 
                {
                    i--;
                    t1.setText(""+i);
                    int s1=(Integer.parseInt(t1.getText().toString()));
                    if(s1<=0)
                    {
                        b1.setEnabled(false);
                    }

                }
            });

Upvotes: 1

Lucifer
Lucifer

Reputation: 29632

suppose number is the variable you are showing in the TextView, consider following code,

int number = 10; 

if ( number > 0 ) 
{
    yourTextView.setText ( String.valueOf ( number ) ); 
}
else
{
    // do not set text
}

Upvotes: 1

EGHDK
EGHDK

Reputation: 18130

This doesn't seem to have anything to do with Android, but more with Java specifically. You just want to make sure that your code doesn't allow you to get below 0.

Your code probably goes something like this:

if button is hit then subtract 1 from x and setText to x

You want to just throw in something that also checks if number is greater than zero

Like:

If button is hit && x is greater than 0 then subtract 1 from x and setText to x

If that is the case, then I recommend that you step away from the Android platform, and do some Java tutorials and examples.

Upvotes: 0

Chirag
Chirag

Reputation: 56925

Its all about Logic.

You have to check condition if ( YourCount < 0 ) then you have to disable that button or do not set value in textView as per condition.

Upvotes: 0

Related Questions