Android - Add a double every time the function is called

Asked

Viewed 45 times

0

How do I make a function always add a double value to another, since onClick() adds only once.

Mainactivity.java:

Button somar = (Button)findViewById(R.id.btn);
TextView txt = (TextView)findViewById(R.id.txt);

double a = 20;
double b = 20;
double c = a + b;

somar.setOnClickListener(
new OnClickListener(){

public void onClick(View p1){
    txt.setText(c);

}
  • That’s not true. Put your code in so we can take a look.

  • Actually I asked the wrong question. What I want is for a function to always add a double value to another. I will edit the question.

1 answer

0


Every time you click the button, it adds up to a+b, is that it? If it is you can’t leave the static variable that way.

Button somar = (Button)findViewById(R.id.btn);
TextView txt = (TextView)findViewById(R.id.txt);

double a = 20;
double b = 20;
double c = 0;

somar.setOnClickListener(
new OnClickListener(){

public void onClick(View p1){
    c = c + (a+b);
    txt.setText(c);
}

That way for example we have:

1° Click - 40;

2° Click - 80;

3° Click - 120;

....

  • Thanks! Exactly what I needed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.