Android实现简单的计算器

android的布局和配置文件

1.下面就是android实现的计算器的布局配置,可以看见基本是线性布局,这样的好处是界面简洁明了

[html] view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity" >  
  11.       
  12. <!-- 任务:    要求datapick,监听用户选择的日期 -->  
  13.     <!--         android:inputType="text" -->  
  14.       
  15.         <EditText  
  16.         android:layout_width="280dp"  
  17.         android:layout_height="80dp"  
  18.         android:background="@android:drawable/edit_text"  
  19.         android:inputType="text"  
  20.         android:id="@+id/result"   
  21.           
  22.             />   
  23.       
  24.     <LinearLayout   
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         android:orientation="horizontal"  
  28.         >  
  29.         <Button   
  30.             android:layout_width="70dp"  
  31.             android:layout_height="60dp"  
  32.             android:text="@string/c_ce"  
  33.             android:id="@+id/c_ce"  
  34.             />  
  35.         <Button   
  36.             android:layout_width="70dp"  
  37.             android:layout_height="60dp"  
  38.             android:text="@string/c_c"  
  39.             android:id="@+id/c_c"  
  40.             />  
  41.         <Button   
  42.             android:layout_width="70dp"  
  43.             android:layout_height="60dp"  
  44.             android:text="@string/c_xx"  
  45.             android:id="@+id/c_xx"  
  46.             />  
  47.         <Button   
  48.             android:layout_width="70dp"  
  49.             android:layout_height="60dp"  
  50.             android:text="@string/c_div"  
  51.             android:id="@+id/c_div"  
  52.             />  
  53.     </LinearLayout>  
  54.       
  55.     <LinearLayout   
  56.         android:layout_width="fill_parent"  
  57.         android:layout_height="wrap_content"  
  58.         android:orientation="horizontal"  
  59.         >  
  60.         <Button   
  61.             android:layout_width="70dp"  
  62.             android:layout_height="60dp"  
  63.             android:text="@string/c_7"  
  64.             android:id="@+id/c_7"  
  65.             />  
  66.         <Button   
  67.             android:layout_width="70dp"  
  68.             android:layout_height="60dp"  
  69.             android:text="@string/c_8"  
  70.             android:id="@+id/c_8"  
  71.             />  
  72.         <Button   
  73.             android:layout_width="70dp"  
  74.             android:layout_height="60dp"  
  75.             android:text="@string/c_9"  
  76.             android:id="@+id/c_9"  
  77.             />  
  78.         <Button   
  79.             android:layout_width="70dp"  
  80.             android:layout_height="60dp"  
  81.             android:text="@string/c_X"  
  82.             android:id="@+id/c_X"  
  83.             />  
  84.     </LinearLayout>  
  85.       
  86.     <LinearLayout   
  87.         android:layout_width="fill_parent"  
  88.         android:layout_height="wrap_content"  
  89.         android:orientation="horizontal"  
  90.         >  
  91.         <Button   
  92.             android:layout_width="70dp"  
  93.             android:layout_height="60dp"  
  94.             android:text="@string/c_4"  
  95.             android:id="@+id/c_4"  
  96.             />  
  97.         <Button   
  98.             android:layout_width="70dp"  
  99.             android:layout_height="60dp"  
  100.             android:text="@string/c_5"  
  101.             android:id="@+id/c_5"  
  102.             />  
  103.         <Button   
  104.             android:layout_width="70dp"  
  105.             android:layout_height="60dp"  
  106.             android:text="@string/c_6"  
  107.             android:id="@+id/c_6"  
  108.             />  
  109.         <Button   
  110.             android:layout_width="70dp"  
  111.             android:layout_height="60dp"  
  112.             android:text="@string/c_delete"  
  113.             android:id="@+id/c_delete"  
  114.             />  
  115.     </LinearLayout>  
  116.       
  117.     <LinearLayout   
  118.         android:layout_width="fill_parent"  
  119.         android:layout_height="wrap_content"  
  120.         android:orientation="horizontal"  
  121.         >  
  122.         <Button   
  123.             android:layout_width="70dp"  
  124.             android:layout_height="60dp"  
  125.             android:text="@string/c_1"  
  126.             android:id="@+id/c_1"  
  127.             />  
  128.         <Button   
  129.             android:layout_width="70dp"  
  130.             android:layout_height="60dp"  
  131.             android:text="@string/c_2"  
  132.             android:id="@+id/c_2"  
  133.             />  
  134.         <Button   
  135.             android:layout_width="70dp"  
  136.             android:layout_height="60dp"  
  137.             android:text="@string/c_3"  
  138.             android:id="@+id/c_3"  
  139.             />  
  140.         <Button   
  141.             android:layout_width="70dp"  
  142.             android:layout_height="60dp"  
  143.             android:text="@string/c_add"  
  144.             android:id="@+id/c_add"  
  145.             />  
  146.     </LinearLayout>  
  147.       
  148.     <LinearLayout   
  149.         android:layout_width="fill_parent"  
  150.         android:layout_height="wrap_content"  
  151.         android:orientation="horizontal"  
  152.         >  
  153.         <Button   
  154.             android:layout_width="70dp"  
  155.             android:layout_height="60dp"  
  156.             android:text="@string/c_aord"  
  157.             android:id="@+id/c_aord"  
  158.             />  
  159.         <Button   
  160.             android:layout_width="70dp"  
  161.             android:layout_height="60dp"  
  162.             android:text="@string/c_0"  
  163.             android:id="@+id/c_0"  
  164.             />  
  165.         <Button   
  166.             android:layout_width="70dp"  
  167.             android:layout_height="60dp"  
  168.             android:text="@string/c_point"  
  169.             android:id="@+id/c_point"  
  170.             />  
  171.         <Button   
  172.             android:layout_width="70dp"  
  173.             android:layout_height="60dp"  
  174.             android:text="@string/c_equal"  
  175.             android:id="@+id/c_equal"  
  176.             />  
  177.     </LinearLayout>  
  178.       
  179.       
  180.   
  181.       
  182.   
  183. </LinearLayout>  

