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; } } }