java - Switch statement in non-anonymous private OnClickListener class not working? -
i've tried quite few different tactics achieve desired result, nothing making these buttons they're 'sposed to... have 14 buttons. 4 text "x", digitone, digittwo, digitthree , digitfour. then, there 10 "1", "2", etc, named "one", "two", etc. buttons tied same onclicklistener use switch statement determine button pressed, find soonest display button (buttons marked "x"), , change buttons text entered digit. want happen is:
say clicks "5" button. if first button pressed, first "digit" button change displaying "x" "5", , so-on, so-forth. not happening... in fact, nomatter i've tried, nothing happening. not error... error nice, @ least i'd know logical flaw -_-. here's code:
the button declarations:
one=(button)findviewbyid(r.id.button1); two=(button)findviewbyid(r.id.button2); three=(button)findviewbyid(r.id.button3); four=(button)findviewbyid(r.id.button4); five=(button)findviewbyid(r.id.button5); six=(button)findviewbyid(r.id.button6); seven=(button)findviewbyid(r.id.button7); eight=(button)findviewbyid(r.id.button8); nine=(button)findviewbyid(r.id.button9); zero=(button)findviewbyid(r.id.button0); add=(button)findviewbyid(r.id.buttonadd); digitone=(button)findviewbyid(r.id.number1); digittwo=(button)findviewbyid(r.id.number2); digitthree=(button)findviewbyid(r.id.number3); digitfour=(button)findviewbyid(r.id.number4); one.setonclicklistener(listener); two.setonclicklistener(listener); three.setonclicklistener(listener); four.setonclicklistener(listener); five.setonclicklistener(listener); six.setonclicklistener(listener); seven.setonclicklistener(listener); eight.setonclicklistener(listener); nine.setonclicklistener(listener); zero.setonclicklistener(listener);
the onclicklistener private inner class (i guess that's you'd call it. it's inside activity class):
private onclicklistener listener = new onclicklistener(){ public void onclick(view button) { switch(button.getid()){ case r.id.button0: addanumber(0); break; case r.id.button1: addanumber(1); break; case r.id.button2: addanumber(2); break; case r.id.button3: addanumber(3); break; case r.id.button4: addanumber(4); break; case r.id.button5: addanumber(5); break; case r.id.button6: addanumber(6); break; case r.id.button7: addanumber(7); break; case r.id.button8: addanumber(8); break; case r.id.button9: addanumber(9); break; } } };
and finally, "addanumber" method being called:
public void addanumber(int i){ if(digitone.gettext()=="x"){ digitone.settext(i); }else if(digittwo.gettext()=="x"){ digittwo.settext(i); }else if(digitthree.gettext()=="x"){ digitthree.settext(i); }else if(digitfour.gettext()=="x"){ digitfour.settext(i); } }
i've done before... know i'm missing blatantly stupid deserves smack in head...
before all:
digitone.gettext()=="x"
should "x".equals(digitone.gettext())
you need checking string equality in terms of content, not in term of reference.
nothing happens because ==
none of if
condition evaluated true
, addanumber()
results empty method
Comments
Post a Comment