简易计算器的实现

目标效果:

 

通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能。


1.页面中Button使用的是线性布局,最外边一个是父布局,第一行C,DEL,/,*为第一个子布局,第二行7,8,9,-为第二个子布局,第三行4,5,6,+为第三个子布局,第四五行为第四个子布局,第四个子布局中还有两个相当于是孙布局的级别,1,2,3为第一个孙布局,0和.为第二个孙布局,=在两个孙布局之外第四个子布局以内。因为计算器的水平竖直排列十分鲜明,所以可以用线性布局,当然也可以用表格布局来进行排布。


2.activity_main.xml页面用于存放所有控件。

activity_main.xml页面:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_marginTop="40dp"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/etInput"  
  9.         android:layout_width="310dp"  
  10.         android:layout_height="60dip"  
  11.         android:editable="false"         //代表不能进行键盘输入  
  12.         android:gravity="right"          //文字靠右边  
  13.         android:layout_gravity="center"  
  14.         android:background="@drawable/white_bg"/>   <!-- 设置输入框的背景,为一个xml文件 -->  
  15.   
  16.     <LinearLayout  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_marginTop="5dp"  
  20.         android:gravity="center_horizontal"  
  21.         android:orientation="horizontal" >          <!-- 代表水平排布 -->  
  22.           
  23.         <Button  
  24.         android:id="@+id/btClear"  
  25.         android:background="@drawable/white_select"   //设置按钮的背景,为一个xml文件  
  26.         android:layout_width="75dp"  
  27.         android:layout_height="60dp"  
  28.         android:textSize="20sp"  
  29.         android:text="C" />  
  30.         <Button  
  31.         android:id="@+id/btDel"  
  32.         android:background="@drawable/white_select"  
  33.         android:layout_marginLeft="5dp"  
  34.         android:layout_width="75dp"  
  35.         android:layout_height="60dp"  
  36.         android:textSize="20sp"  
  37.         android:text="DEL" />  
  38.         <Button  
  39.         android:id="@+id/btDivide"  
  40.         android:background="@drawable/white_select"  
  41.         android:layout_marginLeft="5dp"  
  42.         android:layout_width="75dp"  
  43.         android:layout_height="60dp"  
  44.         android:textSize="20sp"  
  45.         android:text="/" />  
  46.         <Button  
  47.         android:id="@+id/btMul"  
  48.         android:background="@drawable/white_select"  
  49.         android:layout_marginLeft="5dp"  
  50.         android:layout_width="75dp"  
  51.         android:layout_height="60dp"  
  52.         android:textSize="20sp"  
  53.         android:text="*" />  
  54.     </LinearLayout>  
  55.       
  56.     <LinearLayout  
  57.         android:layout_width="fill_parent"  
  58.         android:layout_height="wrap_content"  
  59.         android:layout_marginTop="5dp"  
  60.         android:gravity="center_horizontal"  
  61.         android:orientation="horizontal" >  
  62.           
  63.         <Button  
  64.         android:id="@+id/btSeven"  
  65.         android:background="@drawable/white_select"  
  66.         android:layout_width="75dp"  
  67.         android:layout_height="60dp"  
  68.         android:textSize="20sp"  
  69.         android:text="7" />  
  70.         <Button  
  71.         android:id="@+id/btEight"  
  72.         android:background="@drawable/white_select"  
  73.         android:layout_marginLeft="5dp"  
  74.         android:layout_width="75dp"  
  75.         android:layout_height="60dp"  
  76.         android:textSize="20sp"  
  77.         android:text="8" />  
  78.         <Button  
  79.         android:id="@+id/btNine"  
  80.         android:background="@drawable/white_select"  
  81.         android:layout_marginLeft="5dp"  
  82.         android:layout_width="75dp"  
  83.         android:layout_height="60dp"  
  84.         android:textSize="20sp"  
  85.         android:text="9" />  
  86.         <Button  
  87.         android:id="@+id/btJian"  
  88.         android:background="@drawable/white_select"  
  89.         android:layout_marginLeft="5dp"  
  90.         android:layout_width="75dp"  
  91.         android:layout_height="60dp"  
  92.         android:textSize="20sp"  
  93.         android:text="-" />  
  94.     </LinearLayout>  
  95.       
  96.     <LinearLayout  
  97.         android:layout_width="fill_parent"  
  98.         android:layout_height="wrap_content"  
  99.         android:layout_marginTop="5dp"  
  100.         android:gravity="center_horizontal"  
  101.         android:orientation="horizontal" >  
  102.           
  103.         <Button  
  104.         android:id="@+id/btFour"  
  105.         android:background="@drawable/white_select"  
  106.         android:layout_width="75dp"  
  107.         android:layout_height="60dp"  
  108.         android:textSize="20sp"  
  109.         android:text="4" />  
  110.         <Button  
  111.         android:id="@+id/btFive"  
  112.         android:background="@drawable/white_select"  
  113.         android:layout_marginLeft="5dp"  
  114.         android:layout_width="75dp"  
  115.         android:layout_height="60dp"  
  116.         android:textSize="20sp"  
  117.         android:text="5" />  
  118.         <Button  
  119.         android:id="@+id/btSix"  
  120.         android:background="@drawable/white_select"  
  121.         android:layout_marginLeft="5dp"  
  122.         android:layout_width="75dp"  
  123.         android:layout_height="60dp"  
  124.         android:textSize="20sp"  
  125.         android:text="6" />  
  126.         <Button  
  127.         android:id="@+id/btJia"  
  128.         android:background="@drawable/white_select"  
  129.         android:layout_marginLeft="5dp"  
  130.         android:layout_width="75dp"  
  131.         android:layout_height="60dp"  
  132.         android:textSize="20sp"  
  133.         android:text="+" />  
  134.     </LinearLayout>  
  135.       
  136.     <LinearLayout  
  137.         android:layout_width="fill_parent"  
  138.         android:layout_height="wrap_content"  
  139.         android:layout_marginTop="5dp"  
  140.         android:gravity="center_horizontal"  
  141.         android:orientation="horizontal" >  
  142.         <LinearLayout  
  143.         android:layout_width="wrap_content"  
  144.         android:layout_height="wrap_content"  
  145.         android:orientation="vertical" >   
  146.             <LinearLayout  
  147.             android:layout_width="wrap_content"  
  148.             android:layout_height="wrap_content"  
  149.             android:orientation="horizontal" >  
  150.                 <Button  
  151.                 android:id="@+id/btOne"  
  152.                 android:background="@drawable/white_select"  
  153.                 android:layout_width="75dp"  
  154.                 android:layout_height="60dp"  
  155.                 android:textSize="20sp"  
  156.                 android:text="1" />  
  157.                 <Button  
  158.                 android:id="@+id/btTwo"  
  159.                 android:background="@drawable/white_select"  
  160.                 android:layout_marginLeft="5dp"  
  161.                 android:layout_width="75dp"  
  162.                 android:layout_height="60dp"  
  163.                 android:textSize="20sp"  
  164.                 android:text="2" />  
  165.                 <Button  
  166.                 android:id="@+id/btThree"  
  167.                 android:background="@drawable/white_select"  
  168.                 android:layout_marginLeft="5dp"  
  169.                 android:layout_width="75dp"  
  170.                 android:layout_height="60dp"  
  171.                 android:textSize="20sp"  
  172.                 android:text="3" />   
  173.             </LinearLayout>   
  174.             <LinearLayout  
  175.             android:layout_width="wrap_content"  
  176.             android:layout_height="wrap_content"  
  177.             android:layout_marginTop="5dp"  
  178.             android:orientation="horizontal" >  
  179.                 <Button  
  180.                 android:id="@+id/btZero"  
  181.                 android:background="@drawable/white_select"  
  182.                 android:layout_width="155dp"  
  183.                 android:layout_height="60dp"  
  184.                 android:textSize="20sp"  
  185.                 android:text="0" />  
  186.                 <Button  
  187.                 android:id="@+id/btPoint"  
  188.                 android:background="@drawable/white_select"  
  189.                 android:layout_marginLeft="5dp"  
  190.                 android:layout_width="75dp"  
  191.                 android:layout_height="60dp"  
  192.                 android:textSize="20sp"  
  193.                 android:text="." />  
  194.             </LinearLayout>   
  195.         </LinearLayout>   
  196.         <Button  
  197.             android:layout_width="75dp"   
  198.             android:background="@drawable/orange_select"  
  199.             android:layout_height="125dp"  
  200.             android:text="="  
  201.             android:layout_marginLeft="5dp"  
  202.             android:textSize="20sp"  
  203.             android:gravity="center"  
  204.             android:id="@+id/btEqu">               
  205.         </Button>       
  206.     </LinearLayout>  
  207. </LinearLayout>  


