简体   繁体   中英

Android onClickListener doesn't work for second time

When user clicks on button named calButton in my code (which has setOnClickListener method) for second time, it doesn't work and it just shows last results. here's my code:

public void calculate(View view){
    //Identify IDs
    TextView delta = findViewById(R.id.delta);
    Button calButton = findViewById(R.id.calculatebtn);
    EditText textA = findViewById(R.id.editTextA);
    EditText textB = findViewById(R.id.editTextB);
    EditText textC = findViewById(R.id.editTextC);

    TextView root1 , root2;
    root1 = findViewById(R.id.root1);
    root2 = findViewById(R.id.root2);
    //Return text to float
    float a , b , c;
    a = Float.parseFloat(textA.getText().toString());
    b = Float.parseFloat(textB.getText().toString());
    c = Float.parseFloat(textC.getText().toString());
    //Calculate DELTA
    float deltaF = (b * b) - (4 * a * c);
    //Set Texts and Calculate Roots
    calButton.setOnClickListener(v -> {
        delta.setText("Delta : " + deltaF);
        delta.setAlpha(1);

        if (deltaF < 0) {
            root1.setText("Null");
            root1.setAlpha(1);
        } else if (deltaF == 0) {
            float root = -b / (2 * a);
            root1.setText("Root 1 : " + root);
            root1.setAlpha(1);
        } else if (deltaF > 0) {
            if (b > 0) {
                float bPosetive = (0.0f) - b;
                float root1F = (float) ((bPosetive + Math.sqrt(deltaF)) / (2 * a));
                float root2F = (float) ((bPosetive - Math.sqrt(deltaF)) / (2 * a));
                root1.setText("Root 1 : " + root1F);
                root2.setText("Root 2 : " + root2F);
                root1.setAlpha(1);
                root2.setAlpha(1);
            } else if (b < 0) {
                float bNegetive = (0.0f) - b;
                float root1F = (float) ((bNegetive + Math.sqrt(deltaF)) / (2 * a));
                float root2F = (float) ((bNegetive - Math.sqrt(deltaF)) / (2 * a));
                root1.setText("Root 1 : " + root1F);
                root2.setText("Root 2 : " + root2F);
                root1.setAlpha(1);
                root2.setAlpha(1);
            }
        }
    });


}

I expected that when user edits text and clicks on button for second time, it calculates inputs and show them, but doesn't work and just show last outputs

This is because a , b , c and deltaF is primitive type. These values will not be changed when EditText values have changed.

Also, you shall separate your code logic into different methods. For this example, calculate() to calculate the value and initUI() to init the UI.


Code Examples

public float calculate(float a, float b , float c) {
    return a + b + c;
}

public void initUI() {
    calButton.setOnClickListener(v -> {
       EditText textA = findViewById(R.id.editTextA);
       EditText textB = findViewById(R.id.editTextB);
       EditText textC = findViewById(R.id.editTextC);

       float a , b , c;
       a = Float.parseFloat(textA.getText().toString());
       b = Float.parseFloat(textB.getText().toString());
       c = Float.parseFloat(textC.getText().toString());

       calculate(a, b, c);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM