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 ...
– Dibai
@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 ?
– Dibai
It was just then @bigown accidentally clicked on the reverse button...
– Dibai
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
andmatf
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 thisgeof
andmat
should be class fields and not local variables, just a kick.– Maniero
If I did that I would store the value I put in
EditText
and then add up to another value that I put onEditText
? @mustache– Dibai
I don’t really know if it adds up why I asked here.
– Dibai
@bigown You are correct, the variables
geof
andmatf
should have been declared as instance and not local. Andres1
andres2
are unnecessary.– Piovezan
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.
– Maniero