2.等号按钮和其余按钮的背景及点击效果不同。orange_select.xml页面是等号按钮的背景页面。
orange_select.xml页面:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:drawable="@drawable/orange_bg" android:state_pressed="false"></item> <!-- 不点击时背景为orange_bg.xml页面的效果 -->  
  4.     <item android:drawable="@drawable/khaki_bg" android:state_pressed="true"></item>   <!-- 点击时背景为khaki_bg.xml页面的效果 -->  
  5. </selector>  


3.orange_bg.xml页面:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners android:radius="5dp"/>  
  4.     <solid android:color="#ff9900"/>  
  5. </shape>  


4.khaki.xml页面:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners android:radius="5dp"/>  
  4.     <solid android:color="#cc6600"/>  
  5. </shape>  


5.white_select.xml页面是其余按钮的背景页面。
white_select页面:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:drawable="@drawable/white_bg" android:state_pressed="false"></item>  
  4.     <item android:drawable="@drawable/gray_bg" android:state_pressed="true"></item>  
  5. </selector>  


6.white_bg.xml页面:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners android:radius="5dp"/>  
  4.     <solid android:color="#ffffff"/>  
  5. </shape>  


7.gray_bg.xml页面:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners android:radius="5dp"/>  
  4.     <solid android:color="#cccccc"/>  
  5. </shape>  


