public class MainActivity extends AppCompatActivity {
EditText Percent, mmolGlic, mgGlic;
double mmol = 0, mg = 0, perc = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Percent = (EditText) findViewById(R.id.percent);
mmolGlic = (EditText) findViewById(R.id.mmol_glic);
mgGlic = (EditText) findViewById(R.id.mg_glic);
/*mmolGlic.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
frommMol();
}
@Override
public void afterTextChanged(Editable s) {
}
});*/
Percent.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
fromPercent();
}
@Override
public void afterTextChanged(Editable s) {
}
});
/*mgGlic.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
frommg();
}
});*/
}
public void frommMol() {
mmol = Double.parseDouble(mmolGlic.getText().toString());
perc = (mmol/10.929) + 2.15;
Percent.setText(String.format( "%.2f", perc ));
}
public void fromPercent(){
perc = Double.parseDouble(Percent.getText().toString());
mmol = (perc - 2.15) * 10.929;
mmolGlic.setText(String.format( "%.2f", mmol ));
mg = (perc*28.7) - 46.7;
mgGlic.setText(String.format( "%.2f", mg ));
}
public void frommg(){
mg = Double.parseDouble(mgGlic.getText().toString());
perc = (mg + 46.7) / 28.7;
Percent.setText(String.format( "%.2f", perc ));
}
}
Goodmorning everyone :)
this is a continuous question from this one: Question 1
this an example code of what i'm trying to do. But i have some problems. I think most of them are for the logic of variables and how to handle the input in more EditTexts. for example:
- the main problem is that i can't use more than one addTextChangedListener. I try to explain better: if i leave the code as here, the app crashes. I am not sure, maybe it's due to how i handle the three EditTexts.
- then i have a problem when i delete the text: if i have "5.99" and i press del, when it's deleting it deletes until the 5 and then crashes. Probaly i should set when the text field is empty the variables = 0.
can you help me? thank you very much