当前位置:   article > 正文

安卓开发入门-简易计算器_安卓开发计算器代码

安卓开发计算器代码

        又到周末了,继续接着上个星期的内容,给大伙讲讲怎么做一个简单的demo,顺便也讲一点组件的应用。

        先展示一下最终成功图

        

页面设计

        页面布局

        首先,我们点击我们默认的activity文件(一般初始化是activity_main.xml,我这里为了教学演示所以改成了其他的)。进入code模式。

可以看到内容如下,我们将修改布局为线性布局,使用内置的线性布局模板,LinearLayout,修改后的内容如下

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

简单给大家讲讲其中属性的含义

layout_width:组件的宽度,由于这个组件是整个布局,所以宽度就是手机整个的宽度,下同

background: 背景,背景可以是一个颜色,用#xxxxxx(色值)来定义,或者是提前在资源文件中准备好相应的背景类型(drawable类型).。

orientation: 布局类型,vertical代表纵向,horizontial代表横向,我们这里使用纵向布局就行。

更多属性类型请见LinearLayout组件讲解

为了使整个布局更合理,更便于使用,我们还需要添加一个ScrollView,ScrollView的作用是,当屏幕内容超出手机大小的时候,会自动生成滑动视图。

最后的布局内容如下。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@color/white"
  6. android:orientation="vertical">
  7. <ScrollView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:orientation="vertical">
  14. </LinearLayout>
  15. </ScrollView>
  16. </LinearLayout>

        设置内容

        我们已经得到了一个布局

        现在需要开始填内容了,首先,是整个应用的标题,也就是我们需要一个文本框来显示“简易计算器”这几个字,这时我们可以使用到TextView这个组件,其功能是显示text。这里我给出两种开发模式,供大家参考。

                Code开发

                在Code中编辑属性和内容配置,得到源码如下:

  1. <TextView
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="简易计算器"
  5. android:layout_gravity="center"/>

                Design开发

                使用可视化开发模式,拖动组件进行编辑。

不过一般来说两种开发方式都是混用的,不存在只用某个开发方式的说法~~~

简单介绍一下TextView的内容,其中text,textSize看名字就知道是什么功能,我就不多说了

gravity: 定义组件内部元素的位置,比如center就是居中

更多属性请见TextView文本编辑框

显示框

        我们还需要一个框来显示计算的结果内容,同理也使用textView组件实现,这里我直接给出代码了。

  1. <TextView
  2. android:id="@+id/tv_result"
  3. android:layout_width="match_parent"
  4. android:layout_height="129dp"
  5. android:background="@color/white"
  6. android:gravity="right|bottom"
  7. android:text="0"
  8. android:textColor="@color/black"
  9. android:textSize="25sp" />

   如果想要增加对比度,可以使用background属性修改文本框的背景,这里就不多演示了。

        按钮设置

        接下来我们需要设置计算器的按钮,在这之前,为了使按钮的排布更加合理,我们可以使用网格布局进行操作。

        网格布局需要使用GridLayout这个组件

代码如下:

  1. <GridLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:columnCount="4"
  5. android:rowCount="6">
  6. </GridLayout>

其中columnCount,rowCount分别规定了网格布局的行和列

此时,我们添加一行(4个)Button组件试试

  1. <Button
  2. android:text="Button"/>
  3. <Button
  4. android:text="Button"/>
  5. <Button
  6. android:text="Button"/>
  7. <Button
  8. android:text="Button"/>

可以看到布局如下

虽然一行确实排满了四个按钮,但是其按钮排列却没有平均占据一行。

此时,我们需要用到layout_columnWeight这个属性,定义每个按钮占据的权重,我们这里定义每个按钮都占权重1,那么最后一个按钮会分得一行的1/4作为占位。

android:layout_columnWeight="1"

除此之外,我们还需要使按钮之间存在间隔,这里我们使用layout_margin这个属性,即控制按钮的间隔。

