Android - Add a double every time the function is called

0

How do I make a function always add a double value to another, because onClick () only adds up 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);

}
    
asked by anonymous 08.05.2018 / 23:42

1 answer

0

Whenever you click the button it will add up to + b is this? If it is you can not leave the static variable this 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);
}

In this way for example we have:

1 ° Click - 40;

2 ° Click - 80;

3rd Click - 120;

....

    
09.05.2018 / 13:32