Using Android TextWatcher with multiple user input values and Update thses values -
i new android. try develop android application total amount of user input items. here sketch of application.enter image description here
user must enter first row col1 , col 2. first row col3 can enter or not. in sub 1 textview total value of first row should display. value should display in result textview. likewise if user inserts data second row, values must enter under col1 , col2. if user likes, can enter value under col 3. second row total value should display in sub 2 textview. if user enter values second row, result textview should automatically update total value of sub 1 , sub 2 textviews. try follows, cannot find correct way this. please if helps me solve this, thankful. java class
public class addtwo extends appcompatactivity { edittext edit1, edit2, edit3; edittext edit4, edit5, edit6; textview textviewsub1, textviewsub2, textviewresult; /** * called when activity first created. */ @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.activity_add_two); /*first row variables*/ edit1 = (edittext) findviewbyid(r.id.edittext1); edit2 = (edittext) findviewbyid(r.id.edittext2); edit3 = (edittext) findviewbyid(r.id.edittext3); textviewsub1 = (textview) findviewbyid(r.id.textviewsub1); /*second row variables*/ edit4 = (edittext) findviewbyid(r.id.edittext5); edit5 = (edittext) findviewbyid(r.id.edittext6); edit6 = (edittext) findviewbyid(r.id.edittext7); textviewsub2 = (textview) findviewbyid(r.id.textviewsub2); /*final total textview variable*/ textviewresult = (textview) findviewbyid(r.id.textview_result); //use textwatcher edit1 in first row col1 edit1.addtextchangedlistener(new textwatcher() { public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } public void ontextchanged(charsequence s, int start, int before, int count) { //textviewresult.settext(addnumbers()); } public void aftertextchanged(editable s) { // todo auto-generated method stub } }); //use textwatcher first first row col2 edit2.addtextchangedlistener(new textwatcher() { public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } public void ontextchanged(charsequence s, int start, int before, int count) { } public void aftertextchanged(editable s) { // todo auto-generated method stub //textviewresult.settext(addnumbers()); } }); //use textwatcher first first row col3 edit3.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void ontextchanged(charsequence s, int start, int before, int count) { } @override public void aftertextchanged(editable editable) { textviewresult.settext(addnumbers()); } }); } //define method addstring() private string addnumbers() { //declaring variables int number1, number2; double num1, num2; double d1, d2; double da1, da2; double ordertotal1 = null, ordertotal2 = null; double ordtot1 = null, ordtot2 = null; decimalformat df = new decimalformat("0.00##"); //for first row /*if first , second edittext fields in first row filled*/ if ((edit1.gettext().tostring() != "" && edit1.gettext().length() > 0) && (edit2.gettext().tostring() != "" && edit2.gettext().length() > 0)) { num1 = double.parsedouble((edit1.gettext().tostring())); number1 = integer.parseint(edit2.gettext().tostring()); ordtot1 = num1 * number1; textviewsub1.settext(df.format(ordtot1)); /*else if first,secondn third (all) edittext fields in first row filled*/ } else if ((edit1.gettext().tostring() != "" && edit1.gettext().length() > 0) && (edit2.gettext().tostring() != "" && edit2.gettext().length() > 0) && (edit3.gettext().tostring() != "" && edit3.gettext().length() > 0)) { num1 = double.parsedouble((edit1.gettext().tostring())); number1 = integer.parseint(edit2.gettext().tostring()); d1 = double.parsedouble(edit3.gettext().tostring()); da1 = 100 - d1; ordertotal1 = ((da1 * number1) * num1) / 100; textviewsub1.settext(df.format(ordertotal1)); } else { number1 = (int) 0.00; num1 = 0.00; da1 = 0.00; } //for second row /*if first , second edittext fields in second row filled*/ if ((edit4.gettext().tostring() != "" && edit4.gettext().length() > 0) && (edit5.gettext().tostring() != "" && edit5.gettext().length() > 0)) { num2 = double.parsedouble((edit4.gettext().tostring())); number2 = integer.parseint(edit5.gettext().tostring()); ordtot2 = num2 * number2; textviewsub2.settext(df.format(ordtot2)); /*else if first,secondn third (all) edittext fields in second row filled*/ } else if ((edit4.gettext().tostring() != "" && edit4.gettext().length() > 0) && (edit5.gettext().tostring() != "" && edit5.gettext().length() > 0) && (edit6.gettext().tostring() != "" && edit6.gettext().length() > 0)) { num2 = double.parsedouble((edit1.gettext().tostring())); number2 = integer.parseint(edit2.gettext().tostring()); d2 = double.parsedouble(edit3.gettext().tostring()); da2 = 100 - d2; ordertotal2 = ((da2 * number2) * num2) / 100; textviewsub2.settext(df.format(ordertotal2)); } else { number2 = (int) 0.00; num2 = 0.00; da2 = 0.00; } //return statement return df.format((ordertotal1) + (ordertotal2)); } }
hope help.
/** * called when activity first created. */ @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.activity_add_two); /*first row variables*/ edit1 = (edittext) findviewbyid(r.id.edittext1); edit2 = (edittext) findviewbyid(r.id.edittext2); edit3 = (edittext) findviewbyid(r.id.edittext3); textviewsub1 = (textview) findviewbyid(r.id.textviewsub1); /*second row variables*/ edit4 = (edittext) findviewbyid(r.id.edittext5); edit5 = (edittext) findviewbyid(r.id.edittext6); edit6 = (edittext) findviewbyid(r.id.edittext7); textviewsub2 = (textview) findviewbyid(r.id.textviewsub2); /*final total textview variable*/ textviewresult = (textview) findviewbyid(r.id.textview_result); edit1.addtextchangedlistener(new lashcustomtextwatcher()); edit2.addtextchangedlistener(new lashcustomtextwatcher()); edit3.addtextchangedlistener(new lashcustomtextwatcher()); edit4.addtextchangedlistener(new lashcustomtextwatcher()); edit5.addtextchangedlistener(new lashcustomtextwatcher()); edit6.addtextchangedlistener(new lashcustomtextwatcher()); } public class lashcustomtextwatcher implements textwatcher{ @override public void beforetextchanged(charsequence charsequence, int i, int i1, int i2) { } @override public void ontextchanged(charsequence charsequence, int i, int i1, int i2) { textviewresult.settext(lashcalculate()); } @override public void aftertextchanged(editable editable) { } } public string lashcalculate(){ //declaring variables double row1_value = 0; double row2_value = 0; decimalformat df = new decimalformat("0.00##"); //calculate first row if (!edit1.gettext().tostring().equals("") && !edit2.gettext().tostring().equals("")){ double num1 = double.parsedouble((edit1.gettext().tostring())); double num2 = double.parsedouble((edit2.gettext().tostring())); row1_value = num1*num2; double num3 = 0; if (!edit3.gettext().tostring().equals("")){ num3 = double.parsedouble((edit3.gettext().tostring())); row1_value = (((100 - num3) * num2) * num1)/100; } textviewsub1.settext(df.format(row1_value)); } //calculate second row if (!edit4.gettext().tostring().equals("") && !edit5.gettext().tostring().equals("")){ double num4 = double.parsedouble((edit4.gettext().tostring())); double num5 = double.parsedouble((edit5.gettext().tostring())); row2_value = num4*num5; double num6 = 0; if (!edit6.gettext().tostring().equals("")){ num6 = double.parsedouble((edit6.gettext().tostring())); row2_value = (((100 - num6) * num5) * num4)/100; } textviewsub2.settext(df.format(row2_value)); } return df.format(row1_value + row2_value); }
Comments
Post a Comment