然后得到的布局如下:

        自定义按钮风格

        我们可以看到,虽然我们还什么都没干,设置的按钮却已经有自己的风格了,这是怎么回事呢?答案是按钮继承了项目的默认风格。

        我们可以在项目工程的res/themes/themes.xml里面找到,安全起见,我们最好使用默认风格,因为每个手机安卓系统的内部配置不同,如果你这里设置了系统风格,在你的调试机上没问,很有可能到别人的手机上就bug了。而且系统风格限制很多,包括我们后文需要对Button修改颜色这些,都只能在默认风格上进行。

        详情修改如下

        themes.xml

  1. <resources xmlns:tools="http://schemas.android.com/tools">
  2. <!-- Base application theme. -->
  3. <style name="Base.Theme.MyApplication" parent="Theme.AppCompat">
  4. <!-- Customize your light theme here. -->
  5. <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
  6. </style>
  7. </resources>

        配置文件AndroidManifest.xml(位于工程路径中的manifest文件夹下)中,修改这一行

android:theme="@style/Theme.Caculation"

        OK,回到原页,可以看到按钮变得又丑又搓,不急,我们慢慢来。

        首先,我们给按钮田间background颜色,指定颜色为#00BCD4(当然你完全可以自定义)

        设置字体大小,设置内容(暂略)

                使用预设

                由于我们设置每一组按钮都会定义其大小,宽度,而这些数据是通用的,我们可以直接定义预设变量进行使用,防止后续修改麻烦。操作如下

        在res/values/dimens.xml 进行添加(如果没有对应文件可以自己新建),得到内容如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <dimen name="button_font_size">25sp</dimen>
  4. <dimen name="button_height">75dp</dimen>
  5. </resources>

            然后我们就可以直接在Button里面通过下列引用进行调用了

@dimen/button_height 
@dimen/button_font_size

        ,最后得到一个如下的代码(第一行)

        

  1. <Button
  2. android:id="@+id/zero"
  3. android:tag="zero"
  4. android:layout_columnWeight="1"
  5. android:layout_margin="5dp"
  6. android:background="#00BCD4"
  7. android:text="0"
  8. android:height="@dimen/button_height"
  9. android:textSize="@dimen/button_font_size" />
  10. <Button
  11. android:id="@+id/one"
  12. android:tag="one"
  13. android:text="1"
  14. android:height="@dimen/button_height"
  15. android:textSize="@dimen/button_font_size"
  16. android:background="#00BCD4"
  17. android:layout_columnWeight="1"
  18. android:layout_margin="5dp"/>
  19. <Button
  20. android:id="@+id/two"
  21. android:tag="two"
  22. android:text="2"
  23. android:height="@dimen/button_height"
  24. android:textSize="@dimen/button_font_size"
  25. android:background="#00BCD4"
  26. android:layout_margin="5dp"
  27. android:layout_columnWeight="1"/>
  28. <Button
  29. android:id="@+id/three"
  30. android:tag="three"
  31. android:height="@dimen/button_height"
  32. android:textSize="@dimen/button_font_size"
  33. android:background="#00BCD4"
  34. android:layout_columnWeight="1"
  35. android:layout_margin="5dp"
  36. android:text="3"
  37. />

        我们可以看到,我还定义了每个Button的ID和tag属性,这将为后面定义按钮事件起作用,建议大家进行到这一步就先写着,免得后面一个一个添加挺麻烦的。

   得到效果如下

                ImageButton

        如果各位想使按钮里面附加图像,可以使用ImageButton类型,ImageButton类型提供srcCompat接口连接图像,如下:

  1. <ImageButton
  2. android:id="@+id/delete"
  3. android:layout_width="0dp"
  4. android:layout_height="@dimen/button_height"
  5. android:layout_columnWeight="1"
  6. android:layout_margin="10dp"
  7. android:background="#00BCD4"
  8. android:gravity="center"
  9. android:onClick="Calculate"
  10. android:tag="delete"
  11. app:srcCompat="@android:drawable/ic_input_delete" />

最后就是搬砖工作了,按照上面的方法一个一个把按钮填上去吧。(懒得填的可以看后面我发的代码,复制粘贴就行。)

功能实现

        搞完页面功能后,我们还需要实现计算器内核功能,即计算功能和输入输出功能。首先我们先跳转到java文件夹下的XXXacivity.java文件,大致内容如下:

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class CalculateActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_calculate);
  9. }
  10. }