2.按钮对应的字符串配置:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">计算器</string>  
  5.     <string name="action_settings">Settings</string>  
  6.     <string name="c_ce">CE</string>  
  7.     <string name="c_c">C</string>  
  8.     <string name="c_xx">Xx</string>  
  9.     <string name="c_div">÷</string>  
  10.       
  11.     <string name="c_7">7</string>  
  12.     <string name="c_8">8</string>  
  13.     <string name="c_9">9</string>  
  14.     <string name="c_X">X</string>  
  15.       
  16.     <string name="c_4">4</string>  
  17.     <string name="c_5">5</string>  
  18.     <string name="c_6">6</string>  
  19.     <string name="c_delete"></string>  
  20.       
  21.     <string name="c_1">1</string>  
  22.     <string name="c_2">2</string>  
  23.     <string name="c_3">3</string>  
  24.     <string name="c_add"></string>  
  25.       
  26.     <string name="c_aord">±</string>  
  27.     <string name="c_0">0</string>  
  28.     <string name="c_point">·</string>  
  29.     <string name="c_equal"></string>  
  30.   
  31. </resources>  

实现的界面效果如下

Android实现简单的计算器


3.具体操作细节:

这里由于是个练习没有按照很严格的分类但功能基本实现了:

以下是逻辑需要用运算接口和加减乘除算法:

[java] view plain copy
  1. package com.example.control;  
  2.   
  3. public interface Calculate {  
  4.     /** 
  5.      * 计算的方法 
  6.      * @param x 输入的要被计算的参数x 
  7.      * @param y 输入的要被计算的参数y 
  8.      */  
  9.     public float calculate(double x,double y);  
  10. }  

[java] view plain copy
  1. package com.example.control;  
  2.   
  3. public class Add implements Calculate{  
  4.   
  5.   
  6.     @Override  
  7.     public float calculate(double x, double y) {  
  8.         return (float) (x+y);  
  9.     }  
  10.       
  11. }  

[java] view plain copy
  1. package com.example.control;  
  2.   
  3. public class Delete implements Calculate{  
  4.   
  5.     public float calculate(double x, double y) {  
  6.         return (float) (x-y);  
  7.     }  
  8.   
  9. }  

[java] view plain copy
  1. package com.example.control;  
  2.   
  3. public class Mulitply implements Calculate{  
  4.   
  5.     public float calculate(double x, double y) {  
  6.         return (float) (x*y);  
  7.     }  
  8.   
  9. }  

[java] view plain copy
  1. package com.example.control;  
  2.   
  3. public class Div implements Calculate{  
  4.   
  5.     public float calculate(double x, double y) {  
  6.         if(y==0){  
  7.             try {  
  8.                 throw new Exception("被除数不能为0");  
  9.             } catch (Exception e) {  
  10.                 e.printStackTrace();  
  11.             }  
  12.             return 0;  
  13.         }  
  14.         return (float) (x/y);  
  15.     }  
  16.   
  17. }  

以上这些供具体使用的时候调用

4.以下为activity和监听的内容:

