android - I need to make a simple TextWatcher calculator if it's possible -
i want create app contain 3 edittext. (nothing else)
edittext1
edittext2
edittext3
now
1) if enter values edittext1 , edittext2 addition should performed between et1 , et2 answer should set edittext3.... (et1+et2=et3)
2) if enter values edittext1 , edittext3 subtraction should performed between et1 , et3 answer should set edittext2.... (et3-et1=et2)
it's sounds crazy think, "not done before! not mean not done ever."
@confused soul , found solution crazy question...
xml edittext
<edittext android:id="@+id/et_a" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="0.16" android:gravity="center" android:inputtype="number" /> <edittext android:id="@+id/et_b" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="0.16" android:gravity="center" android:inputtype="number" /> <edittext android:id="@+id/et_c" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="0.16" android:gravity="center" android:inputtype="number"/> </linearlayout>
code have done solution...
public class mainactivity extends appcompatactivity { edittext et_a, et_b,et_c; string wherechanging = "a"; string wherefocusing = "a"; string wherelosefocusing = "b"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); et_a = (edittext) findviewbyid(r.id.et_a); et_b = (edittext) findviewbyid(r.id.et_b); et_c = (edittext) findviewbyid(r.id.et_c); et_a.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if(hasfocus) { wherefocusing = "a"; log.e("focus", "a focusing...."); }else{ wherelosefocusing = "a"; log.e("losefocus", "a lose it's focus...."); } } }); et_b.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if(hasfocus) { wherefocusing = "b"; log.e("focus", "b focusing...."); }else{ wherelosefocusing = "b"; log.e("losefocus", "b lose it's focus...."); } } }); et_c.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if(hasfocus) { wherefocusing = "c"; log.e("focus", "c focusing...."); }else{ wherelosefocusing = "c"; log.e("losefocus", "c lose it's focus...."); } } }); et_a.addtextchangedlistener(new textwatcher() { public void ontextchanged(charsequence s, int start, int before, int count) { } public void beforetextchanged(charsequence s, int start, int count,int after) { if(wherefocusing.equals("a")){ wherechanging = "a"; } } public void aftertextchanged(editable s) { if(wherefocusing.equals("a") && wherelosefocusing.equals("b")) { try { if (wherechanging.equals("a")) { log.e("typing", "a typing...."); if (s.length() > 0) { int a_input = integer.parseint(s.tostring()); string b_text = et_b.gettext().tostring(); int b_input = 0; if (!b_text.isempty()) { b_input = integer.parseint(b_text); } et_c.settext((a_input + b_input) + ""); } } } catch (exception exception) { log.e("err", exception.tostring() + ""); } }else if(wherefocusing.equals("a") && wherelosefocusing.equals("c")) { try { if (wherechanging.equals("a")) { log.e("typing", "a typing...."); if (s.length() > 0) { int a_input = integer.parseint(s.tostring()); string c_text = et_c.gettext().tostring(); int c_input = 0; if (!c_text.isempty()) { c_input = integer.parseint(c_text); } et_b.settext((c_input-a_input)+""); } } } catch (exception exception) { log.e("err", exception.tostring() + ""); } } } }); et_b.addtextchangedlistener(new textwatcher() { public void ontextchanged(charsequence s, int start, int before, int count) { } public void beforetextchanged(charsequence s, int start, int count,int after) { if(wherefocusing.equals("b")){ wherechanging = "b"; } } public void aftertextchanged(editable s) { try{ if(wherechanging.equals("b")) { log.e("typing", "b typing...."); if(s.length()>0) { int b_input = integer.parseint(s.tostring()); string a_text = et_a.gettext().tostring(); int a_input = 0; if (!a_text.isempty()) { a_input = integer.parseint(a_text); } et_c.settext((a_input + b_input)+""); } } }catch(exception exception){ log.e("err", exception.tostring()+""); } } }); et_c.addtextchangedlistener(new textwatcher() { public void ontextchanged(charsequence s, int start, int before, int count) { } public void beforetextchanged(charsequence s, int start, int count,int after) { if(wherefocusing.equals("c")){ wherechanging = "c"; } } public void aftertextchanged(editable s) { try{ if(wherechanging.equals("c")) { log.e("typing", "c typing...."); if(s.length()>0) { int c_input = integer.parseint(s.tostring()); string a_text = et_a.gettext().tostring(); int a_input = 0; if (!a_text.isempty()) { a_input = integer.parseint(a_text); } et_b.settext((c_input-a_input)+""); } } }catch(exception exception){ log.e("err", exception.tostring()+""); } } }); } }
i try crazy idea , works file..... hope you... idea...i enjoy :)
Comments
Post a Comment