One button ends the loop and the other button continues the loop, Cycle Do While

0

My app consists of a cycle "Do While" and two buttons, I want that when I touch the first button (finish), the cycle ends and the code of the loop is executed. But raise the other button (more), the cycle is restarted and the loop is repeated. The app does the action of the button (finish), but when you touch the button (more), the loop does not continue (it does not restart).

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CalcularActivity extends AppCompatActivity {

    EditText nombre_local;
    EditText nombre_visitante;
    EditText cuota_local;
    EditText cuota_visitante;
    EditText cuota_empate;

    Button mas;
    Button terminar;

    TextView fintext1;
    TextView fintext2;
    TextView fintext3;
    TextView fintext4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calcular);

        nombre_local = (EditText) findViewById(R.id.nombre_local);
        nombre_visitante = (EditText) findViewById(R.id.nombre_visitante);
        cuota_local = (EditText) findViewById(R.id.cuota_local);
        cuota_empate = (EditText) findViewById(R.id.cuota_empate);
        cuota_visitante = (EditText) findViewById(R.id.cuota_visitante);

        fintext1 = (TextView) findViewById(R.id.fintext1);
        fintext2 = (TextView) findViewById(R.id.fintext2);
        fintext3 = (TextView) findViewById(R.id.fintext3);
        fintext4 = (TextView) findViewById(R.id.fintext4);

        mas = (Button) findViewById(R.id.mas_partidos);
        terminar = (Button) findViewById(R.id.calcular);

        terminar.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                Button siguiente = mas;

                do {

                    String local_0 = nombre_local.getText().toString();

                    String visita_0 = nombre_visitante.getText().toString();

                    double local = Double.parseDouble(cuota_local.getText().toString());

                    double empate = Double.parseDouble(cuota_empate.getText().toString());

                    double visita = Double.parseDouble(cuota_visitante.getText().toString());

                    double local_1 = 1 / local;
                    double empate_1 = 1 / empate;
                    double visita_1 = 1 / visita;

                    double suma_cuotas = local + empate + visita;

                    double local_2 = suma_cuotas - local;
                    double empate_2 = suma_cuotas - empate;
                    double visita_2 = suma_cuotas - visita;

                    double suma_cuotas2 = local_2 + empate_2 + visita_2;

                    double local_3 = local_2 / suma_cuotas2;
                    double empate_3 = empate_2 / suma_cuotas2;
                    double visita_3 = visita_2 / suma_cuotas2;


                    double local_4 = local_1 - local_3;
                    double empate_4 = empate_1 - empate_3;
                    double visita_4 = visita_1 - visita_3;

                    //VALOR ABSOLUTO
                    double local_5 = Math.abs(local_4);
                    double empate_5 = Math.abs(empate_4);
                    double visita_5 = Math.abs(visita_4);

                    fintext1.setText("\nPROBABILIDADES:\n" + local_0 + ":\nC.A__" + (String.format("%.2f", (local_1 * 100))) + "% , EST.__" + (String.format("%.2f", (local_3 * 100))) + "%\nEmpate:\nC.A__" + (String.format("%.2f", (empate_1 * 100))) + "% , EST.__" + (String.format("%.2f", (empate_3 * 100))) + "%\n" + visita_0 + ":\nC.A__" + (String.format("%.2f", (visita_1 * 100))) + "% ,EST.__" + (String.format("%.2f", (visita_3 * 100))) + "%");

                    fintext2.setText("\nPRONOSTICO:");



                    if ((local_4 - empate_5) > 0.11) {
                        fintext3.setText("\nGANA: " + local_0);
                    } else if ((visita_4 - empate_5) > 0.11) {
                        fintext3.setText("\nGANA: " + visita_0);
                    } else if ((local_4 - empate_5) < 0.11 && (local_4 - empate_5) > 0 && ((local_4 + empate_4) > (empate_4 + visita_4))) {
                        fintext3.setText("\nGANA O EMPATA: " + local_0);
                    } else if ((visita_4 - empate_5) < 0.11 && (visita_4 - empate_5) > 0 && ((visita_4 + empate_4) > (empate_4 + local_4))) {
                        fintext3.setText("\nGANA O EMPATA: " + visita_0);
                    } else {
                        fintext3.setText("\nPartido muy parejo: EMPATE");
                    }

                    fintext4.setText("_______________");


                } while (siguiente == terminar);

            }

        });

    }

}
    
asked by Jorge Luis Stark 28.07.2018 в 04:27
source

1 answer

0

Your processing logic should not be done at all times, this generates unnecessary resource expenditure.