[java] view plain copy
  1. package com.example.caculate;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. import com.example.control.Add;  
  14. import com.example.control.Calculate;  
  15. import com.example.control.Delete;  
  16. import com.example.control.Div;  
  17. import com.example.control.Mulitply;  
  18.   
  19. public class MainActivity extends Activity {  
  20.       
  21.     private float x,y;  
  22.     private String text="";  
  23.     private int tagremeber=0;  
  24.     private EditText textview;  
  25.     private boolean eqstatus=false;  
  26.     private boolean zestatus=false;  
  27.     private int count=0;  
  28.       
  29.     private Calculate cl;  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.activity_main);  
  33.         textview=(EditText) findViewById(R.id.result);  
  34.         textview.setText("0.0");  
  35.         textview.requestFocus();  
  36.           
  37.         Button bt_0=(Button) findViewById(R.id.c_0);  
  38.         Button bt_1=(Button) findViewById(R.id.c_1);  
  39.         Button bt_2=(Button) findViewById(R.id.c_2);  
  40.         Button bt_3=(Button) findViewById(R.id.c_3);  
  41.         Button bt_4=(Button) findViewById(R.id.c_4);  
  42.         Button bt_5=(Button) findViewById(R.id.c_5);  
  43.         Button bt_6=(Button) findViewById(R.id.c_6);  
  44.         Button bt_7=(Button) findViewById(R.id.c_7);  
  45.         Button bt_8=(Button) findViewById(R.id.c_8);  
  46.         Button bt_9=(Button) findViewById(R.id.c_9);  
  47.         Button bt_add=(Button) findViewById(R.id.c_add);  
  48.         Button bt_delete=(Button) findViewById(R.id.c_delete);  
  49.         Button bt_mul=(Button) findViewById(R.id.c_X);  
  50.         Button bt_div=(Button) findViewById(R.id.c_div);  
  51.         Button bt_c=(Button) findViewById(R.id.c_c);  
  52.         Button bt_xx=(Button) findViewById(R.id.c_xx);  
  53.         Button bt_ce=(Button) findViewById(R.id.c_ce);  
  54.         Button bt_aord=(Button) findViewById(R.id.c_aord);  
  55.         Button bt_equal=(Button) findViewById(R.id.c_equal);  
  56.         Button bt_point=(Button) findViewById(R.id.c_point);  
  57.           
  58.         //其中1-10为数字  11-20位运算符  
  59.         bt_0.setTag(20);  
  60.         bt_1.setTag(1);  
  61.         bt_2.setTag(2);  
  62.         bt_3.setTag(3);  
  63.         bt_4.setTag(4);  
  64.         bt_5.setTag(5);  
  65.         bt_6.setTag(6);  
  66.         bt_7.setTag(7);  
  67.         bt_8.setTag(8);  
  68.         bt_9.setTag(9);  
  69.         bt_add.setTag(10);  
  70.         bt_delete.setTag(11);  
  71.         bt_mul.setTag(12);  
  72.         bt_div.setTag(13);  
  73.           
  74.         bt_c.setTag(14);  
  75.         bt_xx.setTag(15);  
  76.         bt_ce.setTag(16);  
  77.         bt_aord.setTag(17);  
  78.         bt_equal.setTag(18);  
  79.         bt_point.setTag(19);  
  80.         //给0-9和.加上数值对应的监听  
  81.         bt_0.setOnClickListener(ol);  
  82.         bt_1.setOnClickListener(ol);  
  83.         bt_2.setOnClickListener(ol);  
  84.         bt_3.setOnClickListener(ol);  
  85.         bt_4.setOnClickListener(ol);  
  86.         bt_5.setOnClickListener(ol);  
  87.         bt_6.setOnClickListener(ol);  
  88.         bt_7.setOnClickListener(ol);  
  89.         bt_8.setOnClickListener(ol);  
  90.         bt_9.setOnClickListener(ol);  
  91.         bt_point.setOnClickListener(ol);  
  92.         //运算符类按钮加上运算法类的监听  
  93.         bt_add.setOnClickListener(cal_listener);  
  94.         bt_delete.setOnClickListener(cal_listener);  
  95.         bt_mul.setOnClickListener(cal_listener);  
  96.         bt_div.setOnClickListener(cal_listener);  
  97.         bt_equal.setOnClickListener(cal_listener);  
  98.         //清除等按钮  
  99.         bt_c.setOnClickListener(setzero_listener);  
  100.         bt_xx.setOnClickListener(setzero_listener);  
  101.         bt_ce.setOnClickListener(setzero_listener);  
  102.         bt_aord.setOnClickListener(setzero_listener);  
  103.     }  
  104.       
  105.     OnClickListener ol=new OnClickListener() {  
  106.         public void onClick(View view) {  
  107.             int tag=(Integer) view.getTag();  
  108.             if(eqstatus){  
  109.                 text="";  
  110.                 textview.setSelection(text.length());  
  111.                 eqstatus=false;  
  112.             }  
  113.               
  114.             if(zestatus){  
  115.                 text="";  
  116.                 textview.setSelection(text.length());  
  117.                 zestatus=false;  
  118.             }  
  119.               
  120.             switch(tag){  
  121.   
  122.             case 20:  
  123.                 text=text+"0";  
  124.                 break;  
  125.             case 1:  
  126.                 text=text+"1";  
  127.                 break;  
  128.             case 2:  
  129.                 text=text+"2";  
  130.                 break;  
  131.             case 3:  
  132.                 text=text+"3";  
  133.                 break;  
  134.             case 4:  
  135.                 text=text+"4";  
  136.                 break;  
  137.             case 5:  
  138.                 text=text+"5";  
  139.                 break;  
  140.             case 6:  
  141.                 text=text+"6";  
  142.                 break;  
  143.             case 7:  
  144.                 text=text+"7";  
  145.                 break;  
  146.             case 8:  
  147.                 text=text+"8";  
  148.                 break;  
  149.             case 9:  
  150.                 text=text+"9";  
  151.                 break;  
  152.             case 19:  
  153.                 text=text+".";  
  154.             }  
  155.             textview.setText(text);  
  156.             textview.setSelection(text.length());  
  157.         }  
  158.     };  
  159.       
  160.     OnClickListener cal_listener=new OnClickListener() {  
  161.           
  162.         public void onClick(View view) {  
  163.             int tag=(Integer) view.getTag();  
  164.         //当单击运算按钮不为=时  
  165.         if(tag!=18){  
  166.             //保存x并清除文本域  
  167.             x=Float.parseFloat(text);  
  168.             tagremeber=tag;  
  169.             text="";  
  170.             textview.setText(text);  
  171.             textview.setSelection(text.length());  
  172.         }  
  173.         //点击=运算符时  
  174.         else if(tag==18){  
  175.               
  176.               
  177.                 y=Float.parseFloat(text);  
  178.                 switch(tagremeber){  
  179.                 case 10:  
  180.                     cl=new Add();  
  181.                     break;  
  182.                 case 11:  
  183.                     cl=new Delete();  
  184.                     break;  
  185.                 case 12:  
  186.                     cl=new Mulitply();  
  187.                     break;  
  188.                 case 13:  
  189.                     cl=new Div();  
  190.                     break;  
  191.                 }  
  192.                 float result=cl.calculate(x, y);  
  193.                 text=String.valueOf(result);  
  194.                 textview.setText(text);  
  195.                 textview.setSelection(text.length());  
  196.               
  197.             //表示当前状态为结果状态,下次点击数字时会自动清除这次结果  
  198.             eqstatus=true;  
  199.         }  
  200.           
  201.         }  
  202.     };  
  203.       
  204.       
  205.     OnClickListener setzero_listener=new OnClickListener() {  
  206.         //  
  207.         @Override  
  208.         public void onClick(View view) {  
  209.             int tag=(Integer) view.getTag();  
  210.               
  211.             switch(tag){  
  212.             //全部清除  
  213.             case 14:  
  214.                 x=0;  
  215.                 y=0;  
  216.                 text="0.0";  
  217.                 zestatus=true;  
  218.                 break;  
  219.             case 15:  
  220.                 text=text.substring(0,text.length()-1);  
  221.                 break;  
  222.             case 16:  
  223.                 x=0;  
  224.                 text="0.0";  
  225.                 zestatus=true;  
  226.                 break;  
  227.             case 17:  
  228.                 count++;  
  229.                 if(count!=0&&count%2==0){  
  230.                     text=text.substring(1);  
  231.                 }  
  232.                 else if(count%2==1){  
  233.                     text="-"+text;  
  234.                 }  
  235.                 break;  
  236.             }  
  237.                 textview.setText(text);  
  238.                 textview.setSelection(text.length());  
  239.                   
  240.         }  
  241.     };  
  242.   
  243.       
  244.     /** 
  245.      * 配置菜单组件 
  246.      */  
  247.     public boolean onCreateOptionsMenu(Menu menu) {  
  248.         menu.add(001,"退出" );  
  249.         menu.add(0,1,2,"关于");  
  250.         return true;  
  251.     }  
  252.   
  253.     /** 
  254.      * 触发相应的事件 
  255.      */  
  256.     public boolean onOptionsItemSelected(MenuItem item) {  
  257.         switch(item.getItemId())  
  258.         {  
  259.         case 0:  
  260.             finish();  
  261.         case 1:  
  262.             Toast.makeText(MainActivity.this""1).show();  
  263.         }  
  264.         return super.onOptionsItemSelected(item);  
  265.     }  
  266. }