Welcome 微信登录

首页 / 操作系统 / Linux / Android-简便计算器

自己做的简单计算器。源代码项目下载地址。 FTP地址:ftp://www.linuxidc.com用户名:www.linuxidc.com密码:www.muu.cc在 2011年LinuxIDC.com8月Android-简便计算器下载方法见这里 http://www.linuxidc.net/thread-1187-1-1.html就一个activity
  1. package zhang.calculator;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.os.Bundle;  
  6. import android.os.Vibrator;  
  7. import android.view.Menu;  
  8. import android.view.MenuItem;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12. import android.widget.Toast;  
  13. public class MyCalculator extends Activity {   
  14.     private Vibrator vibrator;    
  15.     private Double num_a;  
  16.     private Double num_b;  
  17.     private TextView text = null;  
  18.     private String temp = null;// 计算符号   
  19.     private boolean isDot = true;// 小数点控制   
  20.     private boolean clickable = true;// 标志是否按过计算按钮   
  21.     private double memoryd; // 使用内存中存储的数字   
  22.     private int memoryi;  
  23.     private TextView lable=null;  
  24.     private boolean isVib=true;  
  25.   
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.calculator_main);  
  29.         final Button num1 = (Button) findViewById(R.id.num1);  
  30.         final Button num2 = (Button) findViewById(R.id.num2);  
  31.         final Button num3 = (Button) findViewById(R.id.num3);  
  32.         final Button num4 = (Button) findViewById(R.id.num4);  
  33.         final Button num5 = (Button) findViewById(R.id.num5);  
  34.         final Button num6 = (Button) findViewById(R.id.num6);  
  35.         final Button num7 = (Button) findViewById(R.id.num7);  
  36.         final Button num8 = (Button) findViewById(R.id.num8);  
  37.         final Button num9 = (Button) findViewById(R.id.num9);  
  38.         Button num0 = (Button) findViewById(R.id.zero);  
  39.         Button point=(Button) findViewById(R.id.point);  
  40.         Button mul=(Button)findViewById(R.id.mul);  
  41.         Button sub=(Button)findViewById(R.id.sub);  
  42.         Button add=(Button)findViewById(R.id.add);  
  43.         Button div=(Button)findViewById(R.id.div);  
  44.         Button sqrt=(Button)findViewById(R.id.sqrt);  
  45.         Button equal=(Button) findViewById(R.id.equal);  
  46.         Button c=(Button) findViewById(R.id.clear);  
  47.         Button cm=(Button)findViewById(R.id.clearm);  
  48.         Button m=(Button)findViewById(R.id.memory);  
  49.         Button tf=(Button)findViewById(R.id.tf);  
  50.         Button mshow=(Button)findViewById(R.id.showmemory);  
  51.         Button back=(Button)findViewById(R.id.back);  
  52.         text = (TextView) findViewById(R.id.showText);  
  53.         lable = (TextView) findViewById(R.id.lable);  
  54.           
  55.         vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);     
  56.           
  57. //0        
  58.         num0.setOnClickListener(new Button.OnClickListener() {  
  59.             public void onClick(View v) {             
  60.                 zd();  
  61.                 if(text.getText().toString().equalsIgnoreCase("0")){  
  62.                   
  63.                 }else{  
  64.                 if (clickable == false) {                         
  65.                     text.setText("");  
  66.                     text.setText(text.getText().toString()+"0");  
  67.                     clickable = true;  
  68.                 } else {  
  69.                     text.setText(text.getText().toString()+"0");  
  70.                 }  
  71.             }     
  72.         }  
  73.     });  
  74. //1   
  75.         num1.setOnClickListener(new View.OnClickListener() {  
  76.             public void onClick(View v) {  
  77.                 show(num1.getText().toString());  
  78.             }  
  79.         });  
  80. // 2   
  81.         num2.setOnClickListener(new View.OnClickListener() {  
  82.             public void onClick(View v) {  
  83.                 show(num2.getText().toString());  
  84.             }  
  85.         });  
  86. // 3   
  87.         num3.setOnClickListener(new View.OnClickListener() {  
  88.             public void onClick(View v) {  
  89.                 show(num3.getText().toString());  
  90.             }  
  91.         });  
  92. // 4   
  93.         num4.setOnClickListener(new View.OnClickListener() {  
  94.             public void onClick(View v) {  
  95.                 show(num4.getText().toString());  
  96.             }  
  97.         });  
  98. // 5   
  99.         num5.setOnClickListener(new View.OnClickListener() {  
  100.             public void onClick(View v) {  
  101.                 show(num5.getText().toString());  
  102.             }  
  103.         });  
  104. // 6   
  105.         num6.setOnClickListener(new View.OnClickListener() {  
  106.             public void onClick(View v) {  
  107.                 show(num6.getText().toString());  
  108.             }  
  109.         });  
  110. // 7   
  111.         num7.setOnClickListener(new View.OnClickListener() {  
  112.             public void onClick(View v) {  
  113.                 show(num7.getText().toString());  
  114.             }  
  115.         });  
  116. // 8   
  117.         num8.setOnClickListener(new View.OnClickListener() {  
  118.             public void onClick(View v) {  
  119.                 show(num8.getText().toString());  
  120.             }  
  121.         });  
  122. // 9   
  123.         num9.setOnClickListener(new View.OnClickListener() {  
  124.             public void onClick(View v) {  
  125.                 show(num9.getText().toString());  
  126.             }  
  127.         });  
  128. //.   
  129.         point.setOnClickListener(new View.OnClickListener() {  
  130.             public void onClick(View arg0) {  
  131.                 zd();  
  132.                     if(text.getText().toString().equalsIgnoreCase("")){  
  133.                     }else{    
  134.                         if (text.getText().toString()!=""&&isDot == true) {  
  135.                             text.setText(text.getText()+".");  
  136.                             isDot = false;  
  137.                         }else{  
  138.                         text.setText(text.getText().toString());  
  139.                         }     
  140.                     }  
  141.                 }  
  142.             });  
  143. //加   
  144.         add.setOnClickListener(new View.OnClickListener() {  
  145.             public void onClick(View v) {  
  146.                 zd();  
  147.                 if(text.getText().toString().equalsIgnoreCase("")){  
  148.                 }else{  
  149.                 if (text.getText() != null){  
  150.                     num_a =Double.parseDouble(text.getText().toString());  
  151.                     temp = "add";  
  152.                     clickable = false;  
  153.                     isDot=true;  
  154.                     lable.setText("+");  
  155.                     }  
  156.                 }  
  157.             }  
  158.     });  
  159. //减   
  160.         sub.setOnClickListener(new View.OnClickListener() {  
  161.             public void onClick(View v) {  
  162.                 zd();  
  163.                 if(text.getText().toString().equalsIgnoreCase("")){  
  164.                 }else{  
  165.                     if (text.getText() != null){  
  166.                         num_a =Double.parseDouble(text.getText().toString());  
  167.                         temp = "sub";  
  168.                         clickable = false;  
  169.                         lable.setText("—");  
  170.                     }else{  
  171.                         text.setText("—");  
  172.                         }  
  173.                 isDot=true;  
  174.         }  
  175.     }  
  176. });  
  177. //乘   
  178.         mul.setOnClickListener(new View.OnClickListener() {  
  179.             public void onClick(View v) {  
  180.                 zd();  
  181.                 if(text.getText().toString().equalsIgnoreCase("")){  
  182.                 }else{  
  183.                 if (text.getText() != null){  
  184.                     num_a =Double.parseDouble(text.getText().toString());  
  185.                     temp = "mul";  
  186.                     lable.setText("×");  
  187.                     clickable = false;  
  188.                     isDot=true;  
  189.                     }  
  190.                 }  
  191.             }  
  192.         });  
  193. // 除   
  194.         div.setOnClickListener(new View.OnClickListener() {  
  195.             public void onClick(View v) {  
  196.                 zd();  
  197.                 if(text.getText().toString().equalsIgnoreCase("")){  
  198.                 }else{  
  199.                 if (text.getText() != null){  
  200.                     num_a =Double.parseDouble(text.getText().toString());  
  201.                     temp = "div";  
  202.                     lable.setText("÷");  
  203.                     clickable = false;  
  204.                     isDot=true;  
  205.                     }  
  206.                 }  
  207.             }  
  208.         });  
  209. //开方   
  210.         sqrt.setOnClickListener(new View.OnClickListener() {  
  211.             public void onClick(View arg0) {  
  212.                 zd();  
  213.                 if(text.getText().toString().equalsIgnoreCase("")){  
  214.                 }else{  
  215.                 String s = text.getText().toString();  
  216.                   if (s.charAt(0) == "-") {  
  217.                       Toast.makeText(MyCalculator.this"负数不能开根号!!",Toast.LENGTH_SHORT).show();  
  218.                     clickable = false;  
  219.                   } else {  
  220.                     text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText().toString()))));  
  221.         lable.setText("√");  
  222.                     clickable = false;  
  223.                     }  
  224.                 }  
  225.             }  
  226.         });  
  227.           
  228. //等于   
  229.         equal.setOnClickListener(new View.OnClickListener() {  
  230.             public void onClick(View v) {  
  231.                 zd();  
  232.                 if (temp != null && text.getText() != null){  
  233.                     num_b = (Double.parseDouble(text.getText().toString()));  
  234.                     if (temp == "add"){  
  235.                         text.setText(Float.toString((float) (num_a + num_b)));  
  236.                         temp = null;  
  237.                     }else if (temp == "sub"){  
  238.                         text.setText(Float.toString((float) (num_a - num_b)));  
  239.                         temp = null;  
  240.                     }else if (temp == "mul"){  
  241.                         text.setText(Float.toString((float) (num_a * num_b)));  
  242.                         temp = null;  
  243.                     }else if (temp == "div"){  
  244.                         text.setText(Float.toString((float) (num_a / num_b)));  
  245.                         temp = null;  
  246.                     }  
  247.                     clickable = false;  
  248.                     if(text.getText().toString()==""){  
  249.                         isDot =true;  
  250.                     }else{  
  251.                         isDot =false;