java - Values of EditText fields are changed depends on each other -


i'm trying build temperature converter (f -> c , c -> f).

i have 2 et fields. when user types in one, other displays converted value , vice verse.

i understand similar programs has been build already, couldn't find solution.

it works fine 1 field, app closes when try edit other one.

here piece of code:

public class temp extends activity implements onclicklistener, onfocuschangelistener {  private edittext temp_f, temp_c;      protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     setcontentview(r.layout.temp);      temp_f = (edittext) findviewbyid(r.id.temp_f_inp);     temp_c = (edittext) findviewbyid(r.id.temp_c_inp);      temp_c.setonfocuschangelistener((onfocuschangelistener) this);     temp_f.setonfocuschangelistener((onfocuschangelistener) this);  }  private textwatcher tempc = new textwatcher() {     @override     public void ontextchanged(charsequence s, int start, int before, int count) {          if (temp_c.gettext().length() == 0)          {             temp_f.settext("");         } else {             float convvalue = float.parsefloat(temp_c.gettext()                     .tostring());              conv_f = ((convvalue - 32) * 5 / 9);              temp_f.settext(string.valueof(new decimalformat(                     "##.###").format(conv_f)));         }      }     @override     public void beforetextchanged(charsequence s, int start, int count, int after) {}     @override     public void aftertextchanged(editable s) {      }        };  private textwatcher tempf = new textwatcher() {     @override     public void ontextchanged(charsequence s, int start,int before, int count) {          // todo auto-generated method stub                     if (temp_f.gettext().length() == 0)                      {                         temp_c.settext("");                     } else {                          float convvalue = float.parsefloat(temp_f.gettext()                                 .tostring());                          conv_c = ((convvalue * 9) / 5 + 32);                          temp_c.settext(string.valueof(new decimalformat(                                 "##.###").format(conv_c)));      }     @override     public void beforetextchanged(charsequence s, int start,int count, int after) {}     @override     public void aftertextchanged(editable s) {      } };  @override public void onfocuschange(view v, boolean hasfocus) {         if ((v == findviewbyid(r.id.temp_c_inp)) && (hasfocus==true)) {             temp_c.addtextchangedlistener(tempc);         }          else if((v == findviewbyid(r.id.temp_f_inp)) && (hasfocus==true)){             temp_f.addtextchangedlistener(tempf);         }  } 

it seems ontextchanged still holds values of first et has been modified , when try edit other et fields, throws error.

any appreciated!

thank you!

you try this:

@override  public void onfocuschange(view v, boolean hasfocus) {      if (v.equals(findviewbyid(r.id.temp_c_inp))) {         if(hasfocus){             temp_c.addtextchangedlistener(tempc);         }else{             temp_c.removetextchangedlistener(tempc);         }      }     else if(v.equals(findviewbyid(r.id.temp_f_inp))){         if(hasfocus){              temp_f.addtextchangedlistener(tempf);         }else{              temp_f.removetextchangedlistener(tempf);         }     }  } 

i haven't tried code myself, hope you


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -