A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯超 高级黑马   /  2013-8-22 04:21  /  1993 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

头一次搞学习搞的这个晚。。。
       貌似好久都没来这个论坛了,可能是放暑假的原因吧,离开下越来越近了,意味着校园时光逐渐离我们远去。。
运用面向对象的思想+单例模式以下是主要代码
  1. package com.feng;

  2. import android.app.Activity;
  3. import android.widget.Button;
  4. import android.widget.EditText;

  5. public class Buttons extends Activity {
  6.         private Buttons() {}
  7.         private static Buttons instance;
  8.     EditText et_play; // 显示
  9.     Button bt_0;
  10.     Button bt_1;
  11.     Button bt_2;
  12.     Button bt_3;
  13.     Button bt_4;
  14.     Button bt_5;
  15.     Button bt_6;
  16.     Button bt_7;
  17.         Button bt_8;
  18.         Button bt_9;
  19.         Button bt_dot;
  20.         Button bt_back;
  21.         Button bt_add;
  22.         Button bt_sub;
  23.         Button bt_multiply;
  24.         Button bt_div;
  25.         Button clean;
  26.         Button bt_equal;
  27.        
  28.         public static Buttons getInstance() {
  29.                 if(instance == null)
  30.                         instance = new Buttons();
  31.                 return instance;
  32.         }
  33. }
复制代码
  1. package com.feng;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;

  9. public class MainActivity extends Activity {
  10.         final static Buttons bt = Buttons.getInstance();
  11.         static StringBuffer str_display = new StringBuffer();
  12.         static int judge_point = 0;  //判断是否打了小数点 0表示没打小数点 1表示打了一个 2表示不能在打
  13.         static double num1 = 0.0;
  14.         static double num2 = 0.0;
  15.         static char sign;  //判断按了什么计算符合
  16.         @Override
  17.         protected void onCreate(Bundle savedInstanceState) {
  18.                 super.onCreate(savedInstanceState);
  19.                 setContentView(R.layout.lay_layout);

  20.             bt.et_play = (EditText) findViewById(R.id.et);;
  21.             bt.et_play.setText("0.0");
  22.             
  23.             bt.bt_0 = (Button) findViewById(R.id.bt_0);
  24.             bt.bt_1 = (Button) findViewById(R.id.bt_1);
  25.             bt.bt_2 = (Button) findViewById(R.id.bt_2);
  26.             bt.bt_3 = (Button) findViewById(R.id.bt_3);
  27.             bt.bt_4 = (Button) findViewById(R.id.bt_4);
  28.             bt.bt_5 = (Button) findViewById(R.id.bt_5);
  29.             bt.bt_6 = (Button) findViewById(R.id.bt_6);
  30.             bt.bt_7 = (Button) findViewById(R.id.bt_7);
  31.             bt.bt_8 = (Button) findViewById(R.id.bt_8);
  32.             bt.bt_9 = (Button) findViewById(R.id.bt_9);
  33.             bt.bt_dot = (Button) findViewById(R.id.bt_point);
  34.             bt.bt_back = (Button) findViewById(R.id.bt_back);
  35.             bt.clean = (Button)findViewById(R.id.bt_clear);
  36.             bt.bt_add = (Button)findViewById(R.id.bt_add);
  37.             bt.bt_sub = (Button)findViewById(R.id.bt_sub);
  38.             bt.bt_multiply = (Button)findViewById(R.id.bt_multiply);
  39.             bt.bt_div = (Button)findViewById(R.id.bt_divide);
  40.             bt.bt_equal = (Button)findViewById(R.id.bt_equal);
  41.             
  42.             onClickListener(bt.bt_0, "0");
  43.             onClickListener(bt.bt_1, "1");
  44.             onClickListener(bt.bt_2, "2");
  45.             onClickListener(bt.bt_3, "3");
  46.             onClickListener(bt.bt_4, "4");
  47.             onClickListener(bt.bt_5, "5");
  48.             onClickListener(bt.bt_6, "6");
  49.             onClickListener(bt.bt_7, "7");
  50.             onClickListener(bt.bt_8, "8");
  51.             onClickListener(bt.bt_9, "9");
  52.             onClickListener(bt.bt_dot, ".");
  53.             onClickListener(bt.clean);
  54.             onClickListener(bt.bt_add, '+');
  55.             onClickListener(bt.bt_sub, '-');
  56.             onClickListener(bt.bt_multiply, '*');
  57.             onClickListener(bt.bt_div, '/');
  58.             
  59.             bt.bt_back.setOnClickListener(new OnClickListener() {

  60.                         @Override
  61.                         public void onClick(View arg0) {
  62.                                 // TODO Auto-generated method stub
  63.                                 if(!(str_display.length() == 0)) {
  64.                                         str_display.deleteCharAt(str_display.length() - 1);
  65.                                         bt.et_play.setText(str_display);
  66.                                 }
  67.                         }
  68.                    
  69.             });
  70.             bt.bt_equal.setOnClickListener(new OnClickListener() {

  71.                         @Override
  72.                         public void onClick(View v) {
  73.                                 // TODO Auto-generated method stub
  74.                                 if(!(str_display.length() == 0)) {
  75.                                
  76.                                         num1 = Double.valueOf(str_display.toString());
  77.                                         System.out.println(sign);
  78.                                         switch(sign) {
  79.                                                 case '+':
  80.                                                         bt.et_play.setText(String.valueOf((num1 + num2))); break;
  81.                                                 case '-':
  82.                                                         bt.et_play.setText(String.valueOf((num2 - num1))); break;
  83.                                                 case '*':
  84.                                                         bt.et_play.setText(String.valueOf((num2 * num1))); break;
  85.                                                 case '/':
  86.                                                         bt.et_play.setText(String.valueOf((num2 / num1))); break;
  87.                                                        
  88.                                         }
  89.                                        
  90.                                         str_display = new StringBuffer();
  91.                                         judge_point = 0;
  92.                             }
  93.                         }
  94.                    
  95.             });
  96.         }
  97.    
  98.         @Override
  99.         public boolean onCreateOptionsMenu(Menu menu) {
  100.                 // Inflate the menu; this adds items to the action bar if it is present.
  101.                 getMenuInflater().inflate(R.menu.main, menu);
  102.                 return true;
  103.         }
  104.         public static void onClickListener(Button button, final String num) {
  105.                         button.setOnClickListener(new OnClickListener() {

  106.                                 @Override
  107.                                 public void onClick(View v) {
  108.                                         // TODO Auto-generated method stub
  109.                                         if(str_display.indexOf(".") == 0) {
  110.                                                 str_display.insert(0, "0");
  111.                                                 bt.et_play.setText(str_display.toString());
  112.                                         }
  113.                                    if(num .equals(".") && judge_point == 0) {
  114.                                                 judge_point++;
  115.                                                 str_display.append(num);
  116.                                                 bt.et_play.setText(str_display.toString());
  117.                                         }
  118.                                         else if(num.equals(".")) {
  119.                                                         return;
  120.                                     }
  121.                                         else {
  122.                                                 str_display.append(num);
  123.                                                 bt.et_play.setText(str_display.toString());
  124.                                         }
  125.                                 }
  126.                            
  127.                     });
  128.         }
  129.         public static void onClickListener(Button button) {
  130.                 button.setOnClickListener(new OnClickListener() {

  131.                         @Override
  132.                         public void onClick(View arg0) {
  133.                                 // TODO Auto-generated method stub
  134.                                 str_display = new StringBuffer();
  135.                                 judge_point = 0;
  136.                                 bt.et_play.setText("0.0");
  137.                                 num1 = 0.0;
  138.                                 num2 = 0.0;
  139.                         }
  140.                        
  141.                 });
  142.         }
  143.         public static void onClickListener(Button button, final char symbol) {
  144.                 button.setOnClickListener(new OnClickListener() {

  145.                         @Override
  146.                         public void onClick(View v) {
  147.                                 // TODO Auto-generated method stub
  148.                                 if(!(str_display.length() == 0))
  149.                                 {
  150.                                         num1 = Double.valueOf(str_display.toString());
  151.                                         num2 = num1;
  152.                                         str_display = new StringBuffer();
  153.                                         judge_point = 0;
  154.                                         bt.et_play.setText(String.valueOf(symbol));
  155.                                         //num2 =  Double.valueOf(str_display.toString());
  156.                                 }
  157.                                 sign = symbol;
  158.                         }
  159.                        
  160.                 });
  161. }
  162. }
