Android how to change contents of edittext in real time -
this question has answer here:
- how use textwatcher class in android? 7 answers
i looking make app converts 1 unit another(say currency). consists of 2 edit-texts. 1 in user enters value , second contains result. now, here, instead of having 'convert' button put value in second edit text, converted value appear in second edit text enter values first one. how can achieve this? thanks
use textwatcher
this. set on edittext
user types in:
myedittext1.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) { string value = s.tostring(); // perform computations using string // example: parse value integer , use value // set computed value other edittext myedittext2.settext(computedvalue); } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(final charsequence s, int start, int before, int count) { } });
edit 1:
check empty string ""
:
myedittext1.addtextchangedlistener(new textwatcher() { @override public void aftertextchanged(editable s) { string value = s.tostring(); if (value.equals("")) { myedittext1.settext("0"); // may not need line, because "myedittext1.settext("0")" // trigger method again , go else block, where, if code set // correctly, myedittext2 value 0. so, try without next line // , if doesn't work, put back. myedittext2.settext("0"); } else { // perform computations using string // example: parse value integer , use value // set computed value other edittext myedittext2.settext(computedvalue); } } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(final charsequence s, int start, int before, int count){ } });
Comments
Post a Comment