当前位置:   article > 正文

Android studio实现简单的计算器_计算器的mainactivity.java代码

计算器的mainactivity.java代码

目录

计算器1:

MainAcivity.java

activity_main.xml

UI界面 

计算器2:

MainActivity.java:

active_main.xml:

UI界面:


 

计算器1:

MainAcivity.java

  1. package com.example.lijiang.mycalculator;
  2. import android.os.Bundle;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import java.lang.reflect.Method;
  8. import java.math.BigDecimal;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class MainActivity extends AppCompatActivity{
  12. private StringBuilder show_equation=new StringBuilder();//显示运算式
  13. private ArrayList calculate_equation;//计算式
  14. private int signal=0;//为0 时表示刚输入状态;为1 时表示当前在输出结果上继续输入
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. //初始化
  20. show_equation=new StringBuilder();
  21. calculate_equation=new ArrayList<>();
  22. Button zero=(Button)findViewById(R.id.zero);
  23. Button one=(Button)findViewById(R.id.one);
  24. Button two=(Button)findViewById(R.id.two);
  25. Button three=(Button)findViewById(R.id.three);
  26. Button four=(Button)findViewById(R.id.four);
  27. Button five=(Button)findViewById(R.id.five);
  28. Button six=(Button)findViewById(R.id.six);
  29. Button seven=(Button)findViewById(R.id.seven);
  30. Button eight=(Button)findViewById(R.id.eight);
  31. Button nine=(Button)findViewById(R.id.nine);
  32. Button cls=(Button)findViewById(R.id.cls);
  33. Button div=(Button)findViewById(R.id.div);
  34. Button mul=(Button)findViewById(R.id.mul);
  35. Button backspace=(Button)findViewById(R.id.Backspace);
  36. Button sub=(Button)findViewById(R.id.sub);
  37. Button add=(Button)findViewById(R.id.add);
  38. final Button equal=(Button)findViewById(R.id.equal);
  39. final Button point=(Button)findViewById(R.id.spot);
  40. final EditText result=(EditText)findViewById(R.id.result);
  41. result.setCursorVisible(true);
  42. disableShowInput(result);
  43. //点击文本框时光标始终在文本末尾
  44. result.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. result.setSelection(result.getText().length());
  48. }
  49. });
  50. zero.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View v){
  53. if(!(show_equation.toString().equals("0"))){
  54. if(signal==0){
  55. show_equation.append("0");
  56. result.setText(show_equation);
  57. result.setSelection(result.getText().length());
  58. }else{
  59. show_equation.delete(0,show_equation.length());
  60. show_equation.append("0");
  61. result.setText(show_equation);
  62. result.setSelection(result.getText().length());
  63. signal=0;
  64. }
  65. }
  66. }
  67. });
  68. one.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. if(signal==0){
  72. show_equation.append("1");
  73. result.setText(show_equation);
  74. result.setSelection(result.getText().length());
  75. }else{
  76. show_equation.delete(0,show_equation.length());
  77. show_equation.append("1");
  78. result.setText(show_equation);
  79. result.setSelection(result.getText().length());
  80. signal=0;
  81. }
  82. }
  83. });
  84. two.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87. if(signal==0){
  88. show_equation.append("2");
  89. result.setText(show_equation);
  90. result.setSelection(result.getText().length());
  91. }else{
  92. show_equation.delete(0,show_equation.length());
  93. show_equation.append("2");
  94. result.setText(show_equation);
  95. result.setSelection(result.getText().length());
  96. signal=0;
  97. }
  98. }
  99. });
  100. three.setOnClickListener(new View.OnClickListener() {
  101. @Override
  102. public void onClick(View v) {
  103. if(signal==0){
  104. show_equation.append("3");
  105. result.setText(show_equation);
  106. result.setSelection(result.getText().length());
  107. }else{
  108. show_equation.delete(0,show_equation.length());
  109. show_equation.append("3");
  110. result.setText(show_equation);
  111. result.setSelection(result.getText().length());
  112. signal=0;
  113. }
  114. }
  115. });
  116. four.setOnClickListener(new View.OnClickListener() {
  117. @Override
  118. public void onClick(View v) {
  119. if(signal==0){
  120. show_equation.append("4");
  121. result.setText(show_equation);
  122. result.setSelection(result.getText().length());
  123. }else{
  124. show_equation.delete(0,show_equation.length());
  125. show_equation.append("4");
  126. result.setText(show_equation);
  127. result.setSelection(result.getText().length());
  128. signal=0;
  129. }
  130. }
  131. });
  132. five.setOnClickListener(new View.OnClickListener() {
  133. @Override
  134. public void onClick(View v) {
  135. if(signal==0){
  136. show_equation.append("5");
  137. result.setText(show_equation);
  138. result.setSelection(result.getText().length());
  139. }else{
  140. show_equation.delete(0,show_equation.length());
  141. show_equation.append("5");
  142. result.setText(show_equation);
  143. result.setSelection(result.getText().length());
  144. signal=0;
  145. }
  146. }
  147. });
  148. six.setOnClickListener(new View.OnClickListener() {
  149. @Override
  150. public void onClick(View v) {
  151. if(signal==0){
  152. show_equation.append("6");
  153. result.setText(show_equation);
  154. result.setSelection(result.getText().length());
  155. }else{
  156. show_equation.delete(0,show_equation.length());
  157. show_equation.append("6");
  158. result.setText(show_equation);
  159. result.setSelection(result.getText().length());
  160. signal=0;
  161. }
  162. }
  163. });
  164. seven.setOnClickListener(new View.OnClickListener() {
  165. @Override
  166. public void onClick(View v) {
  167. if(signal==0){
  168. show_equation.append("7");
  169. result.setText(show_equation);
  170. result.setSelection(result.getText().length());
  171. }else{
  172. show_equation.delete(0,show_equation.length());
  173. show_equation.append("7");
  174. result.setText(show_equation);
  175. result.setSelection(result.getText().length());
  176. signal=0;
  177. }
  178. }
  179. });
  180. eight.setOnClickListener(new View.OnClickListener() {
  181. @Override
  182. public void onClick(View v) {
  183. if(signal==0){
  184. show_equation.append("8");
  185. result.setText(show_equation);
  186. result.setSelection(result.getText().length());
  187. }else{
  188. show_equation.delete(0,show_equation.length());
  189. show_equation.append("8");
  190. result.setText(show_equation);
  191. result.setSelection(result.getText().length());
  192. signal=0;
  193. }
  194. }
  195. });
  196. nine.setOnClickListener(new View.OnClickListener() {
  197. @Override
  198. public void onClick(View v) {
  199. if(signal==0){
  200. show_equation.append("9");
  201. result.setText(show_equation);
  202. result.setSelection(result.getText().length());
  203. }else{
  204. show_equation.delete(0,show_equation.length());
  205. show_equation.append("9");
  206. result.setText(show_equation);
  207. result.setSelection(result.getText().length());
  208. signal=0;
  209. }
  210. }
  211. });
  212. cls.setOnClickListener(new View.OnClickListener() {
  213. @Override
  214. public void onClick(View v) {
  215. show_equation.delete(0,show_equation.length());
  216. calculate_equation.clear();
  217. signal=0;
  218. result.setText("");
  219. }
  220. });
  221. backspace.setOnClickListener(new View.OnClickListener() {
  222. @Override
  223. public void onClick(View v) {
  224. if(!(show_equation.toString().equals(""))) {
  225. if(signal==0){
  226. show_equation.deleteCharAt(show_equation.length() - 1);
  227. result.setText(show_equation);
  228. result.setSelection(result.getText().length());
  229. }else{
  230. show_equation.delete(0,show_equation.length());
  231. result.setText("");
  232. signal=0;
  233. }
  234. }
  235. }
  236. });
  237. point.setOnClickListener(new View.OnClickListener() {
  238. @Override
  239. public void onClick(View v) {
  240. if(signal==0){
  241. String a=show_equation.toString();
  242. if(a.equals("")){
  243. show_equation.append(".");
  244. result.setText(show_equation);
  245. result.setSelection(result.getText().length());
  246. }else{
  247. int i;
  248. char t='0';
  249. for(i=a.length();i>0;i--){
  250. t=a.charAt(i-1);
  251. if(t=='.'||t=='+'||t=='-'||t=='*'||t=='/')
  252. break;
  253. }
  254. if(i==0){
  255. show_equation.append(".");
  256. result.setText(show_equation);
  257. result.setSelection(result.getText().length());
  258. }else if(t=='+'||t=='-'||t=='*'||t=='/'){
  259. show_equation.append(".");
  260. result.setText(show_equation);
  261. result.setSelection(result.getText().length());
  262. }
  263. }
  264. }else{
  265. show_equation.delete(0,show_equation.length());
  266. show_equation.append(".");
  267. result.setText(".");
  268. result.setSelection(result.getText().length());
  269. signal=0;
  270. }
  271. }
  272. });
  273. equal.setOnClickListener(new View.OnClickListener() {
  274. @Override
  275. public void onClick(View v) {
  276. //判断用户是否输入了内容
  277. if(!show_equation.toString().equals("")){
  278. signal=1;
  279. char temp=show_equation.charAt(show_equation.length()-1);
  280. if(show_equation.charAt(0)=='-')
  281. show_equation.insert(0,"0");
  282. if(temp=='+'||temp=='-')
  283. show_equation.append("0");
  284. if(temp=='*'||temp=='/')
  285. show_equation.append("1");
  286. StringBuilder temp1=new StringBuilder();
  287. for(int i=0;i<show_equation.length();i++){
  288. if(show_equation.charAt(i)>='0'&&show_equation.charAt(i)<='9'||show_equation.charAt(i)=='.'){
  289. temp1.append(String.valueOf(show_equation.charAt(i)));
  290. }else if(show_equation.charAt(i)=='N'){
  291. calculate_equation.add("NaN");
  292. //跳过2个字符
  293. i=i+2;
  294. }else if(show_equation.charAt(i)=='∞'){
  295. calculate_equation.add("∞");
  296. }
  297. else
  298. {
  299. if(temp1.length()!=0){
  300. calculate_equation.add(temp1.toString());
  301. temp1.delete(0,temp1.length());
  302. }
  303. calculate_equation.add(String.valueOf(show_equation.charAt(i)));
  304. }
  305. }
  306. if(temp1.length()!=0){
  307. calculate_equation.add(temp1.toString());
  308. }
  309. calculate_equation.add("#");
  310. String temp8=calculate(calculate_equation);
  311. result.setText(temp8);
  312. result.setSelection(result.getText().length());
  313. show_equation.delete(0,show_equation.length());
  314. calculate_equation.clear();
  315. show_equation.append(temp8);
  316. }
  317. }
  318. });
  319. add.setOnClickListener(new View.OnClickListener() {
  320. @Override
  321. public void onClick(View v) {
  322. //判断用户是否输入了内容
  323. if(!(show_equation.toString().equals(""))) {
  324. signal=0;
  325. char temp=show_equation.charAt(show_equation.length()-1);
  326. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  327. {
  328. show_equation.deleteCharAt(show_equation.length()-1);
  329. show_equation.append("+");
  330. }
  331. else
  332. show_equation.append("+");
  333. result.setText(show_equation);
  334. result.setSelection(result.getText().length());
  335. }
  336. }
  337. });
  338. sub.setOnClickListener(new View.OnClickListener() {
  339. @Override
  340. public void onClick(View v) {
  341. //判断用户是否输入了内容
  342. if(!(show_equation.toString().equals(""))) {
  343. signal=0;
  344. char temp=show_equation.charAt(show_equation.length()-1);
  345. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  346. {
  347. show_equation.deleteCharAt(show_equation.length()-1);
  348. show_equation.append("-");
  349. }
  350. else
  351. show_equation.append("-");
  352. result.setText(show_equation);
  353. result.setSelection(result.getText().length());
  354. }
  355. }
  356. });
  357. mul.setOnClickListener(new View.OnClickListener() {
  358. @Override
  359. public void onClick(View v) {
  360. //判断用户是否输入了内容
  361. if(!(show_equation.toString().equals(""))) {
  362. signal=0;
  363. char temp=show_equation.charAt(show_equation.length()-1);
  364. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  365. {
  366. show_equation.deleteCharAt(show_equation.length()-1);
  367. show_equation.append("*");
  368. }
  369. else
  370. show_equation.append("*");
  371. result.setText(show_equation);
  372. result.setSelection(result.getText().length());
  373. }
  374. }
  375. });
  376. div.setOnClickListener(new View.OnClickListener() {
  377. @Override
  378. public void onClick(View v) {
  379. //判断用户是否输入了内容
  380. if(!(show_equation.toString().equals(""))) {
  381. signal=0;
  382. char temp=show_equation.charAt(show_equation.length()-1);
  383. if(temp=='+'||temp=='-'||temp=='*'||temp=='/')
  384. {
  385. show_equation.deleteCharAt(show_equation.length()-1);
  386. show_equation.append("/");
  387. }
  388. else
  389. show_equation.append("/");
  390. result.setText(show_equation);
  391. result.setSelection(result.getText().length());
  392. }
  393. }
  394. });
  395. }
  396. protected boolean operatorPriorityCompare(char operator1,char operator2)
  397. {
  398. int o1=0;
  399. int o2=0;
  400. switch (operator1){
  401. case '+':{o1=0;break;}
  402. case '-':{o1=0;break;}
  403. case '*':{o1=1;break;}
  404. case '/':{o1=1;break;}
  405. }
  406. switch (operator2){
  407. case '+':{o2=0;break;}
  408. case '-':{o2=0;break;}
  409. case '*':{o2=1;break;}
  410. case '/':{o2=1;break;}
  411. }
  412. if(o1<=o2)
  413. {
  414. return false;
  415. }
  416. else
  417. return true;
  418. }
  419. //相加
  420. public static Double Add(Double d1,Double d2) {
  421. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  422. return d1+d2;
  423. }
  424. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  425. return d1+d2;
  426. }
  427. BigDecimal b1 = new BigDecimal(Double.toString(d1));
  428. BigDecimal b2 = new BigDecimal(Double.toString(d2));
  429. return b1.add(b2).doubleValue();
  430. }
  431. //相减
  432. public static Double Sub(Double d1,Double d2){
  433. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  434. return d1-d2;
  435. }
  436. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  437. return d1-d2;
  438. }
  439. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  440. return d1*d2;
  441. }
  442. BigDecimal b1=new BigDecimal(Double.toString(d1));
  443. BigDecimal b2=new BigDecimal(Double.toString(d2));
  444. return b1.subtract(b2).doubleValue();
  445. }
  446. //相乘
  447. public static Double Mul(Double d1,Double d2){
  448. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  449. return d1*d2;
  450. }
  451. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  452. return d1*d2;
  453. }
  454. BigDecimal b1=new BigDecimal(Double.toString(d1));
  455. BigDecimal b2=new BigDecimal(Double.toString(d2));
  456. return b1.multiply(b2).setScale(8).doubleValue();
  457. }
  458. //相除
  459. public static Double Div(Double d1,Double d2){
  460. if(d1==Double.NEGATIVE_INFINITY||d1==Double.POSITIVE_INFINITY||d2==Double.NEGATIVE_INFINITY||d2==Double.POSITIVE_INFINITY){
  461. return d1/d2;
  462. }
  463. if(String.valueOf(d1).equals("NaN")||String.valueOf(d1).equals("NaN")){
  464. return d1/d2;
  465. }
  466. if(d1==0.0&&d2==0.0){
  467. return Double.NaN;
  468. }
  469. if(d2==0.0){
  470. return d1/d2;
  471. }
  472. BigDecimal b1=new BigDecimal(Double.toString(d1));
  473. BigDecimal b2=new BigDecimal(Double.toString(d2));
  474. return b1.divide(b2,8,BigDecimal.ROUND_HALF_UP).doubleValue();
  475. }
  476. protected String calculate(ArrayList equation){
  477. Double temp2;
  478. Double temp3;
  479. Double result;
  480. List operator=new ArrayList();
  481. List<Double> operand=new ArrayList();
  482. for(int i=0;i<equation.size();i++)
  483. {
  484. String temp4=(String) equation.get(i);
  485. if(temp4.equals("+")||temp4.equals("-")||temp4.equals("*")||temp4.equals("/"))
  486. {
  487. if(operator.size()>0)
  488. {
  489. String temp5=operator.get(operator.size()-1).toString();
  490. while(!(operatorPriorityCompare(temp4.charAt(0),temp5.charAt(0)))&&operator.size()>0)
  491. {
  492. operator.remove(operator.size()-1);
  493. temp3=operand.get(operand.size()-1);
  494. operand.remove(operand.size()-1);
  495. temp2=operand.get(operand.size()-1);
  496. operand.remove(operand.size()-1);
  497. switch (temp5.charAt(0)){
  498. case '+':{result=Add(temp2,temp3);operand.add(result);break;}
  499. case '-':{result=Sub(temp2,temp3);operand.add(result);break;}
  500. case '*':{result=Mul(temp2,temp3);operand.add(result);break;}
  501. case '/':{result=Div(temp2,temp3);operand.add(result);break;}
  502. }
  503. if(operator.size()>0)
  504. {
  505. temp5=operator.get(operator.size()-1).toString();
  506. }
  507. else
  508. break;
  509. }
  510. operator.add(temp4);
  511. }
  512. else
  513. operator.add(temp4);
  514. }
  515. else if(temp4.equals("#"))
  516. {
  517. while(operator.size()>0)
  518. {
  519. String temp6=(String)operator.get(operator.size()-1);
  520. operator.remove(operator.size()-1);
  521. temp3=operand.get(operand.size()-1);
  522. operand.remove(operand.size()-1);
  523. temp2=operand.get(operand.size()-1);
  524. operand.remove(operand.size()-1);
  525. switch (temp6.charAt(0)){
  526. case '+':{result=Add(temp2,temp3);operand.add(result);break;}
  527. case '-':{result=Sub(temp2,temp3);operand.add(result);break;}
  528. case '*':{result=Mul(temp2,temp3);operand.add(result);break;}
  529. case '/':{result=Div(temp2,temp3);operand.add(result);break;}
  530. }
  531. }
  532. }
  533. else
  534. {
  535. if(temp4.equals("NaN")){
  536. operand.add(Double.NaN);
  537. }else if(temp4.equals("∞")){
  538. operand.add(Double.POSITIVE_INFINITY);
  539. }else{
  540. operand.add(Double.parseDouble(temp4));
  541. }
  542. }
  543. }
  544. if(operand.get(0)==Double.NEGATIVE_INFINITY) return "-∞";
  545. if(operand.get(0)==Double.POSITIVE_INFINITY) return "∞";
  546. return operand.get(0).toString();
  547. }
  548. //当API最低版小于21时使用这个函数实现点击文本框不弹出键盘
  549. public void disableShowInput(EditText et) {
  550. Class<EditText> cls = EditText.class;
  551. Method method;
  552. try {
  553. method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
  554. method.setAccessible(true);
  555. method.invoke(et, false);
  556. } catch (Exception e) {
  557. e.printStackTrace();
  558. }
  559. }
  560. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <EditText
  7. android:id="@+id/result"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:textSize="40sp"
  11. android:enabled="false"/>
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="0dp"
  15. android:layout_weight="1"
  16. android:orientation="horizontal">
  17. <Button
  18. android:id="@+id/cls"
  19. android:layout_width="0dp"
  20. android:layout_height="match_parent"
  21. android:layout_weight="1"
  22. android:textSize="20sp"
  23. android:text="C"
  24. android:textColor="#ffffff"/>
  25. <Button
  26. android:id="@+id/div"
  27. android:layout_width="0dp"
  28. android:layout_height="match_parent"
  29. android:layout_weight="1"
  30. android:textSize="20sp"
  31. android:text="/"
  32. android:textColor="#ffffff"/>
  33. <Button
  34. android:id="@+id/mul"
  35. android:layout_width="0dp"
  36. android:layout_height="match_parent"
  37. android:layout_weight="1"
  38. android:textSize="20sp"
  39. android:text="*"
  40. android:textColor="#ffffff"/>
  41. <Button
  42. android:id="@+id/Backspace"
  43. android:layout_width="0dp"
  44. android:layout_height="match_parent"
  45. android:layout_weight="1"
  46. android:textSize="20sp"
  47. android:text="Backspace"
  48. android:textAllCaps="false"
  49. android:textColor="#ffffff"/>
  50. </LinearLayout>
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="0dp"
  54. android:layout_weight="1"
  55. android:orientation="horizontal">
  56. <Button
  57. android:id="@+id/seven"
  58. android:layout_width="0dp"
  59. android:layout_height="match_parent"
  60. android:layout_weight="1"
  61. android:textSize="20sp"
  62. android:text="7"
  63. android:textColor="#ffffff"/>
  64. <Button
  65. android:id="@+id/eight"
  66. android:layout_width="0dp"
  67. android:layout_height="match_parent"
  68. android:layout_weight="1"
  69. android:textSize="20sp"
  70. android:text="8"
  71. android:textColor="#ffffff"/>
  72. <Button
  73. android:id="@+id/nine"
  74. android:layout_width="0dp"
  75. android:layout_height="match_parent"
  76. android:layout_weight="1"
  77. android:textSize="20sp"
  78. android:text="9"
  79. android:textColor="#ffffff"/>
  80. <Button
  81. android:id="@+id/sub"
  82. android:layout_width="0dp"
  83. android:layout_height="match_parent"
  84. android:layout_weight="1"
  85. android:textSize="20sp"
  86. android:text="-"
  87. android:textColor="#ffffff"/>
  88. </LinearLayout>
  89. <LinearLayout
  90. android:layout_width="match_parent"
  91. android:layout_height="0dp"
  92. android:layout_weight="1"
  93. android:orientation="horizontal">
  94. <Button
  95. android:id="@+id/four"
  96. android:layout_width="0dp"
  97. android:layout_height="match_parent"
  98. android:layout_weight="1"
  99. android:textSize="20sp"
  100. android:text="4"
  101. android:textColor="#ffffff"/>
  102. <Button
  103. android:id="@+id/five"
  104. android:layout_width="0dp"
  105. android:layout_height="match_parent"
  106. android:layout_weight="1"
  107. android:textSize="20sp"
  108. android:text="5"
  109. android:textColor="#ffffff"/>
  110. <Button
  111. android:id="@+id/six"
  112. android:layout_width="0dp"
  113. android:layout_height="match_parent"
  114. android:layout_weight="1"
  115. android:textSize="20sp"
  116. android:text="6"
  117. android:textColor="#ffffff"/>
  118. <Button
  119. android:id="@+id/add"
  120. android:layout_width="0dp"
  121. android:layout_height="match_parent"
  122. android:layout_weight="1"
  123. android:textSize="20sp"
  124. android:text="+"
  125. android:textColor="#ffffff"/>
  126. </LinearLayout>
  127. <LinearLayout
  128. android:layout_width="match_parent"
  129. android:layout_height="0dp"
  130. android:layout_weight="2"
  131. android:orientation="horizontal">
  132. <LinearLayout
  133. android:layout_width="0dp"
  134. android:layout_height="match_parent"
  135. android:layout_weight="1"
  136. android:orientation="vertical">
  137. <LinearLayout
  138. android:layout_width="match_parent"
  139. android:layout_height="1dp"
  140. android:layout_weight="1"
  141. android:orientation="horizontal">
  142. <Button
  143. android:id="@+id/one"
  144. android:layout_width="0dp"
  145. android:layout_height="match_parent"
  146. android:layout_weight="1"
  147. android:textSize="20sp"
  148. android:text="1"
  149. android:textColor="#ffffff"/>
  150. <Button
  151. android:id="@+id/two"
  152. android:layout_width="0dp"
  153. android:layout_height="match_parent"
  154. android:layout_weight="1"
  155. android:textSize="20sp"
  156. android:text="2"
  157. android:textColor="#ffffff"/>
  158. </LinearLayout>
  159. <LinearLayout
  160. android:layout_width="match_parent"
  161. android:layout_height="1dp"
  162. android:layout_weight="1">
  163. <Button
  164. android:id="@+id/zero"
  165. android:layout_width="match_parent"
  166. android:layout_height="match_parent"
  167. android:text="0"
  168. android:textSize="20sp"
  169. android:textColor="#ffffff"/>
  170. </LinearLayout>
  171. </LinearLayout>
  172. <LinearLayout
  173. android:layout_width="0dp"
  174. android:layout_height="match_parent"
  175. android:layout_weight="1"
  176. android:orientation="horizontal">
  177. <LinearLayout
  178. android:layout_width="0dp"
  179. android:layout_height="match_parent"
  180. android:layout_weight="1"
  181. android:orientation="vertical">
  182. <Button
  183. android:id="@+id/three"
  184. android:layout_width="match_parent"
  185. android:layout_height="1dp"
  186. android:layout_weight="1"
  187. android:text="3"
  188. android:textSize="20sp"
  189. android:textColor="#ffffff"/>
  190. <Button
  191. android:id="@+id/spot"
  192. android:layout_width="match_parent"
  193. android:layout_height="1dp"
  194. android:layout_weight="1"
  195. android:text="."
  196. android:textSize="20sp"
  197. android:textColor="#ffffff"/>
  198. </LinearLayout>
  199. <LinearLayout
  200. android:layout_width="0dp"
  201. android:layout_height="match_parent"
  202. android:layout_weight="1">
  203. <Button
  204. android:id="@+id/equal"
  205. android:layout_width="match_parent"
  206. android:layout_height="match_parent"
  207. android:text="="
  208. android:textSize="20sp"
  209. android:textColor="#ffffff"/>
  210. </LinearLayout>
  211. </LinearLayout>
  212. </LinearLayout>
  213. </LinearLayout>

