Reputation: 775
I am creating a small 'Click Counter' app, which basically has a button function, which when pressed, a textview field displays the number of clicks made. I am trying to setup a 'Reset' button, which changes the value of the textview back to 0. This is the code I have so far:
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txtCount.setText(String.valueOf(0));
}
This changes the textview value to 0, however when the button is clicked again it starts back from the count which it was on when the reset button was clicked.
For example:
a. the current count = 10
b. reset button is selected
c. current count = 0
d. clicker button pressed
e. current count = 11
Am I using the wrong statement or intent?
Upvotes: 0
Views: 4645
Reputation: 1
public void onClick(View view) {
int count = 0;
count++;//increment the count
TextView text = (TextView) findViewById(R.id.counttxt);// resorce location
text.setText("No.of Clicks " + count);// view in the text
Upvotes: 0
Reputation: 117589
You need to save the counter state in an instance field or a static field. Then set that field to zero on reset. For example:
private int counter = 0;
// ...
wreset = (Button) findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
counter = 0;
txtCount.setText(String.valueOf(counter));
}
}
increment = (Button) findViewById(R.id.increment);
increment.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
counter++;
txtCount.setText(String.valueOf(counter));
}
}
Upvotes: 1
Reputation: 76458
Your not resetting whatever counter you are using:
@Override
public void onClick(View arg0) {
yourCounter = 0;
txtCount.setText("0");
}
Upvotes: 5