复制代码
OK  睡觉了···早上自己用手机 测设·····里面只是简单的两个数+ - / *  。

QQ图片20130822041336.jpg (37.11 KB, 下载次数: 3)

QQ图片20130822041336.jpg

评分

参与人数 1技术分 +2 收起 理由
EYE_SEE_YOU + 2 2+2

查看全部评分

4 个回复

倒序浏览
长度不符合?

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >

  6. <TableLayout
  7. android:id="@+id/tableLayout1"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:collapseColumns="4" >

  11. <TableRow
  12. android:id="@+id/tableRow_et"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent" >

  15. <EditText
  16. android:id="@+id/et"
  17. android:layout_width="fill_parent"
  18. android:layout_height="fill_parent"
  19. android:layout_span="4"
  20. android:focusable="false"
  21. android:singleLine="true"
  22. android:inputType="text"
  23. android:gravity="right">
  24. </EditText>

  25. </TableRow>

  26. <TableRow
  27. android:id="@+id/tableRow1"
  28. android:layout_width="fill_parent"
  29. android:layout_height="fill_parent" >

  30. <Button
  31. android:id="@+id/bt_7"
  32. android:layout_width="40px"
  33. android:layout_height="40px"
  34. android:text="7" />

  35. <Button
  36. android:id="@+id/bt_8"
  37. android:layout_width="40px"
  38. android:layout_height="40px"
  39. android:text="8" />

  40. <Button
  41. android:id="@+id/bt_9"
  42. android:layout_width="40px"
  43. android:layout_height="40px"
  44. android:text="9" />

  45. <Button
  46. android:id="@+id/bt_back"
  47. android:layout_width="40px"
  48. android:layout_height="40px"
  49. android:text="back" />
  50. </TableRow>

  51. <TableRow
  52. android:id="@+id/tableRow2"
  53. android:layout_width="fill_parent"
  54. android:layout_height="fill_parent" >

  55. <Button
  56. android:id="@+id/bt_4"
  57. android:layout_width="40px"
  58. android:layout_height="40px"
  59. android:text="4" />

  60. <Button
  61. android:id="@+id/bt_5"
  62. android:layout_width="40px"
  63. android:layout_height="40px"
  64. android:text="5" />

  65. <Button
  66. android:id="@+id/bt_6"
  67. android:layout_width="40px"
  68. android:layout_height="40px"
  69. android:text="6" />

  70. <Button
  71. android:id="@+id/bt_divide"
  72. android:layout_width="40px"
  73. android:layout_height="40px"
  74. android:text="/" />
  75. </TableRow>

  76. <TableRow
  77. android:id="@+id/tableRow3"
  78. android:layout_width="fill_parent"
  79. android:layout_height="fill_parent" >

  80. <Button
  81. android:id="@+id/bt_1"
  82. android:layout_width="40px"
  83. android:layout_height="40px"
  84. android:text="1" />

  85. <Button
  86. android:id="@+id/bt_2"
  87. android:layout_width="40px"
  88. android:layout_height="40px"
  89. android:text="2" />

  90. <Button
  91. android:id="@+id/bt_3"
  92. android:layout_width="40px"
  93. android:layout_height="40px"
  94. android:text="3" />

  95. <Button
  96. android:id="@+id/bt_multiply"
  97. android:layout_width="40px"
  98. android:layout_height="40px"
  99. android:text="*" />
  100. </TableRow>

  101. <TableRow
  102. android:id="@+id/tableRow4"
  103. android:layout_width="fill_parent"
  104. android:layout_height="fill_parent" >

  105. <Button
  106. android:id="@+id/bt_0"
  107. android:layout_width="50px"
  108. android:layout_height="40px"
  109. android:text="0" />

  110. <Button
  111. android:id="@+id/bt_point"
  112. android:layout_width="50px"
  113. android:layout_height="40px"
  114. android:text="." />

  115. <Button
  116. android:id="@+id/bt_add"
  117. android:layout_width="50px"
  118. android:layout_height="40px"
  119. android:text="+" />

  120. <Button
  121. android:id="@+id/bt_sub"
  122. android:layout_width="50px"
  123. android:layout_height="40px"
  124. android:text="-" />
  125. </TableRow>

  126. <TableRow
  127. android:id="@+id/tableRow4"
  128. android:layout_width="fill_parent"
  129. android:layout_height="fill_parent" >

  130. <Button
  131. android:id="@+id/bt_equal"
  132. android:layout_width="fill_parent"
  133. android:layout_height="fill_parent"
  134. android:layout_span="3"
  135. android:text="=" />
  136. <Button
  137. android:id="@+id/bt_clear"
  138. android:layout_width="fill_parent"
  139. android:layout_height="fill_parent"
  140. android:text="clear" />
  141. </TableRow>

  142. </TableLayout>
  143. </LinearLayout>
