I am new to the whole two days to finally get the fix.
1. First, register in the OnCreate function in mainactivity
2. Press the + or-sign to divide into two large strings S1 and S2
Then divide the S2 into smaller s1 and S2 until S2 does not contain a + or-No.
The idea is this, no recursion, recursive speed is too slow, also not good at recursion.
3. is to find a variety of bugs and decorations
Basically achieve the goal and your own mobile phone on Android's own calculator basically the same.
The project code is as follows:
Link: Link: http://pan.baidu.com/s/1sj65nop Password: 926r
See also use Baidu cloud know I have many dishes ...
The wording is as follows:
Package Com.example.administrator.myapplication;import Android.app.activity;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.util.log;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.edittext;public class Mainactivity extends Activity implements Onclicklistener {private Button bt_0; Private Button bt_1; Private Button bt_2; Private Button bt_3; Private Button Bt_4; Private Button bt_5; Private Button bt_6; Private Button bt_7; Private Button bt_8; Private Button Bt_9; Private Button Bt_point; Private Button Bt_c; Private Button Bt_del; Private Button Bt_minuse; Private Button bt_multipy; Private Button Bt_plus; Private Button bt_divide; Private Button Bt_dengyu; Private EditText Input_text; @Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Register button BT_0 = (button) Findviewbyid (R.ID.BT_0); Bt_1 = (Button) Findviewbyid (r.id.bt_1); Bt_2 = (Button) Findviewbyid (r.id.bt_2); Bt_3 = (Button) Findviewbyid (r.id.bt_3); Bt_4 = (Button) Findviewbyid (r.id.bt_4); Bt_5 = (Button) Findviewbyid (r.id.bt_5); Bt_6 = (Button) Findviewbyid (r.id.bt_6); bt_7 = (Button) Findviewbyid (r.id.bt_7); Bt_8 = (Button) Findviewbyid (r.id.bt_8); Bt_9 = (Button) Findviewbyid (r.id.bt_9); Bt_point = (Button) Findviewbyid (r.id.bt_point); Bt_c = (Button) Findviewbyid (r.id.bt_clear); Bt_del = (Button) Findviewbyid (R.id.bt_del); Bt_minuse = (Button) Findviewbyid (r.id.bt_minuse); bt_multipy = (Button) Findviewbyid (r.id.bt_multipy); Bt_plus = (Button) Findviewbyid (R.id.bt_plus); Bt_divide = (Button) Findviewbyid (r.id.bt_divide); Bt_dengyu = (Button) findviEwbyid (R.id.bt_dengyu); Input_text = (EditText) Findviewbyid (R.ID.EDIT_TEXT1);//Listener Event Bt_0.setonclicklistener (this); Bt_1.setonclicklistener (this); Bt_2.setonclicklistener (this); Bt_3.setonclicklistener (this); Bt_4.setonclicklistener (this); Bt_5.setonclicklistener (this); Bt_6.setonclicklistener (this); Bt_7.setonclicklistener (this); Bt_8.setonclicklistener (this); Bt_9.setonclicklistener (this); Bt_point.setonclicklistener (this); Bt_c.setonclicklistener (this); Bt_del.setonclicklistener (this); Bt_minuse.setonclicklistener (this); Bt_multipy.setonclicklistener (this); Bt_plus.setonclicklistener (this); Bt_divide.setonclicklistener (this); Bt_dengyu.setonclicklistener (this); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Getmenuinflater (). Inflate (R.menu. Menu_main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent Activity in Androidmanifest.xml. int id = item.getitemid (); Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); } @Override public void OnClick (View v) {String str = Input_text.gettext (). toString (); Switch (V.getid ()) {case R.id.bt_0:case r.id.bt_1:case r.id.bt_2:case r.id . bt_3:case r.id.bt_4:case r.id.bt_5:case r.id.bt_6:case r.id.bt_7: Case R.id.bt_8:case R.id.bt_9:input_text.settext (str + (Button) v). GetText ()); Break Case R.id.bt_point:if (!str.contains (".")) Input_text.settext (str + (button) v). GetText ()); Break Case R.id.bt_plus:case r.id.bt_minuse:case r.id.bt_multipy:case r.id.bt_divide: if (Str.equals ("")); else {char a =str.charat (Str.length ()-1); LOG.I ("str", str+ ""); LOG.I ("Last character", A + ""); if (a = = "); else {input_text.settext (str + "" + (Button) v). GetText () + ""); } A =str.charat (Str.length ()-1); Break } break; Case R.id.bt_clear:input_text.settext (NULL); Break Case R.id.bt_del:if (str! = null &&!str.equals ("")) {Input_text.settext (STR.S Ubstring (0, Str.length()-1)); } break; Case R.id.bt_dengyu:getrseult (); Break }}//operation result private void Getrseult () {String str = Input_text.gettext (). toString (); if (str = = NULL | | str.equals (")") return; if (!str.contains ("+") &&!str.contains ("-") &&!str.contains ("X") &&!str.contains ("+")) re Turn Traverse computed result double result = 0; Char a =str.charat (Str.length ()-2); if (A = = ' + ' | | a = = '-') {str = str + ' 0 '; } else if (a = = ' X ' | | a = = ' ÷ ') {str = str + ' 1 '; } int i = 0; int start = 0; char temp = '; int flag = 1; if (!str.contains ("+") &&!str.contains ("-")) {result + = Calculate_include_multy_or_minuse (str); } else {for (; I < str.length (); i++) {char c = str.charat (i); if (c = = ' + ' | | c = = '-') {String S1 = str.substring (start, i); LOG.I ("S1 is", S1 + ""); if (flag = = 0) Result-= Calculate_include_multy_or_minuse (S1); else result + = Calculate_include_multy_or_minuse (S1); start = i + 1; if (c = = '-') flag = 0; else flag = 1; String s2 = str.substring (Start, str.length ()); LOG.I ("S2 is", S2 + ""); if (S2.contains ("+") | | | s2.contains ("-")) {temp = C; Continue } else {if (flag = = 0) {Result-= Calculate_include_ Multy_or_minuse (S2); } else result + = Calculate_include_multy_or_minuse (s2); }}}} int r = 0;//String result_string = DOuble.tostring (result); if ((int) result = = result) {R = (int) result; Input_text.settext (R + ""); } else Input_text.settext (Result + ""); } private double Calculate (String s) {double sum = 1; int start = 0; int flag = 0; int flag2 = 0; s = s + ' x '; char op = '; for (int i = 0; i < s.length (); i++) {char c = s.charat (i); if (c = = ' X ' | | c = = ' ÷ ') {if (Flag2 = = 0) op = c; String S1 = s.substring (start, i); if (flag = = 0) {sum *= double.parsedouble (S1);//LOG.I ("First is", "" +sum); flag = 1; Flag2 = 1; } else {switch (OP) {case ' X '://LOG.I ("S1 is", "" "+s1 ); sum = sum * double.parsedouble (S1);//LOG.I ("Sum is "," "+sum"; Break Case ' ÷ '://LOG.I ("S2 is", "" +s1); sum = sum/double.parsedouble (S1);//LOG.I ("Sum is", "" +sum); Break }} op = C; start = i + 1; }}//log.i ("The result of Sum is", "" + sum); return sum; } private double Calculate_include_multy_or_minuse (String s) {double sum = 0; if (S.contains ("x") | | | s.contains ("÷")) {sum = calculate (s); } else {sum = double.parsedouble (s); } return sum; }}
Android Calculator maker 2. Register the View build function