Android-简便计算器


自己做的简单计算器。

源代码项目下载地址。

FTP地址:ftp://www.bkjia.com

用户名:www.bkjia.com

密码:www.muu.cc

在 2011年LinuxIDC.com\8月\Android-简便计算器

下载方法见这里 http://www.bkjia.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;  
  252.                     }  
  253.                 }  
  254.             }  
  255.         });  
  256. //c   
  257.             c.setOnClickListener(new View.OnClickListener() {  
  258.             public void onClick(View arg0) {  
  259.                 zd();  
  260.                     text.setText("");  
  261.                     lable.setText("");  
  262.                     num_a = 0.0;  
  263.                     temp = null;  
  264.                     clickable = false;  
  265.                     isDot = true;  
  266.                 }  
  267.         });  
  268. //cm   
  269.             cm.setOnClickListener(new View.OnClickListener() {  
  270.                 public void onClick(View arg0) {      
  271.                     zd();  
  272.                         memoryd = memoryi = 0;  
  273.                     }  
  274.             });  
  275. //m   
  276.             m.setOnClickListener(new View.OnClickListener() {  
  277.                 public void onClick(View arg0) {      
  278.                     zd();  
  279.                     if (text.getText().toString() != " "){  
  280.                         boolean isDot = false;  
  281.                         for (int i = 0; i < text.getText().length(); i++){  
  282.                             if ('.' == text.getText().charAt(i)) {  
  283.                                 isDot = true;  
  284.                                 break;  
  285.                             }  
  286.                         }  
  287.                         if(text.getText().toString().equalsIgnoreCase("")){  
  288.                             memoryd = memoryi = 0;  
  289.                         }  
  290.                         else{  
  291.                         if (isDot == true) {  
  292.                             memoryd = Double.parseDouble(text.getText().toString());  
  293.                             memoryi = 0;   
  294.                         }  
  295.                         else {    
  296.                             memoryi = Integer.parseInt(text.getText().toString());  
  297.                             memoryd = 0;  
  298.                         }  
  299.                         }  
  300.                         clickable = false;  
  301.                     }  
  302.                     }  
  303.             });  
  304. //ms   
  305.             mshow.setOnClickListener(new View.OnClickListener() {  
  306.                 public void onClick(View arg0) {  
  307.                     zd();  
  308.                     if (memoryd != 0) {  
  309.                         text.setText(Double.toString(memoryd));  
  310.                     }  
  311.                     if (memoryi != 0) {  
  312.                         text.setText(Integer.toString(memoryi));  
  313.                     }  
  314.                     clickable = false;  
  315.                 }  
  316.             });  
  317. //取反   
  318.             tf.setOnClickListener(new View.OnClickListener() {  
  319.                 public void onClick(View arg0) {  
  320.                         zd();  
  321.                     if(text.getText().toString().equalsIgnoreCase("")){  
  322.                     }else{  
  323.                     boolean isNumber = true;  
  324.                       String s = text.getText().toString();  
  325.                       for (int i = 0; i < s.length(); i++)  
  326.                         if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'  
  327.                               || s.charAt(i) == '.' || s.charAt(i) == '-')) {  
  328.                             isNumber = false;  
  329.                             break;  
  330.                         }  
  331.                       if (isNumber == true) {  
  332.                         //如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉   
  333.                         if (s.charAt(0) == '-') {  
  334.                             text.setText("");  
  335.                             for (int i = 1; i < s.length(); i++) {  
  336.                               char a = s.charAt(i);  
  337.                               text.setText(text.getText().toString() + a);  
  338.                             }  
  339.                         }  
  340.                         //如果当前字符串第一个字符不是符号,则添加一个符号在首字母处   
  341.                         else  
  342.                             text.setText('-' + s);  
  343.                       }  
  344.                 }}  
  345.             });  
  346.               
  347. //back   
  348.             back.setOnClickListener(new View.OnClickListener() {  
  349.                 public void onClick(View arg0) {  
  350.                     zd();  
  351.                     if(clickable == true)  
  352.                     if (text.getText().toString() != "") {  
  353.                           String s = text.getText().toString();  
  354.                           text.setText("");  
  355.                           for (int i = 0; i < s.length() - 1; i++) {  
  356.                             char a = s.charAt(i);  
  357.                             text.setText(text.getText().toString() + a);  
  358.                           }  
  359.                     }  
  360.                 }  
  361.             });       
  362.     }  
  363.     public void zd(){   //振动的方法   
  364.         if(isVib==true){  
  365.         long[] pattern = {020010}; // OFF/ON/OFF/ON...      
  366.         vibrator.vibrate(pattern, -1);  
  367.         }else{  
  368.           
  369.             vibrator.cancel();  
  370.         }  
  371.     }  
  372.     public void show(String i){                                                 //1-9的数   
  373.         zd();  
  374.         if (clickable == false) {  
  375.             text.setText("");  
  376.             text.setText(text.getText() + i);  
  377.             clickable = true;  
  378.         } else {  
  379.             text.setText(text.getText() + i);  
  380.         }  
  381.     }  
  382.     public boolean onCreateOptionsMenu(Menu menu) {    //添加菜单   
  383.         // TODO Auto-generated method stub   
  384.         menu.add(011,R.string.exit);  
  385.         menu.add(022, R.string.about);  
  386.         menu.add(1,3,3,"开/关震动");  
  387.         return super.onCreateOptionsMenu(menu);  
  388.     }  
  389.     public boolean onOptionsItemSelected(MenuItem item) {  //设置菜单事件   
  390.         switch(item.getItemId()){  
  391.         case 1:  
  392.             finish();  
  393.             break;  
  394.         case 2:  
  395.             AlertDialog.Builder dialog = new AlertDialog.Builder(this);  
  396.             dialog.setTitle("关于").setMessage("This is a black cool convenient calculator!\nBy:没落凄凉").show();  
  397.             break;  
  398.         case 3:  
  399.             if(isVib){  
  400.                 isVib=false;  
  401.             }else{  
  402.                 isVib=true;  
  403.             }  
  404.         }  
  405.           
  406.           
  407.         return super.onOptionsItemSelected(item);     
  408.     }  
  409. }   
  • 1
  • 2
  • 下一页

相关内容