简单的计算器编写 基于Android 2.2


简单的计算器编写 基于Android 2.2

  1. package com.jiajia.calculate;  
  2.   
  3. import java.io.IOException;  
  4.   
  5.   
  6. import android.app.Activity;  
  7. import android.app.AlertDialog;  
  8. import android.app.AlertDialog.Builder;  
  9. import android.content.DialogInterface;  
  10.   
  11.   
  12. import android.os.Bundle;  
  13.   
  14. import android.view.KeyEvent;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18.   
  19. import android.widget.TextView;  
  20.   
  21. public class CalculateActivity extends Activity {  
  22.     /** Called when the activity is first created. */  
  23.     //定义能显示的最大个数   
  24.     public static final int Maxindex=14;  
  25.     public static final double MAX=999999999999.9;  
  26.       
  27.     private Button button1,button2,button3,button4,button5,  
  28.     button6,button7,button8,button9,button0,button00,buttonadd,  
  29.     buttonsub,buttonmul,buttondiv,buttonM_add,buttonM_sub,  
  30.     buttonMR,buttonMC,buttonclear,buttondot,buttonequale,buttonBack;  
  31.       
  32.     private TextView Text1;  
  33.     private TextView Text21;  
  34.     private TextView Text22;  
  35.     private TextView Text31;  
  36.     private TextView Text32;  
  37.       
  38.     private String factor1="0";  
  39.     private String factor2="0";  
  40.     private String memory="";  
  41.     private String result="";  
  42.   
  43.     private String M1="";  
  44.     private String M2="";  
  45.     private String M3="";     
  46.       
  47.     private String op="";  
  48.     private String mop="";  
  49.     private double d_result=0.0;  
  50.     private double d_M1=0.0;  
  51.     private double d_M2=0.0;  
  52.     private double d_M3=0.0;  
  53.       
  54.     @Override  
  55.     public void onCreate(Bundle savedInstanceState) {  
  56.         super.onCreate(savedInstanceState);  
  57.         setContentView(R.layout.main);  
  58.         button0=(Button)findViewById(R.id.button0);  
  59.         button0.setOnClickListener(new listener());  
  60.         button00=(Button)findViewById(R.id.button00);  
  61.         button00.setOnClickListener(new listener());  
  62.         buttondot=(Button)findViewById(R.id.buttondot);  
  63.         buttondot.setOnClickListener(new listener());  
  64.         buttonequale=(Button)findViewById(R.id.buttonequale);  
  65.         buttonequale.setOnClickListener(new listener());  
  66.         button1=(Button)findViewById(R.id.button1);  
  67.         button1.setOnClickListener(new listener());  
  68.         button2=(Button)findViewById(R.id.button2);  
  69.         button2.setOnClickListener(new listener());  
  70.         button3=(Button)findViewById(R.id.button3);  
  71.         button3.setOnClickListener(new listener());  
  72.         button4=(Button)findViewById(R.id.button4);  
  73.         button4.setOnClickListener(new listener());  
  74.         button5=(Button)findViewById(R.id.button5);  
  75.         button5.setOnClickListener(new listener());  
  76.         button6=(Button)findViewById(R.id.button6);  
  77.         button6.setOnClickListener(new listener());  
  78.         button7=(Button)findViewById(R.id.button7);  
  79.         button7.setOnClickListener(new listener());  
  80.         button8=(Button)findViewById(R.id.button8);  
  81.         button8.setOnClickListener(new listener());  
  82.         button9=(Button)findViewById(R.id.button9);  
  83.         button9.setOnClickListener(new listener());  
  84.         buttonadd=(Button)findViewById(R.id.buttonadd);  
  85.         buttonadd.setOnClickListener(new listener());  
  86.         buttonsub=(Button)findViewById(R.id.buttonsub);  
  87.         buttonsub.setOnClickListener(new listener());  
  88.         buttonmul=(Button)findViewById(R.id.buttonmul);  
  89.         buttonmul.setOnClickListener(new listener());  
  90.         buttondiv=(Button)findViewById(R.id.buttondiv);  
  91.         buttondiv.setOnClickListener(new listener());  
  92.         buttonM_add=(Button)findViewById(R.id.buttonM_add);  
  93.         buttonM_add.setOnClickListener(new listener());  
  94.         buttonM_sub=(Button)findViewById(R.id.buttonM_sub);  
  95.         buttonM_sub.setOnClickListener(new listener());  
  96.         buttonMR=(Button)findViewById(R.id.buttonMR);  
  97.         buttonMR.setOnClickListener(new listener());  
  98.         buttonMC=(Button)findViewById(R.id.buttonMC);  
  99.         buttonMC.setOnClickListener(new listener());  
  100.         buttonclear=(Button)findViewById(R.id.buttonclear);  
  101.         buttonclear.setOnClickListener(new listener());  
  102.         buttonBack=(Button)findViewById(R.id.buttonback);  
  103.         buttonBack.setOnClickListener(new buttonBack());  
  104.         Text1=(TextView)findViewById(R.id.TextView1);  
  105.         Text21=(TextView)findViewById(R.id.TextView21);  
  106.         Text22=(TextView)findViewById(R.id.TextView22);  
  107.         Text31=(TextView)findViewById(R.id.TextView31);  
  108.         Text32=(TextView)findViewById(R.id.TextView32);  
  109.          
  110.         Text1.setText("0");  
  111.         Text21.setText("");  
  112.         Text22.setText("");  
  113.         Text31.setText("");  
  114.         Text32.setText("0");  
  115.           
  116.     }  
  117.       
  118.  class buttonBack implements OnClickListener{  
  119.        
  120.   
  121.     public void onClick(View arg0) {  
  122.         //onKeyDown(KeyEvent.KEYCODE_BACK, null);   
  123.        AlertDialog.Builder builder=new Builder(CalculateActivity.this);  
  124.        builder.setTitle(R.string.Dia_Title);  
  125.        builder.setMessage(R.string.Dia_msg);  
  126.        builder.setPositiveButton(R.string.Dia_Yes, new DialogInterface.OnClickListener() {  
  127.           
  128.         public void onClick(DialogInterface dialog, int which) {  
  129.             // TODO Auto-generated method stub   
  130.             dialog.dismiss();  
  131.             CalculateActivity.this.finish();  
  132.         }  
  133.     });  
  134.        builder.setNegativeButton(R.string.Dia_Cancle, new DialogInterface.OnClickListener() {  
  135.           
  136.         public void onClick(DialogInterface dialog, int which) {  
  137.             // TODO Auto-generated method stub   
  138.             dialog.dismiss();  
  139.         }  
  140.     });  
  141.      
  142.     builder.create().show();  
  143.     }  
  144.  }  
  145. /*  public void clearWallpaper(){ 
  146.         try { 
  147.             super. clearWallpaper(); 
  148.         } catch (IOException e) { 
  149.             // TODO Auto-generated catch block 
  150.             e.printStackTrace(); 
  151.         } 
  152.     }*/  
  153.     class listener implements android.view.View.OnClickListener{  
  154.         //获取操作符计算结果   
  155.       public void operator(String op,double d1,double d2){  
  156.                   if(op.compareTo("+")==0){d_result=d1+d2;}  
  157.                   if(op.compareTo("-")==0){d_result=d1-d2;}  
  158.                   if(op.compareTo("x")==0){d_result=d1*d2;}  
  159.                   if(op.compareTo("/")==0&&d2!=0){d_result=d1/d2;}  
  160.                   if(op.compareTo("/")==0&&d2==0){this.errorOp(); }  
  161.             }  
  162.       //获取代表M的各个View的值   
  163.       public void getViewM(){  
  164.             M1=Text1.getText().toString();  
  165.             M2=Text22.getText().toString();  
  166.             if(M2.equals("")||M2.equals(" ")||M2.equals(null)){M2="0";}  
  167.             M3=Text32.getText().toString();  
  168.             op=Text31.getText().toString();  
  169.             d_M1=Double.parseDouble(M1);  
  170.             d_M2=Double.parseDouble(M2);  
  171.             d_M3=Double.parseDouble(M3);  
  172.       }  
  173.   
  174.       //出错处理   
  175.       public void errorOp(){  
  176.           d_result=0;  
  177.           factor1="";  
  178.           factor2="0";  
  179.           result="0";  
  180.           Text21.setText("E");  
  181.           Text32.setText( factor2);  
  182.       }  
  183.       //判断result是否符合计算结果   
  184.       public void judgeRes(){  
  185.           if((result.endsWith(".0"))){  
  186.               result= result.substring(0, result.lastIndexOf("."));  
  187.               }  
  188.           if(result.length()>Maxindex&& d_result<=MAX){  
  189.               result=result.substring(0,Maxindex);  
  190.           }  
  191.           if(d_result>MAX){  
  192.               this.errorOp();  
  193.               }  
  194.       }  
  195.       //相当于等号操作的个操作符运算(包括等号)   
  196.       public void equale(){  
  197.           this.getViewM();  
  198.           this.operator(op, d_M2, d_M3);  
  199.           result=String.valueOf(d_result);  
  200.           this.judgeRes();  
  201.           Text32.setText(result);  
  202.           System.out.println(" result = "+ result);  
  203.           Text31.setText(op);  
  204.      //     result=factor1;   
  205.           
  206.           factor1="0";  
  207.           factor2="";  
  208.            
  209.            
  210.       }  
  211.      //连续的操作符运算    
  212.       public void CouOP(String  op) {  
  213.               
  214.             M3=Text32.getText().toString();  
  215.             Text31.setText(op);  
  216.             factor1=M3;  
  217.             Text22.setText(factor1);  
  218.             factor1="0";  
  219.             factor2="0";  
  220.             Text32.setText(factor2);  
  221.       }  
  222.      //M操作的运算   
  223.       public void MOperator(double d_M2,double d_M3,String mop){  
  224.         this.operator(op, d_M2, d_M3);  
  225.         if(mop.equals("+")){d_result+=d_M1; }  
  226.         if(mop.equals("-")){d_result=d_M1-d_result;}  
  227.         result=String.valueOf(d_result);  
  228.         this.judgeRes();  
  229.         Text32.setText(result);  
  230.         Text1.setText(result);  
  231.         Text22.setText("");  
  232.         factor1="";  
  233.         factor2="";  
  234.       }  
  235.         
  236.       //点击事件   
  237.         public void onClick(View v) {  
  238.             switch(v.getId()){  
  239.             case R.id.button0:  
  240.                  Text21.setText("");  
  241.                 if(factor1.equals("0")){  
  242.                     factor1="0";  
  243.                     Text32.setText(factor1);  
  244.                 }else{  
  245.                 factor1+="0";  
  246.                 Text32.setText(factor1);  
  247.                 }  
  248.                   
  249.                 break;  
  250.             case R.id.button1:  
  251.                  Text21.setText("");  
  252.                 if(factor1.equals("0")){  
  253.                     factor1="1";  
  254.                     Text32.setText(factor1);  
  255.                 }else{  
  256.                 factor1+="1";  
  257.                 Text32.setText(factor1);  
  258.                 }  
  259.                 result=factor1;  
  260.                    this.judgeRes();  
  261.                    break;  
  262.             case R.id.button2:  
  263.                  Text21.setText("");  
  264.                 if(factor1.compareTo("0")==0){  
  265.                     factor1="2";  
  266.                     Text32.setText(factor1);  
  267.                 }else{  
  268.                 factor1+="2";  
  269.                 Text32.setText(factor1);  
  270.                 }  
  271.                 result=factor1;  
  272.                this.judgeRes();  
  273.                 break;  
  274.             case R.id.button3:  
  275.                  Text21.setText("");  
  276.                 if(factor1.compareTo("0")==0){  
  277.                     factor1="3";  
  278.                     Text32.setText(factor1);  
  279.                 }else{  
  280.                 factor1+="3";  
  281.                 Text32.setText(factor1);  
  282.                 }     
  283.                 result=factor1;  
  284.              this.judgeRes();  
  285.                 break;  
  286.             case R.id.button4:  
  287.                  Text21.setText("");  
  288.                 if(factor1.compareTo("0")==0){  
  289.                     factor1="4";  
  290.                     Text32.setText(factor1);  
  291.                 }else{  
  292.                 factor1+="4";  
  293.                 Text32.setText(factor1);  
  294.                 }  
  295.                 break;  
  296.             case R.id.button5:  
  297.                  Text21.setText("");  
  298.                 if(factor1.compareTo("0")==0){  
  299.                     factor1="5";  
  300.                     Text32.setText(factor1);  
  301.                 }else{  
  302.                 factor1+="5";  
  303.                 Text32.setText(factor1);  
  304.                 }  
  305.                 break;  
  306.             case R.id.button6:  
  307.                  Text21.setText("");  
  308.                 if(factor1.compareTo("0")==0){  
  309.                     factor1="6";  
  310.                     Text32.setText(factor1);  
  311.                 }else{  
  312.                 factor1+="6";  
  313.                 Text32.setText(factor1);  
  314.                 }  
  315.                 break;  
  316.             case R.id.button7:  
  317.                  Text21.setText("");  
  318.                 if(factor1.compareTo("0")==0){  
  319.                     factor1="7";  
  320.                     Text32.setText(factor1);  
  321.                 }else{  
  322.                 factor1+="7";  
  323.                 Text32.setText(factor1);  
  324.                 }  
  325.                 break;  
  326.             case R.id.button8:  
  327.                  Text21.setText("");  
  328.                 if(factor1.compareTo("0")==0){   
  329.                     factor1="8";  
  330.                     Text32.setText(factor1);  
  331.                 }else{  
  332.                 factor1+="8";  
  333.                 Text32.setText(factor1);  
  334.                 }  
  335.                 break;  
  336.             case R.id.button9:  
  337.                  Text21.setText("");  
  338.                 if(factor1.compareTo("0")==0){  
  339.                     factor1="9";  
  340.                     Text32.setText(factor1);  
  341.                 }else{  
  342.                   factor1+="9";  
  343.                   Text32.setText(factor1);  
  344.                 }  
  345.                 break;  
  346.  //00不能出现在最前面                  
  347.             case R.id.button00:  
  348.                  Text21.setText("");  
  349.                 if(factor1.equals("0")||factor1.equals("")){  
  350.                     System.out.println("factor1 = "+factor1);  
  351.                      factor1="0";  
  352.                      Text32.setText(factor1);  
  353.                 }else{  
  354.                 factor1+="00";  
  355.                 Text32.setText(factor1);  
  356.                 }  
  357.                 break;  
  358.  //.   
  359.             case R.id.buttondot:  
  360.                  Text21.setText("");  
  361.                  if(!factor1.contains(".")){  
  362.                     factor1+=".";  
  363.                     Text32.setText(factor1);}  
  364. //               else if((factor1.startsWith("0", 0))){   
  365. //                      factor1="0.";   
  366. //               }    
  367.                  else{  
  368.                     Text32.setText(factor1);  
  369.                  }  
  370.               
  371.                 break;  
  372.             case R.id.buttonadd:  
  373.                  Text21.setText("");  
  374.                  M2=Text22.getText().toString();  
  375.                 if(M2.equals("")){  
  376.                 op="+";  
  377.                 this.CouOP(op);  
  378.                 }  
  379.                 else{  
  380.                     this.equale();   
  381.                     Text22.setText(result);  
  382.                     Text32.setText(factor1);      
  383.                     op="+";  
  384.                     Text31.setText(op);  
  385.                 }  
  386.                 break;  
  387.             case R.id.buttonsub:  
  388.                  Text21.setText("");  
  389.                  M2=Text22.getText().toString();  
  390.                 if(M2.equals("")){  
  391.                     op="-";  
  392.                     this.CouOP(op);  
  393.                     }  
  394.               else {  
  395.                     this.equale();  
  396.                     Text22.setText(result);  
  397.                     Text32.setText(factor1);      
  398.                     op="-";  
  399.                     Text31.setText(op);  
  400.                 }  
  401.                 break;  
  402.             case R.id.buttonmul:  
  403.                 M2=Text22.getText().toString();  
  404.                 if(M2.equals("")){  
  405.                     op="x";  
  406.                     this.CouOP(op);  
  407.                 }else {  
  408.                     this.equale();  
  409.                     Text22.setText(result);  
  410.                     Text32.setText(factor1);      
  411.                     op="x";  
  412.                     Text31.setText(op);  
  413.                 }  
  414.                 break;  
  415.             case R.id.buttondiv:  
  416.                 M2=Text22.getText().toString();  
  417.                 if(M2.equals("")){  
  418.                     op="/";  
  419.                     this.CouOP(op);  
  420.                 }else {  
  421.                     this.equale();  
  422.                     Text22.setText(result);  
  423.                     Text32.setText(factor1);      
  424.                     op="/";  
  425.                     Text31.setText(op);  
  426.                 }  
  427.                 break;  
  428.   //M+         
  429.    //当作M2=“ ”||“”||null的时候 出现了问题。。因为直接调用的getview    
  430.      //所以里面带有将M2转化为d_M2的时候就出问题了,,那是不能之间转化的   
  431.           
  432.             case R.id.buttonM_add:  
  433.                 mop="+";  
  434.                 this.getViewM();  
  435.                 this.MOperator(d_M2,d_M3,mop);  
  436.               
  437.              break;  
  438.  //M-                  
  439.             case R.id.buttonM_sub:  
  440.                 mop="-";  
  441.                 this.getViewM();  
  442.                 this.MOperator(d_M2,d_M3,mop);  
  443.               
  444.                   break;  
  445. //MR :将View1中的数据显示在View3中                    
  446.             case R.id.buttonMR:  
  447.             this.getViewM();  
  448.              Text1.setText(M1);  
  449.                  d_result=d_M1;  
  450.                  result=String.valueOf(d_result);  
  451.                  this.judgeRes();  
  452.                  Text32.setText(result);  
  453.             case R.id.buttonMC:  
  454.                // factor1=Text1.getText().toString();   
  455.                 factor1="0";  
  456.                 factor2="0";  
  457.                 d_result=0;  
  458.                 Text1.setText(factor1);  
  459.                 break;  
  460.   //清除view3中数据   
  461.             case R.id.buttonclear:  
  462.                 factor1="0";  
  463.                 op="";  
  464.                 factor2="0";  
  465.                 Text21.setText("");  
  466.                 Text32.setText("0");  
  467.                 Text22.setText("");  
  468.                 Text31.setText(op);  
  469.                  break;  
  470.             case R.id.buttonequale:  
  471.                this.equale();  
  472.                Text22.setText(factor2);  
  473.                op="=";  
  474.                Text31.setText(op);  
  475.         break;  
  476.             }         
  477.                }  
  478.  }  
  479. }  

 

相关内容