由于我这里是零基础开发教程,所以我就不讲特别细的JAVA知识了。

        内容显示

                首先我们需要获取到计算器显示器的内容,即那个TextView文件,我们先定义一个方法

  1. public void Calculate(View view)
  2. {
  3. }

        然后用下列语句获取我们定义的TextView组件,以及其包含的内容

  1. TextView textView=findViewById(R.id.tv_result); //通过id获取组件
  2. String data=textView.getText().toString(); //获取组件内容

                然后我们的目标就是根据按钮的类型判断该做什么事情

        举个例子就是,如果我按的是“1”,那么显示框中也应该添加一个字符“1"才对。这里我使用的是switch方法,大概就是下面这种逻辑

  1. switch ((String) view.getTag() //根据tag判断我按下的是哪个按钮)
  2. {
  3. case "one":
  4. data=data+"1"; //对字符串进行处理
  5. break;
  6. case "xxx":
  7. .....
  8. }

        最后,我们将处理好的新的data重新写回textView上,达到按按钮就增加输入行的效果。

整个方法的框架如下:

  1. public void Calculate(View view)
  2. {
  3. TextView textView=findViewById(R.id.tv_result); //通过id获取组件
  4. String data=textView.getText().toString(); //获取组件内容
  5. switch ((String) view.getTag())
  6. {
  7. case "one":
  8. data=data+"1";
  9. break;
  10. case "xxxx"
  11. ......
  12. }
  13. textView.setText(data);//写回
  14. }

 有兴趣的小伙伴可以自己写写,当然也可以直接抄我写的。

        计算处理

               最后我们要处理等号,因为其他操作都是单纯的处理字符串,而等号操作要识别字符串内部的实际含义,然后进行计算。

                最开始我想的是,如果Java有Python里面的eval函数就好了,然后我就去网上搜了一下,发现javax包里面确实可以实现eval函数,然而!我们这里不是JDK,而是SDK,所以是用不了滴。(当然网上也有使用javaScript插件的,但是我试过直接崩了,有兴趣的可以去试试)。

                还记得学数据结构的时候那个经典的实验吗,用栈结构处理字符串算式。

        让我们新建一个类,Caculate.java。写入以下内容。(别把自己的包路径也删了,就是那个package开头的指令。

  1. public class Caculate {
  2. /**
  3. * 判断表达式是不是只有一个数字
  4. *
  5. * @param str 原值
  6. * @return 数字非法性校验结果,失败返回false;
  7. */
  8. private static boolean isNumber(String str) {
  9. for (int i = 0; i < str.length(); i++) {
  10. if (!Character.isDigit(str.charAt(i)) && str.charAt(i) != '.' && str.charAt(i) != ' ') {
  11. return false;
  12. }
  13. }
  14. return true;
  15. }
  16. /**
  17. * 解析算式,并计算算式结果;
  18. *
  19. * @param str 算式的字符串
  20. * @return double类型的算式结果
  21. */
  22. public static Double getResult(String str) {
  23. // 递归头
  24. if (str.isEmpty() || isNumber(str)) {
  25. return str.isEmpty() ? 0 : Double.parseDouble(str);
  26. }
  27. //递归体
  28. if (str.contains(")")) {
  29. // 最后一个左括号
  30. int lIndex = str.lastIndexOf("(");
  31. // 对于的右括号
  32. int rIndex = str.indexOf(")", lIndex);
  33. return getResult(str.substring(0, lIndex) + getResult(str.substring(lIndex + 1, rIndex)) + str.substring(rIndex + 1));
  34. }
  35. if (str.contains("+")) {
  36. int index = str.lastIndexOf("+");
  37. return getResult(str.substring(0, index)) + getResult(str.substring(index + 1));
  38. }
  39. if (str.contains("-")) {
  40. int index = str.lastIndexOf("-");
  41. return getResult(str.substring(0, index)) - getResult(str.substring(index + 1));
  42. }
  43. if (str.contains("*")) {
  44. int index = str.lastIndexOf("*");
  45. return getResult(str.substring(0, index)) * getResult(str.substring(index + 1));
  46. }
  47. if (str.contains("/")) {
  48. int index = str.lastIndexOf("/");
  49. return getResult(str.substring(0, index)) / getResult(str.substring(index + 1));
  50. }
  51. // 出错
  52. return null;
  53. }
  54. }

调用这个getResult方法就能处理字符串啦。整合一下所有功能试试吧。

代码附录

        页面设计:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#EEE8E8E8"
  7. android:orientation="vertical"
  8. android:padding="20dp">
  9. <ScrollView
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="vertical">
  16. <TextView
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:gravity="center"
  20. android:text="简单计算器"
  21. android:textColor="@color/black"
  22. android:textSize="20sp" />
  23. <TextView
  24. android:id="@+id/tv_result"
  25. android:layout_width="match_parent"
  26. android:layout_height="129dp"
  27. android:background="@color/white"
  28. android:gravity="right|bottom"
  29. android:text=""
  30. android:textColor="@color/black"
  31. android:textSize="25sp" />
  32. <GridLayout
  33. android:layout_width="match_parent"
  34. android:layout_height="wrap_content"
  35. android:columnCount="4"
  36. android:rowCount="6">
  37. <Button
  38. android:id="@+id/btn_cancel"
  39. android:layout_width="0dp"
  40. android:layout_height="@dimen/button_height"
  41. android:layout_columnWeight="1"
  42. android:layout_margin="10dp"
  43. android:background="#00BCD4"
  44. android:gravity="center"
  45. android:onClick="Calculate"
  46. android:tag="cancel"
  47. android:text="c"
  48. android:textColor="@color/black"
  49. android:textSize="@dimen/button_font_size" />
  50. <ImageButton
  51. android:id="@+id/delete"
  52. android:layout_width="0dp"
  53. android:layout_height="@dimen/button_height"
  54. android:layout_columnWeight="1"
  55. android:layout_margin="10dp"
  56. android:background="#00BCD4"
  57. android:gravity="center"
  58. android:onClick="Calculate"
  59. android:tag="delete"
  60. app:srcCompat="@android:drawable/ic_input_delete" />
  61. <Button
  62. android:id="@+id/zero"
  63. android:tag="zero"
  64. android:layout_width="0dp"
  65. android:layout_height="@dimen/button_height"
  66. android:layout_columnWeight="1"
  67. android:layout_margin="10dp"
  68. android:onClick="Calculate"
  69. android:background="#00BCD4"
  70. android:gravity="center"
  71. android:text="0"
  72. android:textColor="@color/black"
  73. android:textSize="@dimen/button_font_size" />
  74. <Button
  75. android:id="@+id/division"
  76. android:tag="division"
  77. android:layout_width="0dp"
  78. android:layout_height="@dimen/button_height"
  79. android:layout_columnWeight="1"
  80. android:layout_margin="10dp"
  81. android:background="#00BCD4"
  82. android:gravity="center"
  83. android:onClick="Calculate"
  84. android:text="/"
  85. android:textColor="@color/black"
  86. android:textSize="24sp" />
  87. <Button
  88. android:id="@+id/seven"
  89. android:layout_width="0dp"
  90. android:layout_height="@dimen/button_height"
  91. android:layout_columnWeight="1"
  92. android:layout_margin="10dp"
  93. android:background="#00BCD4"
  94. android:gravity="center"
  95. android:tag="seven"
  96. android:onClick="Calculate"
  97. android:text="7"
  98. android:textColor="@color/black"
  99. android:textSize="@dimen/button_font_size" />
  100. <Button
  101. android:id="@+id/eight"
  102. android:layout_width="0dp"
  103. android:layout_height="@dimen/button_height"
  104. android:layout_columnWeight="1"
  105. android:layout_margin="10dp"
  106. android:background="#00BCD4"
  107. android:gravity="center"
  108. android:tag="eight"
  109. android:onClick="Calculate"
  110. android:text="8"
  111. android:textColor="@color/black"
  112. android:textSize="@dimen/button_font_size" />
  113. <Button
  114. android:id="@+id/nine"
  115. android:layout_width="0dp"
  116. android:layout_height="@dimen/button_height"
  117. android:layout_columnWeight="1"
  118. android:layout_margin="10dp"
  119. android:background="#00BCD4"
  120. android:gravity="center"
  121. android:onClick="Calculate"
  122. android:tag="nine"
  123. android:text="9"
  124. android:textColor="@color/black"
  125. android:textSize="@dimen/button_font_size" />
  126. <Button
  127. android:id="@+id/mul"
  128. android:layout_width="0dp"
  129. android:layout_height="@dimen/button_height"
  130. android:layout_columnWeight="1"
  131. android:layout_margin="10dp"
  132. android:background="#00BCD4"
  133. android:gravity="center"
  134. android:onClick="Calculate"
  135. android:tag="mul"
  136. android:text="X"
  137. android:textColor="@color/black"
  138. android:textSize="@dimen/button_font_size" />
  139. <Button
  140. android:id="@+id/four"
  141. android:layout_width="0dp"
  142. android:layout_height="@dimen/button_height"
  143. android:layout_columnWeight="1"
  144. android:layout_margin="10dp"
  145. android:background="#00BCD4"
  146. android:gravity="center"
  147. android:onClick="Calculate"
  148. android:tag="four"
  149. android:text="4"
  150. android:textColor="@color/black"
  151. android:textSize="@dimen/button_font_size" />
  152. <Button
  153. android:id="@+id/five"
  154. android:layout_width="0dp"
  155. android:layout_height="@dimen/button_height"
  156. android:layout_columnWeight="1"
  157. android:layout_margin="10dp"
  158. android:background="#00BCD4"
  159. android:gravity="center"
  160. android:onClick="Calculate"
  161. android:tag="five"
  162. android:text="5"
  163. android:textColor="@color/black"
  164. android:textSize="@dimen/button_font_size" />
  165. <Button
  166. android:id="@+id/six"
  167. android:layout_width="0dp"
  168. android:layout_height="@dimen/button_height"
  169. android:layout_columnWeight="1"
  170. android:layout_margin="10dp"
  171. android:background="#00BCD4"
  172. android:gravity="center"
  173. android:onClick="Calculate"
  174. android:tag="six"
  175. android:text="6"
  176. android:textColor="@color/black"
  177. android:textSize="@dimen/button_font_size" />
  178. <Button
  179. android:id="@+id/sub"
  180. android:layout_width="0dp"
  181. android:layout_height="@dimen/button_height"
  182. android:layout_columnWeight="1"
  183. android:layout_margin="10dp"
  184. android:background="#00BCD4"
  185. android:gravity="center"
  186. android:onClick="Calculate"
  187. android:tag="sub"
  188. android:text="—"
  189. android:textColor="@color/black"
  190. android:textSize="@dimen/button_font_size" />
  191. <Button
  192. android:id="@+id/one"
  193. android:layout_width="0dp"
  194. android:layout_height="74dp"
  195. android:layout_columnWeight="1"
  196. android:layout_margin="10dp"
  197. android:background="#00BCD4"
  198. android:gravity="center"
  199. android:onClick="Calculate"
  200. android:tag="one"
  201. android:text="1"
  202. android:textColor="@color/black"
  203. android:textSize="@dimen/button_font_size" />
  204. <Button
  205. android:id="@+id/two"
  206. android:layout_width="0dp"
  207. android:layout_height="@dimen/button_height"
  208. android:layout_columnWeight="1"
  209. android:layout_margin="10dp"
  210. android:background="#00BCD4"
  211. android:onClick="Calculate"
  212. android:gravity="center"
  213. android:tag="two"
  214. android:text="2"
  215. android:textColor="@color/black"
  216. android:textSize="@dimen/button_font_size" />
  217. <Button
  218. android:id="@+id/three"
  219. android:layout_width="0dp"
  220. android:layout_height="@dimen/button_height"
  221. android:layout_columnWeight="1"
  222. android:layout_margin="10dp"
  223. android:background="#00BCD4"
  224. android:onClick="Calculate"
  225. android:gravity="center"
  226. android:text="3"
  227. android:tag="three"
  228. android:textColor="@color/black"
  229. android:textSize="@dimen/button_font_size" />
  230. <Button
  231. android:id="@+id/add"
  232. android:layout_width="0dp"
  233. android:layout_height="@dimen/button_height"
  234. android:layout_columnWeight="1"
  235. android:layout_margin="10dp"
  236. android:background="#00BCD4"
  237. android:gravity="center"
  238. android:onClick="Calculate"
  239. android:text="+"
  240. android:tag="add"
  241. android:textColor="@color/black"
  242. android:textSize="@dimen/button_font_size" />
  243. <Button
  244. android:id="@+id/dot"
  245. android:tag="dot"
  246. android:layout_width="0dp"
  247. android:layout_height="@dimen/button_height"
  248. android:layout_columnWeight="1"
  249. android:layout_margin="10dp"
  250. android:background="#00BCD4"
  251. android:gravity="center"
  252. android:onClick="Calculate"
  253. android:text="."
  254. android:textColor="@color/black"
  255. android:textSize="@dimen/button_font_size" />
  256. <Button
  257. android:id="@+id/left"
  258. android:layout_width="0dp"
  259. android:layout_height="@dimen/button_height"
  260. android:layout_columnWeight="1"
  261. android:layout_margin="10dp"
  262. android:background="#00BCD4"
  263. android:gravity="center"
  264. android:onClick="Calculate"
  265. android:tag="left"
  266. android:text="("
  267. android:textColor="@color/black"
  268. android:textSize="@dimen/button_font_size" />
  269. <Button
  270. android:id="@+id/right"
  271. android:layout_width="0dp"
  272. android:layout_height="@dimen/button_height"
  273. android:layout_columnWeight="1"
  274. android:layout_margin="10dp"
  275. android:background="#00BCD4"
  276. android:gravity="center"
  277. android:onClick="Calculate"
  278. android:tag="right"
  279. android:text=")"
  280. android:textColor="@color/black"
  281. android:textSize="@dimen/button_font_size" />
  282. <Button
  283. android:id="@+id/equal"
  284. android:tag="equal"
  285. android:layout_width="0dp"
  286. android:layout_height="@dimen/button_height"
  287. android:layout_columnWeight="1"
  288. android:layout_margin="10dp"
  289. android:background="#00BCD4"
  290. android:gravity="center"
  291. android:onClick="Calculate"
  292. android:text="="
  293. android:textColor="@color/black"
  294. android:textSize="@dimen/button_font_size" />
  295. </GridLayout>
  296. </LinearLayout>
  297. </ScrollView>
  298. </LinearLayout>

XXXactivity.java

  1. package com.example.caculation;
  2. import com.example.caculation.Caculate;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. public class MainActivity extends AppCompatActivity {
  8. TextView textView;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. setTheme(R.style.Theme_Caculation);
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. textView=findViewById(R.id.tv_result);
  15. }
  16. public void Calculate(View view)
  17. {
  18. String data=textView.getText().toString();
  19. switch ((String) view.getTag())
  20. {
  21. case "one":
  22. data=data+"1";
  23. break;
  24. case "two":
  25. data=data+"2";
  26. break;
  27. case "three":
  28. data=data+"3";
  29. break;
  30. case "four":
  31. data=data+"4";
  32. break;
  33. case "five":
  34. data=data+"5";
  35. break;
  36. case "six":
  37. data=data+"6";
  38. break;
  39. case "seven":
  40. data=data+"7";
  41. break;
  42. case "eight":
  43. data=data+"8";
  44. break;
  45. case "nine":
  46. data=data+"9";
  47. break;
  48. case "zero":
  49. data=data+"0";
  50. break;
  51. case "double_zero":
  52. data=data+"00";
  53. break;
  54. case "cancel":
  55. data="";
  56. break;
  57. case "delete":
  58. data=data.substring(0,data.length()-1);
  59. break;
  60. case "mul":
  61. data=data+"*";
  62. break;
  63. case "division":
  64. data=data+"/";
  65. break;
  66. case "sub":
  67. data=data+"-";
  68. break;
  69. case "add":
  70. data=data+"+";
  71. break;
  72. case "dot":
  73. if (data.substring(data.length()-1).equals("."))
  74. return;
  75. else
  76. {
  77. data=data+".";
  78. break;
  79. }
  80. case "left":
  81. data=data+"(";
  82. break;
  83. case "right":
  84. data=data+")";
  85. break;
  86. case "equal":
  87. data=String.valueOf(Caculate.getResult(data));
  88. break;
  89. }
  90. textView.setText(data);
  91. }
  92. }

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

闽ICP备14008679号