How to add two floats interacting with the UI?

Asked

Viewed 495 times

1

I wanted to add two floats, but I put the first decimal number on EditText and when I’m going to add one more decimal number, I put it in EditText milestone in the CheckBox and he continues the same number I put on CheckBox.

Example: I put 1.5

That’s when I erase the 1.5 of mine EdiText and put another 1.5 and mark one of the CheckBox, It’s actually 1.5 instead of 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);   
    }

}
  • Blz vlw fixed the bugs ...

  • @bigown you know what’s wrong with my code so it’s because I’m trying to add this way but I don’t know if there are other ?

  • It was just then @bigown accidentally clicked on the reverse button...

  • I’m trying to understand this logic, it seems something very strange. I don’t know the Android UI and its events system but for me it is strange you initialize geof and matf and then try to accumulate on it. It seems obvious to me that it will always zero and before making any sum, so it will always give the initial result and not the sum. But there may be something specific about technology that I don’t know is different. It seems to me that this geofand mat should be class fields and not local variables, just a kick.

  • If I did that I would store the value I put in EditText and then add up to another value that I put on EditText ? @mustache

  • I don’t really know if it adds up why I asked here.

  • @bigown You are correct, the variables geof and matf should have been declared as instance and not local. And res1 and res2 are unnecessary.

  • I didn’t understand your goal and how the whole code is but I think it would solve the problem. Did you try now and get what you wanted? And yes, these variables are unnecessary.

Show 3 more comments

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();
                }
            }
        });
    }

}
  • Actually I don’t want to just add up two numbers I want to add up how many numbers I want more whenever I add a number on EditText it will add up to something else and show in TextView.

  • 1

    @Gustavodibai actually looks at his question... How to add two floats interacting with the UI? I believe your question is answered

  • @Gustavodibai you had marked as answered earlier but if you want to add more numbers and only do what was said below as an example after the code: for example add 3+4+5 ... put 3 checkbox check box then uncheck put 4 then you can put 5 and mark the checkbox again and press send

  • Beauty I will test the code if it works I mark the answer as right ;)

  • And on my question she’s right because the two floats I’m wanting to add up is geof and matf ...

  • @Gustavodibai you don’t need 2 variables to add....

  • Yes I know but these two variables will add to the value I put in EditText and mark on CheckBox and click on Button

  • But in reality your code doesn’t give me the option to choose the CheckBox because when I click the button on your code you are already redirecting me to the first CheckBox.

  • @Gustavodibai I edited does not need these checkbox are useless... I showed the layout I created.. it is not very beautiful but only for test... then it is only you to put the number and then click on add and so on...

  • more bro you’re not understanding my app needs these CheckBox for you to select which option you want to put the value.

  • and then the value gets wrinkled inside that checkbox...

  • fixed copies the code to see if it meets what you want... the layout and mainactivity

  • Our brother needed it bad...

  • @Gustavodibai was a pleasure to help I’ll give +1 in your post.

Show 9 more comments

Browser other questions tagged

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