how to handle with addTextChangedListener and memo

2019-09-14 21:15发布

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:

  1. 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.
  2. 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

2条回答
来,给爷笑一个
2楼-- · 2019-09-14 21:27

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.

查看更多
beautiful°
3楼-- · 2019-09-14 21:46
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.
查看更多
登录 后发表回答