Android老師佈置的課程作業——簡單計算器 功能要求實現四則運算,參考界面見下圖: 首先給各位老鐵提供apk安裝包以及項目壓縮包,略表誠意~ github地址:https://github.com/wonke/Calculator 使用GirdLayout比較適合計算器這種規則的佈局,每行有幾個 ...
Android老師佈置的課程作業——簡單計算器
功能要求實現四則運算,參考界面見下圖:
首先給各位老鐵提供apk安裝包以及項目壓縮包,略表誠意~
github地址:https://github.com/wonke/Calculator
使用GirdLayout比較適合計算器這種規則的佈局,每行有幾個按鈕幾乎都是固定的,遇到一個按鈕需要占兩個常規按鈕空間時,使用android:layout_columnSpan="2"屬性即可解決。
Layout佈局代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:height="30dp" android:gravity="center" android:text="@string/title" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <GridLayout android:id="@+id/grid_layout" android:layout_width="match_parent" android:layout_height="500sp" android:background="#ece7e7" android:alignmentMode="alignBounds" android:columnCount="4" android:orientation="horizontal" android:rowCount="10" android:useDefaultMargins="false"> <TextView android:id="@+id/contentText" android:background="@drawable/textview_border" android:gravity="end" android:textSize="50sp" android:layout_columnSpan="4" android:layout_columnWeight="1" android:layout_rowWeight="5" /> <Button android:id="@+id/allClear" android:textSize="30sp" android:background="@drawable/button_border" android:layout_rowWeight="1" android:layout_columnWeight="1" android:text="CE" /> <Button android:id="@+id/divide" android:textSize="30sp" android:background="@drawable/button_border" android:layout_rowWeight="1" android:layout_columnWeight="1" android:text="÷" /> <Button android:id="@+id/multiply" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="×" android:textSize="30sp" /> <Button android:id="@+id/clear" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="C" android:textSize="30sp" /> <Button android:id="@+id/b7" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="7" android:textSize="30sp" /> <Button android:id="@+id/b8" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="8" android:textSize="30sp" /> <Button android:id="@+id/b9" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="9" android:textSize="30sp" /> <Button android:id="@+id/add" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="+" android:textSize="30sp" /> <Button android:id="@+id/b4" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="4" android:textSize="30sp" /> <Button android:id="@+id/b5" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="5" android:textSize="30sp" /> <Button android:id="@+id/b6" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="6" android:textSize="30sp" /> <Button android:id="@+id/subtract" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="-" android:textSize="30sp" /> <Button android:id="@+id/b1" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="1" android:textSize="30sp" /> <Button android:id="@+id/b2" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="2" android:textSize="30sp" /> <Button android:id="@+id/b3" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="3" android:textSize="30sp" /> <Button android:id="@+id/root" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="√ ̄" android:textSize="30sp" /> <Button android:id="@+id/b0" android:layout_rowWeight="1" android:layout_columnSpan="2" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="0" android:textSize="30sp" /> <Button android:id="@+id/point" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="." android:textSize="30sp" /> <Button android:id="@+id/equal" android:layout_rowWeight="1" android:layout_columnWeight="1" android:background="@drawable/button_border" android:text="=" android:textSize="30sp" /> </GridLayout> </LinearLayout>
MainActivity邏輯處理代碼:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button[] buttons = new Button[18]; private int[] ids = new int[]{R.id.allClear,R.id.divide,R.id.multiply,R.id.clear,R.id.b0,R.id.b1, R.id.b2,R.id.b3,R.id.b4,R.id.b5,R.id.b6,R.id.b7,R.id.b8,R.id.b9,R.id.add,R.id.subtract, R.id.equal,R.id.point}; private TextView textView; private String expression = ""; private boolean end = false; private int countOperate=2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i=0; i<ids.length;i++){ buttons[i] = findViewById(ids[i]); buttons[i].setOnClickListener(this); } textView = (TextView)findViewById(R.id.contentText); } public void onClick(View view) { int id = view.getId(); Button button = (Button)view.findViewById(id); String current = button.getText().toString(); if(end){ //如果上一次算式已經結束,則先清零 expression = ""; end = false; } if(current.equals("CE")){ //如果為CE則清零 expression = ""; countOperate=0; }else if(current.equals("C")){ //如果點擊退格 if(expression.length()>1){ //算式長度大於1 expression = expression.substring(0,expression.length()-1);//退一格 int i = expression.length()-1; char tmp = expression.charAt(i); //獲得最後一個字元 char tmpFront = tmp; for(;i>=0;i--){ //向前搜索最近的 +-*/和.,並退出 tmpFront = expression.charAt(i); if(tmpFront=='.'||tmpFront=='+'||tmpFront=='-'||tmpFront=='×'||tmpFront=='÷'){ break; } } if(tmp>='0'&&tmp<='9'){ //最後一個字元為數字,則識別數賦值為0 countOperate=0; }else if(tmp==tmpFront&&tmpFront!='.') countOperate=2; //如果為+-*/,賦值為2 else if(tmpFront=='.') countOperate=1; //如果前面有小數點賦值為1 }else if(expression.length()==1){ expression = ""; } }else if(current.equals(".")){ if(expression.equals("")||countOperate==2){ expression+="0"+current; countOperate = 1; //小數點按過之後賦值為1 } if(countOperate==0){ expression+="."; countOperate = 1; } }else if(current.equals("+")||current.equals("-")||current.equals("×")||current.equals("÷")){ if(countOperate==0){ expression+=current; countOperate = 2; // +-*/按過之後賦值為2 } }else if(current.equals("=")){ //按下=時,計算結果並顯示 double result = (double) Math.round(count()*100)/100; expression+="="+result; end = true; //此次計算結束 } else{//此處是當退格出現2+0時,用current的值替代0 if(expression.length()>=1){ char tmp1 = expression.charAt(expression.length()-1); if(tmp1=='0'&&expression.length()==1){ expression = expression.substring(0,expression.length()-1); } else if(tmp1=='0'&&expression.length()>1){ char tmp2 = expression.charAt(expression.length()-2); if(tmp2=='+'||tmp2=='-'||tmp2=='×'||tmp2=='÷'){ expression = expression.substring(0,expression.length()-1); } } } expression+=current; if(countOperate==2||countOperate==1) countOperate=0; } // Toast.makeText(this, "countOperate:"+countOperate, Toast.LENGTH_SHORT).show(); textView.setText(expression); //顯示出來 } //計算邏輯,求expression表達式的值 private double count(){ double result=0; double tNum=1,lowNum=0.1,num=0; char tmp=0; int operate = 1; //識別+-*/,為+時為正數,為-時為負數,為×時為-2/2,為/時為3/-3; boolean point = false; for(int i=0;i<expression.length();i++){ //遍歷表達式 tmp = expression.charAt(i); if(tmp=='.'){ //因為可能出現小數,此處進行判斷是否有小數出現 point = true; lowNum = 0.1; }else if(tmp=='+'||tmp=='-'){ if(operate!=3&&operate!=-3){ //此處判斷通用,適用於+-* tNum *= num; }else{ //計算/ tNum /= num; } // Toast.makeText(this, "tNum = "+tNum, Toast.LENGTH_SHORT).show(); if(operate<0){ //累加入最終的結果 result -= tNum; }else{ result += tNum; } operate = tmp=='+'?1:-1; num = 0; tNum = 1; point = false; }else if(tmp=='×'){ if(operate!=3&&operate!=-3){ tNum *= num; }else{ tNum /= num; } operate = operate<0?-2:2; point = false; num = 0; }else if(tmp=='÷'){ if(operate!=3&&operate!=-3){ tNum *= num; }else{ tNum /= num; } operate = operate<0?-3:3; point = false; num = 0; }else{ //讀取expression中的每個數字,doube型 if(!point){ num = num*10+tmp-'0'; }else{ num += (tmp-'0')*lowNum; lowNum*=0.1; } } } //迴圈遍歷結束,計算最後一個運算符後面的數 if(operate!=3&&operate!=-3){ tNum *= num; }else{ tNum /= num; } // Toast.makeText(this, "tNum = "+tNum, Toast.LENGTH_SHORT).show(); if(operate<0){ result -= tNum; }else{ result += tNum; } //返回最後的結果 return result; } }