复制代码

评分

参与人数 1技术分 +2 收起 理由
张智文 + 2 很给力!

查看全部评分

回复 使用道具 举报
我用JavaScript里的eval写过一个简单的计算器,感觉以前的if else switch的写法弱爆了
回复 使用道具 举报
yxz 发表于 2013-8-22 11:54
我用JavaScript里的eval写过一个简单的计算器,感觉以前的if else switch的写法弱爆了 ...

这个逻辑可不是一蹴而就的
  1. public static void onClickListener(Button button, final String num) {
  2.                         button.setOnClickListener(new OnClickListener() {

  3.                                 @Override
  4.                                 public void onClick(View v) {
  5.                                         // TODO Auto-generated method stub
  6.                                         if(str_display.indexOf(".") == 0) {
  7.                                                 str_display.insert(0, "0");
  8.                                                 bt.et_play.setText(str_display.toString());
  9.                                         }
  10.                                    if(num .equals(".") && judge_point == 0) {
  11.                                                 judge_point++;
  12.                                                 str_display.append(num);
  13.                                                 bt.et_play.setText(str_display.toString());
  14.                                         }
  15.                                         else if(num.equals(".")) {
  16.                                                         return;
  17.                                     }
  18.                                         else {
  19.                                                 str_display.append(num);
  20.                                                 bt.et_play.setText(str_display.toString());
  21.                                         }
  22.                                 }
  23.                            
  24.                     });
  25.         }
复制代码
回复 使用道具 举报
好强大         
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马