If you are making a simple calculator, there are basically no difficulties. For your reference, the specific content is as follows
The steps are to write the interface layout first, make the button layout, font size and color, and then set up the monitor.
Theme using NoTitleBar
The code is as follows:
The code about the interface in activity_main:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android: android:layout_width="fill_parent" android:layout_height="90dp" android:background="@drawable/white_bg" android:editable="false" android:gravity="right|bottom" android:paddingBottom="20dp" android:paddingRight="20dp" android:textSize="50sp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="C" android:gravity="right|bottom" android:textSize="30sp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="DEL" android:gravity="right|bottom" android:textSize="30sp" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="÷" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="×" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="7" android:gravity="right|bottom" android:textSize="30sp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="8" android:gravity="right|bottom" android:textSize="30sp" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="9" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="-" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal" android:gravity="center_horizontal" > <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="4" android:gravity="right|bottom" android:textSize="30sp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="5" android:gravity="right|bottom" android:textSize="30sp" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="6" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android: android:layout_width="80dp" android:layout_height="80dp" android:text="+" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp" android:gravity="center_horizontal"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="80dp" android:layout_height="80dp" android: android:text="1" android:textSize="30sp" android:gravity="right|bottom" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:layout_width="80dp" android:layout_height="80dp" android: android:text="2" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:layout_width="80dp" android:layout_height="80dp" android: android:text="3" android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <Button android:layout_width="170dp" android:layout_height="80dp" android: android:text="0" android:textSize="30sp" android:gravity="right|bottom" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> <Button android:layout_width="80dp" android:layout_height="80dp" android: android:text="." android:textSize="30sp" android:gravity="right|bottom" android:layout_marginLeft="10dp" android:background="@drawable/white_selector" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> </LinearLayout> <Button android: android:layout_width="80dp" android:layout_height="170dp" android:layout_marginLeft="10dp" android:background="@drawable/orange_selector" android:gravity="right|bottom" android:text="=" android:textSize="30sp" android:paddingRight="15sp" android:paddingBottom="15sp" /> </LinearLayout> </LinearLayout>
Mainactivity code:
package ; import ; import ; import .; import ; import ; import ; import ; public class MainActivity extends Activity implements { Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt; Button bt_mul,bt_div,bt_add,bt_sub; Button bt_clr,bt_del,bt_eq; EditText et_input; boolean clr_flag; //Judge whether et is cleared @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); //Instantiate the object setContentView(.activity_main); bt_0= (Button) findViewById(.bt_0); bt_1= (Button) findViewById(.bt_1); bt_2= (Button) findViewById(.bt_2); bt_3= (Button) findViewById(.bt_3); bt_4= (Button) findViewById(.bt_4); bt_5= (Button) findViewById(.bt_5); bt_6= (Button) findViewById(.bt_6); bt_7= (Button) findViewById(.bt_7); bt_8= (Button) findViewById(.bt_8); bt_9= (Button) findViewById(.bt_9); bt_pt= (Button) findViewById(.bt_pt); bt_add= (Button) findViewById(.bt_add); bt_sub= (Button) findViewById(.bt_sub); bt_mul= (Button) findViewById(.bt_mul); bt_div= (Button) findViewById(.bt_div); bt_clr= (Button) findViewById(.bt_clr); bt_del= (Button) findViewById(.bt_del); bt_eq= (Button) findViewById(.bt_eq); et_input= (EditText) findViewById(.et_input); //Set button click 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_pt.setOnClickListener(this); bt_add.setOnClickListener(this); bt_sub.setOnClickListener(this); bt_mul.setOnClickListener(this); bt_div.setOnClickListener(this); bt_clr.setOnClickListener(this); bt_del.setOnClickListener(this); bt_eq.setOnClickListener(this); } @Override public void onClick(View v) { String str=et_input.getText().toString(); switch (()){ case .bt_0: case .bt_1: case .bt_2: case .bt_3: case .bt_4: case .bt_5: case .bt_6: case .bt_7: case .bt_8: case .bt_9: case .bt_pt: if(clr_flag){ clr_flag=false; str=""; et_input.setText(""); } et_input.setText(str+((Button)v).getText()); break; case .bt_add: case .bt_sub: case .bt_mul: case .bt_div: if(clr_flag){ clr_flag=false; str=""; et_input.setText(""); } if(("+")||("-")||("×")||("÷")) { str=(0,(" ")); } et_input.setText(str+" "+((Button)v).getText()+" "); break; case .bt_clr: if(clr_flag) clr_flag=false; str=""; et_input.setText(""); break; case .bt_del: //Judge whether it is empty, and then delete it if(clr_flag){ clr_flag=false; str=""; et_input.setText(""); } else if(str!=null&&!("")){ et_input.setText((0,()-1)); } break; case .bt_eq: //The final result is calculated separately getResult(); break; } } private void getResult(){ String exp=et_input.getText().toString(); if(exp==null||("")) return ; //Because there is no operator, no operation is needed if(!(" ")){ return ; } if(clr_flag){ clr_flag=false; return; } clr_flag=true; //Intercept the string before the operator String s1=(0,(" ")); //Intercepted operator String op=((" ")+1,(" ")+2); //Intercept the string after the operator String s2=((" ")+3); double cnt=0; if(!("")&&!("")){ double d1=(s1); double d2=(s2); if(("+")){ cnt=d1+d2; } if(("-")){ cnt=d1-d2; } if(("×")){ cnt=d1*d2; } if(("÷")){ if(d2==0) cnt=0; else cnt=d1/d2; } if(!(".")&&!(".")&&!("÷")) { int res = (int) cnt; et_input.setText(res+""); }else { et_input.setText(cnt+"");} } //s1 is not empty but s2 is empty else if(!("")&&("")){ double d1=(s1); if(("+")){ cnt=d1; } if(("-")){ cnt=d1; } if(("×")){ cnt=0; } if(("÷")){ cnt=0; } if(!(".")) { int res = (int) cnt; et_input.setText(res+""); }else { et_input.setText(cnt+"");} } //s1 is empty but s2 is not empty else if(("")&&!("")){ double d2=(s2); if(("+")){ cnt=d2; } if(("-")){ cnt=0-d2; } if(("×")){ cnt=0; } if(("÷")){ cnt=0; } if(!(".")) { int res = (int) cnt; et_input.setText(res+""); }else { et_input.setText(cnt+"");} } else { et_input.setText(""); } } }
Please check out the wonderful articles about calculator"Calculator topic", more exciting things are waiting for you to discover!
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.