8.MainActivity.java处理按钮的点击事件以及数值运算。
MainActivity.java页面:
  1. package com.example.jisuanqi;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.util.Log;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11.   
  12. public class MainActivity extends Activity implements OnClickListener{  
  13.   
  14.     private EditText etInput;     //实例输入框  
  15.     private Button btOne;         //实例所有按钮  
  16.     private Button btTwo;  
  17.     private Button btThree;  
  18.     private Button btFour;  
  19.     private Button btFive;  
  20.     private Button btSix;  
  21.     private Button btSeven;  
  22.     private Button btEight;  
  23.     private Button btNine;  
  24.     private Button btZero;  
  25.     private Button btPoint;  
  26.     private Button btJia;  
  27.     private Button btJian;  
  28.     private Button btMul;  
  29.     private Button btDivide;  
  30.     private Button btEqu;  
  31.     private Button btClear;  
  32.     private Button btDel;  
  33.     boolean clear_flag;           //清空标识,当用户点击等号完成一次运算时进行清空  
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.           
  39.         etInput=(EditText) findViewById(R.id.etInput);     //获取输入框的id  
  40.         btOne=(Button) findViewById(R.id.btOne);           //获取所有按钮的id  
  41.         btTwo=(Button) findViewById(R.id.btTwo);  
  42.         btThree=(Button) findViewById(R.id.btThree);  
  43.         btFour=(Button) findViewById(R.id.btFour);  
  44.         btFive=(Button) findViewById(R.id.btFive);  
  45.         btSix=(Button) findViewById(R.id.btSix);  
  46.         btSeven=(Button) findViewById(R.id.btSeven);  
  47.         btEight=(Button) findViewById(R.id.btEight);  
  48.         btNine=(Button) findViewById(R.id.btNine);  
  49.         btZero=(Button) findViewById(R.id.btZero);  
  50.         btPoint=(Button) findViewById(R.id.btPoint);  
  51.         btJia=(Button) findViewById(R.id.btJia);  
  52.         btJian=(Button) findViewById(R.id.btJian);  
  53.         btMul=(Button) findViewById(R.id.btMul);  
  54.         btDivide=(Button) findViewById(R.id.btDivide);  
  55.         btEqu=(Button) findViewById(R.id.btEqu);  
  56.         btClear=(Button) findViewById(R.id.btClear);  
  57.         btDel=(Button) findViewById(R.id.btDel);  
  58.           
  59.         btOne.setOnClickListener(this);     //设置点击事件,因为MainActivity已经实现了OnClickListener接口,所以只需要参数只需要传this  
  60.         btTwo.setOnClickListener(this);  
  61.         btThree.setOnClickListener(this);  
  62.         btFour.setOnClickListener(this);  
  63.         btFive.setOnClickListener(this);  
  64.         btSix.setOnClickListener(this);  
  65.         btSeven.setOnClickListener(this);  
  66.         btEight.setOnClickListener(this);  
  67.         btNine.setOnClickListener(this);  
  68.         btZero.setOnClickListener(this);  
  69.         btPoint.setOnClickListener(this);  
  70.         btJia.setOnClickListener(this);  
  71.         btJian.setOnClickListener(this);  
  72.         btMul.setOnClickListener(this);  
  73.         btDivide.setOnClickListener(this);  
  74.         btEqu.setOnClickListener(this);  
  75.         btClear.setOnClickListener(this);  
  76.         btDel.setOnClickListener(this);  
  77.     }  
  78.   
  79.     @Override  
  80.     public boolean onCreateOptionsMenu(Menu menu) {  
  81.         // Inflate the menu; this adds items to the action bar if it is present.  
  82.         getMenuInflater().inflate(R.menu.main, menu);  
  83.         return true;  
  84.     }  
  85.   
  86.     @Override  
  87.     public void onClick(View v) {  
  88.         // TODO Auto-generated method stub  
  89.         String etinput=etInput.getText().toString();    //获取输入框中的内容并转化为String类型  
  90.         switch(v.getId()){                              //判断点击按钮的id  
  91.             case R.id.btZero:  
  92.             case R.id.btOne:  
  93.             case R.id.btTwo:  
  94.             case R.id.btThree:  
  95.             case R.id.btFour:  
  96.             case R.id.btFive:  
  97.             case R.id.btSix:  
  98.             case R.id.btSeven:  
  99.             case R.id.btEight:                
  100.             case R.id.btNine:  
  101.             case R.id.btPoint:  
  102.                 if(clear_flag){  
  103.                     clear_flag=false;  
  104.                     etinput="";  
  105.                     etInput.setText("");  
  106.                 }  
  107.                 etInput.setText(etinput+((Button)v).getText());   //点击数字和小数点直接显示内容  
  108.                 break;  
  109.             case R.id.btJia:  
  110.             case R.id.btJian:  
  111.             case R.id.btMul:  
  112.             case R.id.btDivide:  
  113.                 if(clear_flag){  
  114.                     clear_flag=false;  
  115.                     etinput="";  
  116.                     etInput.setText("");    //当clear_flag为true时进入if语句,并可以清空,代表用户点击了等于号完成一次运算,并使clear_flag变成了true  
  117.                 }  
  118.                 etInput.setText(etinput+" "+((Button)v).getText()+" ");   //点击运算符也是直接显示,但是为了后边运算要在运算符两侧加上空格  
  119.                 break;  
  120.             case R.id.btDel:  
  121.                 if(clear_flag){  
  122.                     clear_flag=false;  
  123.                     etinput="";  
  124.                     etInput.setText("");  
  125.                 }else if(etinput!=null&&!etinput.equals("")){  
  126.                     etInput.setText(etinput.substring(0,etinput.length()-1));  //如果输入框内容不为空,则去掉最后一位  
  127.                 }  
  128.                 break;  
  129.             case R.id.btClear:  
  130.                 clear_flag=false;  
  131.                 etinput="";  
  132.                 etInput.setText("");    //直接设置输入框为空  
  133.                 break;  
  134.             case R.id.btEqu:  
  135.                 getResult();            //点击等号调用getResult()方法  
  136.                 break;  
  137.         }  
  138.     }  
  139.     public void getResult(){  
  140.         String exp=etInput.getText().toString();  //获取输入框的内容  
  141.         if(exp==null||exp.equals("")){  
  142.             return;  
  143.         }  
  144.         if(!exp.contains(" ")){                   //判断输入框是否包含空格,也就是没有点击运算符  
  145.             return;  
  146.         }  
  147.         if(clear_flag){                           //点击等号clear_flag为true,当再点击别的数字或运算符时才会变为false,如果连续点击等号,则第二次点击无效,直接返回  
  148.             clear_flag=false;  
  149.             return;  
  150.         }  
  151.         clear_flag=true;  
  152.         double result=0;  
  153.         String s1=exp.substring(0,exp.indexOf(" "));                     //运算符前面的字符串  
  154.         String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);  //运算符,是根据运算符前边的空格计算的  
  155.         String s2=exp.substring(exp.indexOf(" ")+3);                     //运算符后边的字符串  
  156.         if(!s1.equals("")&&!s2.equals("")){  
  157.             double d1=Double.parseDouble(s1);                            //将字符串转换为double类型  
  158.             double d2=Double.parseDouble(s2);  
  159.             if(op.equals("+")){  
  160.                 result=d1+d2;  
  161.             }else if(op.equals("-")){  
  162.                 result=d1-d2;  
  163.             }else if(op.equals("*")){  
  164.                 result=d1*d2;  
  165.             }else if(op.equals("/")){  
  166.                 if(d2==0){          //判断除数为0的情况  
  167.                     result=0;  
  168.                 }else{  
  169.                     result=d1/d2;  
  170.                 }  
  171.             }  
  172.             if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("/")){   //如果两个数都是整形,那么结果就需要显示为整数  
  173.                 int r=(int)result;                                       //将String型计算结果强制转换为整形  
  174.                 etInput.setText(r+"");  
  175.             }else{  
  176.                 etInput.setText(result+"");  
  177.             }  
  178.         }else if(!s1.equals("")&&s2.equals("")){         //如果用户输入一个数字就点击运算符,那么将不计算  
  179.             etInput.setText(exp);  
  180.         }else if(s1.equals("")&&!s2.equals("")){         //如果一上来就点击运算符并输入第二个数,那么第一个数默认为0  
  181.             double d2=Double.parseDouble(s2);  
  182.             if(op.equals("+")){  
  183.                 result=0+d2;  
  184.             }else if(op.equals("-")){  
  185.                 result=0-d2;  
  186.             }else if(op.equals("*")){  
  187.                 result=0;  
  188.             }else if(op.equals("/")){  
  189.                 result=0;  
  190.             }  
  191.             if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("/")){  
  192.                 int r=(int)result;  
  193.                 etInput.setText(r+"");  
  194.             }else{  
  195.                 etInput.setText(result+"");  
  196.             }  
  197.         }else{  
  198.             etInput.setText("");  
  199.         }  
  200.     }  
  201.   
  202. }  


9.程序完成就可以运行了。因为是简易计算器,所以还不能进行连续的加减乘除。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值