Reputation: 49
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:
can you help me? thank you very much
Upvotes: 0
Views: 1042
Reputation: 64
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);
Percent.addTextChangedListener(percentWatcher);
mmolGlic.addTextChangedListener(mmolGlicTextWatcher);
mgGlic.addTextChangedListener(mgGlicWatcher);
}
public void frommMol() {
if (!mmolGlic.getText().toString().trim().isEmpty()) {
mmol = Double.parseDouble(mmolGlic.getText().toString());
perc = (mmol / 10.929) + 2.15;
Percent.removeTextChangedListener(percentWatcher);
Percent.setText(String.format("%.2f", perc));
Percent.addTextChangedListener(percentWatcher);
}
}
public void fromPercent() {
if (!Percent.getText().toString().trim().isEmpty()) {
perc = Double.parseDouble(Percent.getText().toString().trim());
mmol = (perc - 2.15) * 10.929;
mmolGlic.removeTextChangedListener(mmolGlicTextWatcher);
mgGlic.removeTextChangedListener(mgGlicWatcher);
mmolGlic.setText(String.format("%.2f", mmol));
mg = (perc * 28.7) - 46.7;
mgGlic.setText(String.format("%.2f", mg));
mmolGlic.addTextChangedListener(mmolGlicTextWatcher);
mgGlic.addTextChangedListener(mgGlicWatcher);
}
}
public void frommg() {
if (!mgGlic.getText().toString().trim().isEmpty()) {
mg = Double.parseDouble(mgGlic.getText().toString());
perc = (mg + 46.7) / 28.7;
Percent.removeTextChangedListener(percentWatcher);
Percent.setText(String.format("%.2f", perc));
Percent.addTextChangedListener(percentWatcher);
}
}
private TextWatcher percentWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
fromPercent();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private TextWatcher mgGlicWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
frommg();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private TextWatcher mmolGlicTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
frommMol();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
}
Hope this will help you.
Upvotes: 4
Reputation: 516
well what it looks like, that you are doing your operation even if you don't have any text to work with.. i would recommend, do a null/empty check on the editTextView before you do thefromPercent(); operation. Hope that helps.
Upvotes: 0