UI界面 

计算器2:

MainActivity.java:

  1. package com.example.yuanmei.yuanmeicalculatorproject;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.TextView;
  7. public class MainActivity extends Activity {
  8. private TextView textView;
  9. private String str,num1,num2;
  10. private double result;
  11. private boolean addNum;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. textView = (TextView) findViewById(R.id.textView);
  17. addNum = true;
  18. }
  19. public void onClick(View v){
  20. str = (String) textView.getText();
  21. switch (v.getId()) {
  22. case R.id.btn_cle:
  23. textView.setText("");
  24. break;
  25. case R.id.btn_del:
  26. if(!str.equals("") && str != null){
  27. textView.setText(str.substring(0, str.length()-1));
  28. }
  29. break;
  30. case R.id.btn_equ:
  31. if(str.contains("+")){
  32. getResult(num1, num2, "+");
  33. }
  34. else if(str.contains("-")){
  35. getResult(num1, num2, "-");
  36. }
  37. else if(str.contains("×")){
  38. getResult(num1, num2, "×");
  39. }
  40. else if(str.contains("÷")){
  41. getResult(num1, num2, "÷");
  42. }
  43. else {
  44. return;
  45. }
  46. break;
  47. case R.id.btn_add:
  48. case R.id.btn_sub:
  49. case R.id.btn_mul:
  50. case R.id.btn_div:
  51. if (str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷"))
  52. return;
  53. else
  54. textView.setText(str+((Button)v).getText());
  55. if(!addNum)
  56. addNum = true;
  57. break;
  58. default:
  59. if (addNum) {
  60. textView.setText(str+((Button)v).getText());
  61. }else{
  62. textView.setText(((Button)v).getText());
  63. addNum = true;
  64. }
  65. break;
  66. }
  67. }
  68. private void getResult(String num1,String num2,String op) {
  69. num1 = str.substring(0,str.indexOf(op));
  70. num2 = str.substring(str.indexOf(op)+1);
  71. try {
  72. double n1 = Double.parseDouble(num1);
  73. double n2 = Double.parseDouble(num2);
  74. if (op.equals("+")) {
  75. result = n1+n2;
  76. }else if(op.equals("-")){
  77. result = n1-n2;
  78. }else if(op.equals("×")){
  79. result = n1*n2;
  80. }else if(op.equals("÷")){
  81. result = n1/n2;
  82. }else {
  83. return;
  84. }
  85. String r = result+"";
  86. if(r.contains(".")&&r.substring(r.length()-1).equals("0")){
  87. r = r.substring(0,r.indexOf("."));
  88. }
  89. textView.setText(r);
  90. addNum = false;
  91. } catch (Exception e) {
  92. return;
  93. }
  94. }
  95. }

