java - How to perform one operation at time android? -
i making android calculator. made functions first problem double clickable operation.i tried couldn't. when click 2 operations consequently both appearing on text view. i'll post code if can solve double clickable operation problem please post code on comment.
public class mainactivity extends appcompatactivity { private textview _screen; private textview _result; private string display = ""; private string currentoperator; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); _screen = (textview) findviewbyid(r.id.txtscreen); _result = (textview) findviewbyid(r.id.id_result); } public void updatescreen() { _screen.settext(display); } public void onclicknumber(view v) { button b = (button) v; display += b.gettext(); updatescreen(); } public void onclickoperator(view v) { button b = (button) v; display += b.gettext(); currentoperator = b.gettext().tostring(); updatescreen(); } public void clear() { display = ""; currentoperator = ""; } public void onclickclear(view view) { clear(); updatescreen(); } public double operate(string a, string op, string b) { switch (op) { case "+": return double.valueof(a) + double.valueof(b); case "-": return double.valueof(a) - double.valueof(b); case "*": return double.valueof(a) * double.valueof(b); case "/": return double.valueof(a) / double.valueof(b); default: return -1; } } public void onclickequal(view v) { string[] operation = display.split(pattern.quote(currentoperator)); _result.settext(string.valueof(operate(operation[0], currentoperator, operation[1]))); } }
what understand question regarding double operation let suppose performing operation of + , when click again + gets displayed 2 times on textview (as method updatescreen suggest). if problem, can mentioned below
public void updatescreen() { if (screen.gettext().tostring().equalsignorecase("")) { screen.settext(display); } else if (!display.equalsignorecase(screen.gettext().tostring())) { screen.settext(""); screen.settext(display); } else { //this case means operator set on text } }
hope helps you.
Comments
Post a Comment