java - Common onclick listner inside a dialog -


i working on application managing daily expenses. need add feature , user can calculate sum of more expenses under 1 category. [simply when user wants add expense, (lets food) needs calculate expenses food , add total amount food category.

to this, use button. when user tap button,a small calculator pops out.(using dialog).enter image description here

my problem is, want use common onclicklistner buttons inside dialog.how can it.?

here code using

protected void popupcalculator(){     final dialog dialog_cal = new dialog(newexpence.this);     dialog_cal.settitle("calculator");     dialog_cal.setcontentview(r.layout.calculater);      initialisebuttons(dialog_cal);      dialog_cal.show(); }      public void initialisebuttons(dialog d){     num0 = (button) d.findviewbyid(r.id.btn0);      num1 = (button) d.findviewbyid(r.id.btn1);     num2 = (button) d.findviewbyid(r.id.btn2);     num3 = (button) d.findviewbyid(r.id.btn3);     num4 = (button) d.findviewbyid(r.id.btn4);     num5 = (button) d.findviewbyid(r.id.btn5);     num6 = (button) d.findviewbyid(r.id.btn6);     num7 = (button) d.findviewbyid(r.id.btn7);     num8 = (button) d.findviewbyid(r.id.btn8);     num9 = (button) d.findviewbyid(r.id.btn9);     numc = (button) d.findviewbyid(r.id.btnc);     dot = (button) d.findviewbyid(r.id.btndot);     opdiv = (button) d.findviewbyid(r.id.btndiv);     opmul = (button) d.findviewbyid(r.id.btnmulti);     opminus = (button) d.findviewbyid(r.id.btnminus);     opplus = (button) d.findviewbyid(r.id.btnadd);     calculatorvalue = (textview) d.findviewbyid(r.id.value);  }  @override public void onclick(view v) {      if(v.getid()==r.id.calbutton){         popupcalculator();     }    } 

and logic calculator this.it should code segments placed inside onclicklistner

string buttonpressed = ((button) v).gettext().tostring();              if(digits.contains(buttonpressed)){                 if(userisinthemiddleoftypinganumber){                     if (buttonpressed.equals(".") && calculatorvalue.gettext().tostring().contains(".")) {                         // error prevention                         // eliminate entering multiple decimals                     } else {                         calculatorvalue.append(buttonpressed);                     }                 }else{                      if (buttonpressed.equals(".")) {                             // error prevention                             // avoid error if decimal hit before operator, placing leading 0                             // before decimal                          calculatorvalue.settext(0 + buttonpressed);                         } else {                             calculatorvalue.settext(buttonpressed);                         }                     userisinthemiddleoftypinganumber=true;                 }              }         } 

edit

the accepted answer best approach

but have made this..

button [] buttonids = {num0,num1,num2,num3,num4,num5,num6,num7,num8,num9,numc,dot,opdiv,opminus,opmul,opplus};       for(int x=0;x<buttonids.length;x++){         ((button)buttonids[x]).setonclicklistener(this);     } 

both ways correct..

have registered onclicklistener buttons?

you can way

protected void popupcalculator(){    final dialog dialog_cal = new dialog(newexpence.this);    dialog_cal.settitle("calculator");    dialog_cal.setcontentview(r.layout.calculater);     int buttonids[] = { r.id.btn0 , r.id.btn1, r.id.btn2, r.id.btn3,                        r.id.btn4 , r.id.btn5, r.id.btn6, r.id.btn6,                        r.id.btn7 , r.id.btn8, r.id.btn9, r.id.btnc,                        r.id.btndot , r.id.btndiv, r.id.btnmulti,                         r.id.btnminus , r.id.btnadd , r.id.value }     for(int i=0; i<buttonids.length; i++){         dialog_cal.findviewbyid(buttonids[i]).setonclicklistener(this);        // use if implementing view.onclicklistener    }     dialog_cal.show(); }  @override public void onclick(view v) {    if(v.getid() == r.id.calbutton){       popupcalculator();    }    // other buttons chech ids , write respective code } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -