Android development application example: simple calculator + multi-touch

Source: Internet
Author: User

In this example, a simple Integer Calculator applies multi-touch operations: One Touch operation is addition operations, two touch operations are subtraction operations, three touch operations are multiplication operations, and four touch operations are Division operations. The option menu is also added. The options include exit, help, and clear.
:


 
Specific Code:
MainActivity. java
1. package com. lingdududu. test;
2.
3. import android. app. Activity;
4. import android. app. AlertDialog;
5. import android. OS. Bundle;
6. import android. view. Menu;
7. import android. view. MenuItem;
8. import android. view. MotionEvent;
9. import android. widget. EditText;
10.
11. import android. widget. Toast;
12 ./**
13 .*
14. * @ author lingdududu
15. * This program is mainly used to solve the four arithmetic operations of simple integer operations. The user inputs divisor and devisor respectively in the first two edittexts,
16. * when using multi-touch, one-touch operations are addition operations, while two-touch operations are subtraction operations,
17. * Three-touch operations are multiplication and four-touch operations are Division operations.
18. * display the result in the third EditText after touch
19 .*/
20. public class MainActivity extends Activity {
21. // define the log label
22. private static final String Tag = "System. out ";
23. // constant of the menu item ID
24. private static final int ITEM1 = Menu. FIRST;
25. private static final int ITEM2 = Menu. FIRST + 1;
26. private static final int ITEM3 = Menu. FIRST + 2;
27. private static EditText firstEtx, secondEtx, resultEtx;
28. // define the number of two user inputs
29. int value1, value2;
30. String str;
31. @ Override
32. public void onCreate (Bundle savedInstanceState ){
33. super. onCreate (savedInstanceState );
34. setContentView (R. layout. main );
35.
36. // obtain the EditText instance through the findViewById Method
37. firstEtx = (EditText) findViewById (R. id. text1 );
38. secondEtx = (EditText) findViewById (R. id. text2 );
39. resultEtx = (EditText) findViewById (R. id. text3 );
40. // The EditText output from the calculation result cannot be edited.
41. resultEtx. setFocusable (false );
42 .}
43.
44. // Option Menu
45. public boolean onCreateOptionsMenu (Menu menu ){
46./* Add menu items
47. * android. R uses the built-in icons.
48 .*/
49. menu. add (0, ITEM1, 0, "exit"). setIcon (android. R. drawable. ic_lock_power_off );
50. menu. add (0, ITEM2, 0, "help"). setIcon (android. R. drawable. ic_menu_help );
51. menu. add (0, ITEM3, 0, "empty"). setIcon (android. R. drawable. ic_menu_close_clear_cancel );
52. return true;
53 .}
54.
55. // override onOptionsItemSelected (MenuItem item) to respond to the menu option clicked event
56. public boolean onOptionsItemSelected (MenuItem item ){
57. switch (item. getItemId ()){
58. // menu item 1 Selected
59. case ITEM1:
60. // return to the main interface
61. finish ();
62. break;
63. // menu item 2 Selected
64. case ITEM2:
65. // The AlertDialog dialog box displays help information.
66. AlertDialog. Builder dialog = new AlertDialog. Builder (this );
67. dialog. setTitle ("operation instructions ")
68. setMessage (
69. "In the first two edittexts, the user enters the divisor and the divisor respectively. One-touch operations are addition operations, two-touch operations are subtraction operations, three-touch operations are multiplication operations, and four-touch operations are Division operations. "
70. + "behind the screen, the calculation result is displayed in the third EditText. When the screen is left, the EditText of the result is cleared. "
71. + "click the Menu to display the option Menu, select clear to clear the data in EditText, select exit to exit the program, and return to the main interface ")
72 .. show ();
73. break;
74. // menu item 3 selected
75. case ITEM3:
76. // clear EditText data
77. firstEtx. setText ("");
78. secondEtx. setText ("");
79. resultEtx. setText ("");
80. break;
81 .}
82. return true;
83 .}
84.
85. public boolean onTouchEvent (MotionEvent event ){
86. // TODO Auto-generated method stub
87. // forced type conversion. Convert String type to int type.
88. value1 = Integer. parseInt (firstEtx. getText (). toString ());
89. value2 = Integer. parseInt (secondEtx. getText (). toString ());
90.
91. // obtain the number of touch points. For example, if 2 is used, the screen may be pressed by two fingers at the same time.
92. int num = event. getPointerCount ();
93. try {
94. if (event. getAction () = MotionEvent. ACTION_UP ){
95. resultEtx. setText ("");
96 .}
97. // process multi-touch
98. else {
99. // hold down the screen with one finger for addition
100. if (num = 1 ){
101. str = String. valueOf (value1 + value2 );
102. // Add the calculated result to the third EditText
103. resultEtx. setText (str );
104 .}
105. else if (num = 2 ){
106. str = String. valueOf (value1-value2 );
107. // Add the calculated result to the third EditText
108. resultEtx. setText (str );
109 .}
110. else if (num = 3 ){
111. str = String. valueOf (value1 * value2 );
112. // Add the calculated result to the third EditText
113. resultEtx. setText (str );
114 .}
115. else if (num = 4 ){
116. str = String. valueOf (value1/value2 );
117. // Add the calculated result to the third EditText
118. resultEtx. setText (str );
119 .}
120. else {
121.
122 .}
123 .}
124.} catch (Exception e ){
125. // call Toast to display the exception information
126. Toast. makeText (MainActivity. this, "check whether your input divisor is correct", Toast. LENGTH_LONG). show ();
127 .}
128. return super. onTouchEvent (event );
129 .}
130 .}
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 = "fill_parent"
5. android: layout_height = "fill_parent"
6.>
7. <TextView
8. android: layout_width = "fill_parent"
9. android: layout_height = "wrap_content"
10. android: text = "Enter the divisor"
11./>
12. <EditText
13. android: id = "@ + id/text1"
14. android: inputType = "number"
15. android: layout_width = "fill_parent"
16. android: layout_height = "wrap_content"
17./>
18. <TextView
19. android: layout_width = "fill_parent"
20. android: layout_height = "wrap_content"
21. android: text = "Enter the divisor"
22./>
23. <EditText
24. android: id = "@ + id/text2"
25. android: inputType = "number"
26. android: layout_width = "fill_parent"
27. android: layout_height = "wrap_content"
28./>
29. <TextView
30. android: layout_width = "fill_parent"
31. android: layout_height = "wrap_content"
32. android: text = "the calculation result is"
33./>
34. <EditText
35. android: id = "@ + id/text3"
36. android: inputType = "number"
37. android: layout_width = "fill_parent"
38. android: layout_height = "wrap_content"
39./>
40. <! -- <Button
41. android: layout_width = "wrap_content"
42. android: layout_height = "wrap_content"
43. android: id = "@ + id/button"
44. android: text = "Division operations"
45./> -->
46. </LinearLayout>
 
This article is from the "IT" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.