main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:padding="30px" android:layout_width="fill_parent" android:orientation="vertical"> <EditText android:id="@+id/txtRes" android:layout_width="240px" android:gravity="center_vertical|right" android:layout_height="wrap_content" android:numeric="integer" android:editable="false" android:text="" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50px" android:gravity="right"> </LinearLayout> <TableLayout android:layout_width="wrap_content" android:id="@+id/TableLayout01" android:layout_height="wrap_content" android:orientation="horizontal"> <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_height="wrap_content" android:text="←" android:layout_width="60px" android:id="@+id/btnBack"></Button> <Button android:layout_height="wrap_content" android:text="CE" android:layout_width="60px" android:id="@+id/btnCE"></Button> <Button android:layout_height="wrap_content" android:text="C" android:layout_width="60px" android:id="@+id/btnC"></Button> <Button android:layout_height="wrap_content" android:text="+/-" android:layout_width="60px" android:id="@+id/btnSym"></Button> </TableRow> <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_height="wrap_content" android:text="7" android:layout_width="60px" android:id="@+id/btn7"></Button> <Button android:layout_height="wrap_content" android:text="8" android:layout_width="60px" android:id="@+id/btn8"></Button> <Button android:layout_height="wrap_content" android:text="9" android:layout_width="60px" android:id="@+id/btn9"></Button> <Button android:layout_height="wrap_content" android:text="+" android:layout_width="60px" android:id="@+id/btnAdd"></Button> </TableRow> <TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_height="wrap_content" android:text="4" android:layout_width="60px" android:id="@+id/btn4"></Button> <Button android:layout_height="wrap_content" android:text="5" android:layout_width="60px" android:id="@+id/btn5"></Button> <Button android:layout_height="wrap_content" android:text="6" android:layout_width="60px" android:id="@+id/btn6"></Button> <Button android:layout_height="wrap_content" android:text="-" android:layout_width="60px" android:id="@+id/btnSub"></Button> </TableRow> <TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_height="wrap_content" android:text="1" android:layout_width="60px" android:id="@+id/btn1"></Button> <Button android:layout_height="wrap_content" android:text="2" android:layout_width="60px" android:id="@+id/btn2"></Button> <Button android:layout_height="wrap_content" android:text="3" android:layout_width="60px" android:id="@+id/btn3"></Button> <Button android:layout_height="wrap_content" android:text="*" android:layout_width="60px" android:id="@+id/btnMul"></Button> </TableRow> <TableRow android:id="@+id/TableRow05" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_height="wrap_content" android:text="0" android:layout_width="60px" android:id="@+id/btn0"></Button> <Button android:layout_height="wrap_content" android:text="." android:layout_width="60px" android:id="@+id/btnPoint"></Button> <Button android:layout_height="wrap_content" android:text="=" android:layout_width="60px" android:id="@+id/btnEqual"></Button> <Button android:layout_height="wrap_content" android:text="/" android:layout_width="60px" android:id="@+id/btnDiv"></Button> </TableRow> </TableLayout> </LinearLayout>
CalculatorActivity.java
package com.demo.Calculator; import android.app.Activity; import android.os.Bundle; //導入相關包 import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class CalculatorActivity extends Activity implements OnClickListener { //全局變量,定義Button的按鈕,private表示私有,只能被類內部調用 //退檔,清空,刪除,符號控件 private Button btnBack, btnC, btnCE, btnSym; //數字0-9控件 private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9; //操作運輸符(加,減,乘,除,小數點,等於) private Button btnAdd, btnSub,btnMul,btnDiv, btnPoint, btnEqual; //定義可編輯文本框 private EditText txtRes; //一些計算時用到的變量(包括中間變量) private double result, temp, numtwo, other=0.0; private char sym='0';//用於向文本框中輸入值(數字,操作運輸符等)時,判斷輸入的是什麼,以便於進行相應的操作 //即當sym爲四個運算符(+,-,*, /)時都會直接跳出,否則將點擊運算符之前的數值保存起來 private String temp1, temp2; private int templength, pointindex; //將這些控件於main.xml的定義綁定起來 public void AddOnClickLister() { //添加對象 btnBack=(Button)findViewById(R.id.btnBack);//強制轉化爲相應的Button類型(下同) btnBack.setOnClickListener( this);//將控件添加到窗體中(下同) btnC=(Button)findViewById(R.id.btnC); btnC.setOnClickListener( this); btnCE=(Button)findViewById(R.id.btnCE); btnCE.setOnClickListener( this); btnSym=(Button)findViewById(R.id.btnSym); btnSym.setOnClickListener( this); btn7=(Button)findViewById(R.id.btn7); btn7.setOnClickListener( this); btn8=(Button)findViewById(R.id.btn8); btn8.setOnClickListener( this); btn9=(Button)findViewById(R.id.btn9); btn9.setOnClickListener( this); btnDiv=(Button)findViewById(R.id.btnDiv); btnDiv.setOnClickListener( this); btn4=(Button)findViewById(R.id.btn4); btn4.setOnClickListener( this); btn5=(Button)findViewById(R.id.btn5); btn5.setOnClickListener( this); btn6=(Button)findViewById(R.id.btn6); btn6.setOnClickListener( this); btnMul=(Button)findViewById(R.id.btnMul); btnMul.setOnClickListener( this); btn1=(Button)findViewById(R.id.btn1); btn1.setOnClickListener( this); btn2=(Button)findViewById(R.id.btn2); btn2.setOnClickListener( this); btn3=(Button)findViewById(R.id.btn3); btn3.setOnClickListener( this); btnSub=(Button)findViewById(R.id.btnSub); btnSub.setOnClickListener( this); btn0=(Button)findViewById(R.id.btn0); btn0.setOnClickListener( this); btnPoint=(Button)findViewById(R.id.btnPoint); btnPoint.setOnClickListener( this); btnAdd=(Button)findViewById(R.id.btnAdd); btnAdd.setOnClickListener( this); btnEqual=(Button)findViewById(R.id.btnEqual); btnEqual.setOnClickListener( this); txtRes=(EditText)findViewById(R.id.txtRes); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setTitle("Calculator");//設置窗體顯示名稱 AddOnClickLister();//將這些控件於main.xml的定義綁定起來 } public void getNum() { //getNum方法,txtRes的值不等於 " 除數不能爲零! " 且不爲 空 才執行花括號內部的語句 if(this.txtRes.getText().toString().equals("除數不能爲零!")) { //.equals用於判斷字符串相等,如果用==則是判斷兩個是否爲同一個引用 this.txtRes.getText(); } else { if(!this.txtRes.getText().toString().equals("")) { //將txtRes的值轉換成字符串再轉換成double類型後複製給numtwo numtwo=Double.valueOf(this.txtRes.getText().toString()); } } } public void smartway() { //這裏用於將.0省略,比方2.0,只顯示2,並且對一些重要數值進行清空 temp1=String.valueOf(temp); templength=temp1.length(); pointindex=temp1.indexOf("."); temp2=temp1.substring(pointindex,templength); other=0.0; sym='0'; if (temp2.equals(".0")) { int newtemp=(int)temp; this.txtRes.setText(String.valueOf(newtemp)); } else { this.txtRes.setText(String.valueOf(temp)); } } public void fhagain() { //因爲存儲運算符的是char類型,這裏的核心作用是防止對其重複賦值導致報錯, //四個運算符(+,-,*,/)都會直接跳出,default的作用則是將點擊運算符之前的數值保存起來,並且重設文本框 switch(this.sym) { case '+': break; case '-': break; case '/': break; case '*': break; default: this.result=Double.valueOf(this.txtRes.getText().toString())+other; this.txtRes.setText(""); } } @Override public void onClick(View arg0) {// TODO Auto-generated method stub switch(arg0.getId()){//用switch來處理各個按鈕的點擊事件 //輸入的字符是數字(0-9)的處理,即this.txtRes.getText()+"i",i爲輸入的數字 case R.id.btn0: if(this.txtRes.getText().toString().equals("0")) {//輸入的第一個字符爲0,則丟棄改0 break; } else {//輸入的0非第一字符,將原有的字符連接上一個0,此操作相當於SNum=FNum*10+0 this.txtRes.setText(this.txtRes.getText()+"0"); } break; //將當前輸入的字符(0-9)加入到文本框末尾 //此操作相當於SNum=FNum*10+i(i爲當前輸入的字符(0-9)) case R.id.btn1: this.txtRes.setText(this.txtRes.getText()+"1"); break; case R.id.btn2: this.txtRes.setText(this.txtRes.getText()+"2"); break; case R.id.btn3: this.txtRes.setText(this.txtRes.getText()+"3"); break; case R.id.btn4: this.txtRes.setText(this.txtRes.getText()+"4"); break; case R.id.btn5: this.txtRes.setText(this.txtRes.getText()+"5"); break; case R.id.btn6: this.txtRes.setText(this.txtRes.getText()+"6"); break; case R.id.btn7: this.txtRes.setText(this.txtRes.getText()+"7"); break; case R.id.btn8: this.txtRes.setText(this.txtRes.getText()+"8"); break; case R.id.btn9: this.txtRes.setText(this.txtRes.getText()+"9"); break; //符號(+/-)的處理 case R.id.btnSym: if(this.txtRes.getText().toString().indexOf("-")==-1) {//如果本身就是無符號正數(省略符號“+”),只要在前面加“-”變換爲負數 this.txtRes.setText("-"+this.txtRes.getText().toString()); } else{//如果本身爲負數,則截取第1-length位(第0位爲符號“-”),其中捨棄第一位(即符號位“-”) this.txtRes.setText(this.txtRes.getText().toString().substring(1, this.txtRes.getText().toString().length())); } break; //CE清空按鈕,刪除所有的變量重新開始新的計算。setEnabled控制控件的禁用 case R.id.btnCE: this.txtRes.setText(null); this.numtwo=0.0; this.other=0.0; this.sym='0'; this.btnEqual.setEnabled(true); this.btnC.setEnabled(true); break; //退格功能的實現 case R.id.btnBack: String check=this.txtRes.getText().toString(); if(!check.equals("")){//只有在文本框具有字符時,對於退檔操作纔有意思 String newnum=this.txtRes.getText().toString(); int length=newnum.length(); //通過String提供的substring(begin,end)方法截取String的從begin到begin+end的end位子串 String newString=newnum.substring(0,length-1);//截取最後一位,即退檔一位 this.txtRes.setText(newString); } else{ this.txtRes.setText(null); } break; //添加小數點功能 case R.id.btnPoint: if(this.txtRes.getText().toString().indexOf(".")==-1) {//當點擊btnPoint按鈕時,先判斷是否已經浮點數(非純整數)了 //是純整數則將小數點加上,否則不執行該操作 this.txtRes.setText(this.txtRes.getText()+"."); } else { break; }break; //刪除當前文本框的值 case R.id.btnC: if (!this.txtRes.getText().toString().equals("")) {//如果文本框不空,則將文本框清空,否則(文本框已經空),則不執行該操作 this.other=Double.valueOf(this.txtRes.getText().toString()); } else { break; }this.txtRes.setText(""); break; //四則運算 //通過sym標記操作(+,-,*,/),待得到第二個操作數時,再進行相應操作 case R.id.btnAdd: fhagain(); this.sym='+';// break; case R.id.btnSub:// fhagain(); this.sym='-';// break; case R.id.btnMul:// fhagain(); this.sym='*'; break; case R.id.btnDiv:// fhagain(); this.sym='/'; break; //計算按鈕 case R.id.btnEqual: switch(this.sym){ case '+': getNum(); temp=this.result+numtwo; this.smartway(); break; case '-': getNum(); temp=this.result-numtwo; this.smartway(); break; case '*': getNum(); temp=this.result*numtwo; this.smartway(); break; case '/': getNum(); if(numtwo==0.0){//除數爲0 this.txtRes.setText("除數不能爲零!"); this.btnEqual.setEnabled(false); this.btnC.setEnabled(false); break; } else{ //除數不爲0 temp=this.result/numtwo; this.smartway(); } break; } break; default: break; } } }