active_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/LinearLayout1"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity" >
  9. <TextView
  10. android:id="@+id/textView"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:gravity="right"
  14. android:height="40dp"
  15. android:textSize="30sp" />
  16. <TableLayout
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:stretchColumns="*" >
  20. <TableRow
  21. android:id="@+id/tableRow1"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content" >
  24. <Button
  25. android:id="@+id/btn_cle"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:layout_span="2"
  29. android:onClick="onClick"
  30. android:text="CLEAR" />
  31. <Button
  32. android:id="@+id/btn_del"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_span="2"
  36. android:onClick="onClick"
  37. android:text="☜" />
  38. </TableRow>
  39. <TableRow
  40. android:id="@+id/tableRow2"
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content" >
  43. <Button
  44. android:id="@+id/btn_7"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:onClick="onClick"
  48. android:text="7" />
  49. <Button
  50. android:id="@+id/btn_8"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:onClick="onClick"
  54. android:text="8" />
  55. <Button
  56. android:id="@+id/btn_9"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:onClick="onClick"
  60. android:text="9" />
  61. <Button
  62. android:id="@+id/btn_div"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:onClick="onClick"
  66. android:text="÷" />
  67. </TableRow>
  68. <TableRow
  69. android:id="@+id/tableRow3"
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content" >
  72. <Button
  73. android:id="@+id/btn_4"
  74. android:layout_width="wrap_content"
  75. android:layout_height="wrap_content"
  76. android:onClick="onClick"
  77. android:text="4" />
  78. <Button
  79. android:id="@+id/btn_5"
  80. android:layout_width="wrap_content"
  81. android:layout_height="wrap_content"
  82. android:onClick="onClick"
  83. android:text="5" />
  84. <Button
  85. android:id="@+id/btn_6"
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:onClick="onClick"
  89. android:text="6" />
  90. <Button
  91. android:id="@+id/btn_mul"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:onClick="onClick"
  95. android:text="×" />
  96. </TableRow>
  97. <TableRow
  98. android:id="@+id/tableRow4"
  99. android:layout_width="wrap_content"
  100. android:layout_height="wrap_content" >
  101. <Button
  102. android:id="@+id/btn_1"
  103. android:layout_width="wrap_content"
  104. android:layout_height="wrap_content"
  105. android:onClick="onClick"
  106. android:text="1" />
  107. <Button
  108. android:id="@+id/btn_2"
  109. android:layout_width="wrap_content"
  110. android:layout_height="wrap_content"
  111. android:onClick="onClick"
  112. android:text="2" />
  113. <Button
  114. android:id="@+id/btn_3"
  115. android:layout_width="wrap_content"
  116. android:layout_height="wrap_content"
  117. android:onClick="onClick"
  118. android:text="3" />
  119. <Button
  120. android:id="@+id/btn_sub"
  121. android:layout_width="wrap_content"
  122. android:layout_height="wrap_content"
  123. android:onClick="onClick"
  124. android:text="-" />
  125. </TableRow>
  126. <TableRow
  127. android:id="@+id/tableRow5"
  128. android:layout_width="wrap_content"
  129. android:layout_height="wrap_content" >
  130. <Button
  131. android:id="@+id/btn_point"
  132. android:layout_width="wrap_content"
  133. android:layout_height="wrap_content"
  134. android:onClick="onClick"
  135. android:text="." />
  136. <Button
  137. android:id="@+id/btn_0"
  138. android:layout_width="wrap_content"
  139. android:layout_height="wrap_content"
  140. android:onClick="onClick"
  141. android:text="0" />
  142. <Button
  143. android:id="@+id/btn_equ"
  144. android:layout_width="wrap_content"
  145. android:layout_height="wrap_content"
  146. android:onClick="onClick"
  147. android:text="=" />
  148. <Button
  149. android:id="@+id/btn_add"
  150. android:layout_width="wrap_content"
  151. android:layout_height="wrap_content"
  152. android:onClick="onClick"
  153. android:text="+" />
  154. </TableRow>
  155. </TableLayout>
  156. </LinearLayout>

UI界面:

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/538882
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&