When should it be processed?

When the entries in your process change, in your case when at least one of the EditText values change, or when you press the mas button. And it should no longer be processed when the finish button is pressed.

To know when there is a change we use the TextWatcher class, so using the above logic we get the following:

public class CalcularActivity extends AppCompatActivity implements TextWatcher{

    EditText nombre_local;
    EditText nombre_visitante;
    EditText cuota_local;
    EditText cuota_visitante;
    EditText cuota_empate;

    Button mas;
    Button terminar;

    TextView fintext1;
    TextView fintext2;
    TextView fintext3;
    TextView fintext4;

    boolean isEnabled = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calcular);

        nombre_local = (EditText) findViewById(R.id.nombre_local);
        nombre_visitante = (EditText) findViewById(R.id.nombre_visitante);
        cuota_local = (EditText) findViewById(R.id.cuota_local);
        cuota_empate = (EditText) findViewById(R.id.cuota_empate);
        cuota_visitante = (EditText) findViewById(R.id.cuota_visitante);

        fintext1 = (TextView) findViewById(R.id.fintext1);
        fintext2 = (TextView) findViewById(R.id.fintext2);
        fintext3 = (TextView) findViewById(R.id.fintext3);
        fintext4 = (TextView) findViewById(R.id.fintext4);

        mas = (Button) findViewById(R.id.mas_partidos);
        terminar = (Button) findViewById(R.id.calcular);

        cuota_local.addTextChangedListener(this);
        cuota_empate.addTextChangedListener(this);
        cuota_visitante.addTextChangedListener(this);

        mas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isEnabled = true;
                doProcess();
            }
        });

        terminar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isEnabled = false;
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        doProcess();
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }

    void doProcess() {
        if (isEnabled) {
            String local_0 = nombre_local.getText().toString();

            String visita_0 = nombre_visitante.getText().toString();

            double local = Double.parseDouble(cuota_local.getText().toString());

            double empate = Double.parseDouble(cuota_empate.getText().toString());

            double visita = Double.parseDouble(cuota_visitante.getText().toString());

            double local_1 = 1 / local;
            double empate_1 = 1 / empate;
            double visita_1 = 1 / visita;

            double suma_cuotas = local + empate + visita;

            double local_2 = suma_cuotas - local;
            double empate_2 = suma_cuotas - empate;
            double visita_2 = suma_cuotas - visita;

            double suma_cuotas2 = local_2 + empate_2 + visita_2;

            double local_3 = local_2 / suma_cuotas2;
            double empate_3 = empate_2 / suma_cuotas2;
            double visita_3 = visita_2 / suma_cuotas2;


            double local_4 = local_1 - local_3;
            double empate_4 = empate_1 - empate_3;
            double visita_4 = visita_1 - visita_3;

            //VALOR ABSOLUTO
            double local_5 = Math.abs(local_4);
            double empate_5 = Math.abs(empate_4);
            double visita_5 = Math.abs(visita_4);

            fintext1.setText("\nPROBABILIDADES:\n" + local_0 + ":\nC.A__" + (String.format("%.2f", (local_1 * 100))) + "% , EST.__" + (String.format("%.2f", (local_3 * 100))) + "%\nEmpate:\nC.A__" + (String.format("%.2f", (empate_1 * 100))) + "% , EST.__" + (String.format("%.2f", (empate_3 * 100))) + "%\n" + visita_0 + ":\nC.A__" + (String.format("%.2f", (visita_1 * 100))) + "% ,EST.__" + (String.format("%.2f", (visita_3 * 100))) + "%");

            fintext2.setText("\nPRONOSTICO:");


            if ((local_4 - empate_5) > 0.11) {
                fintext3.setText("\nGANA: " + local_0);
            } else if ((visita_4 - empate_5) > 0.11) {
                fintext3.setText("\nGANA: " + visita_0);
            } else if ((local_4 - empate_5) < 0.11 && (local_4 - empate_5) > 0 && ((local_4 + empate_4) > (empate_4 + visita_4))) {
                fintext3.setText("\nGANA O EMPATA: " + local_0);
            } else if ((visita_4 - empate_5) < 0.11 && (visita_4 - empate_5) > 0 && ((visita_4 + empate_4) > (empate_4 + local_4))) {
                fintext3.setText("\nGANA O EMPATA: " + visita_0);
            } else {
                fintext3.setText("\nPartido muy parejo: EMPATE");
            }

            fintext4.setText("_______________");
        }
    }
}
    
answered by 28.07.2018 / 06:29
source