How to add two floats interacting with the UI?

1

I wanted to add two floats, but I put the first decimal number in EditText and when I add another decimal number, I put it in EditText frame in CheckBox and it continues the same number as I put it in CheckBox .

Example: I placed 1.5

Then when I delete 1.5% of my EdiText and put 1.5 more and mark one of CheckBox It is actually 1.5 instead of getting 3.0 in the result of TextView .

package com.gustavo.sample;

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


public class MainActivity extends ActionBarActivity {

    CheckBox g;
    CheckBox m;
    Button send;
    TextView say;
    EditText num;

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

        bacon();

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String counter = num.getText().toString();

                float counterAsFloat = Float.parseFloat(counter);

                float geof = 0f;
                float matf = 0f;


                  if(g.isChecked()){
                      float res1 = geof += counterAsFloat;
                      say.setText("Geo " + Float.toString(res1));
                  }
                  else if(m.isChecked()){
                      float res2 = matf += counterAsFloat;
                    say.setText("Math " + Float.toString(res2));
                  }

            }
        });
    }
    public void bacon() {
        g = (CheckBox)findViewById(R.id.checkBox1);
        m = (CheckBox)findViewById(R.id.checkBox2);
        send = (Button)findViewById(R.id.button1);
        say = (TextView)findViewById(R.id.textView1);
        num = (EditText)findViewById(R.id.editText1);   
    }

}
    
asked by anonymous 29.09.2014 / 23:52

1 answer

0

You can change your code to:

Layout activity_main:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Digite numero:" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="text" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Geo" />

         <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Math" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="somar" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity Code:

package com.gustavo.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button send;
    TextView say;
    EditText num;
    CheckBox g;
    CheckBox m;
    float geof = 0;
    float matf = 0;

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

        g = (CheckBox) findViewById(R.id.checkBox1);
        m = (CheckBox) findViewById(R.id.checkBox2);
        send = (Button) findViewById(R.id.button1);
        say = (TextView) findViewById(R.id.textView1);
        num = (EditText) findViewById(R.id.editText1);

        send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String counter = num.getText().toString();
                if (counter.equals("")) {
                    Toast.makeText(getApplicationContext(),
                            "Digite um valor numerico", Toast.LENGTH_SHORT)
                            .show();

                } else {
                    float counterAsFloat = Float.parseFloat(counter);
                    if (g.isChecked() && m.isChecked()) {
                        Toast.makeText(getApplicationContext(),
                                "Selecione apenas um checkbox",
                                Toast.LENGTH_SHORT).show();

                    } else if (m.isChecked()) {
                        matf = matf + counterAsFloat;
                        say.setText("Math " + Float.toString(matf));
                    } else if (g.isChecked()) {
                        geof = geof + counterAsFloat;
                        say.setText("Geo " + Float.toString(geof));
                    } else
                        Toast.makeText(getApplicationContext(),
                                "Selecione um checkbox", Toast.LENGTH_SHORT)
                                .show();
                }
            }
        });
    }

}
    
30.09.2014 / 03:50