Android开发实战:创建一个简单的计算器应用
用AndroidStudio做一个简易的计算器界面

⽤AndroidStudio做⼀个简易的计算器界⾯对于计算器的界⾯,我是在xml⽂件⾥编写<TableLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MyCale"><TextViewandroid:gravity="right"android:id="@+id/input1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:height="100px"android:text="3+4"android:textSize="50px"/><TextViewandroid:id="@+id/input2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:height="200px"android:text="7"android:textSize="100px"/><TableLayoutandroid:stretchColumns="0,1,2,3"><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:height="140px"android:text="AC"android:textSize="50px"/><Buttonandroid:layout_width="match_parent"android:layout_height="match_parent"android:height="140px"android:text="DEL"android:textSize="50px"/><Buttonandroid:layout_width="match_parent"android:layout_height="match_parent"android:height="140px"android:text="+/-"android:textSize="50px"/><Buttonandroid:layout_width="72dp"android:layout_height="match_parent"android:height="140px"android:text="÷"android:textSize="50px"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:height="140px"android:text="7"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:height="140px"android:text="8"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="×"android:textSize="50px"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="4"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="5"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="6"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="-"android:textSize="50px"/></TableRow><TableRow><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="1"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="2"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="3"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:height="140px"android:text="+"android:textSize="50px"/></TableRow><TableRow><Buttonandroid:layout_span="2"android:layout_width="match_parent"android:layout_height="match_parent" android:height="140px"android:text="0"android:textSize="50px"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:height="140px"android:text="."android:textSize="50px"/><Buttonandroid:layout_height="wrap_content" android:layout_width="wrap_content"</TableLayout></TableLayout>下⾯是我在编写时遇到的问题:1.Tablelayout⾥如何使⼀⾏的四个按钮占满⼀⾏?<TableLayoutandroid:stretchColumns="0,1,2,3">控制某列的按钮拉伸占满剩下的空间,0是第⼀列。
c语言制作简单的计算器

c语言制作简单的计算器在计算机编程领域中,C语言是一种广泛使用的编程语言之一。
它具有简单易学的特点,并且能够进行复杂的计算和逻辑处理。
今天,我们就来学习一下如何使用C语言来制作一个简单的计算器。
首先,我们需要明确我们计算器的功能和界面设计。
本次制作的计算器将具有基本的四则运算功能,即加法、减法、乘法和除法。
我们将在控制台中实现计算器的操作,并通过简单的用户界面进行交互。
接下来,我们需要创建一个C语言程序的框架。
首先,我们需要包含头文件,以便在程序中使用各种函数和变量。
我们可以使用"#include" 来引入头文件,例如:```c#include <stdio.h>```然后,我们需要定义主函数 `main()`。
在这个函数中,我们将编写计算器的逻辑代码。
让我们开始编写函数吧!首先,我们需要声明一些变量来存储用户输入的数字和运算符。
我们可以使用 `int` 来定义整数变量,并用 `char` 定义字符变量。
例如:```cint num1, num2, result;char operator;```接下来,我们需要提示用户输入要进行的运算,并读取用户输入的数字和运算符。
我们可以使用 `printf` 和 `scanf` 函数来完成这个任务,例如:```cprintf("请输入第一个数字: ");scanf("%d", &num1);printf("请输入运算符(+, -, *, /): ");scanf(" %c", &operator);printf("请输入第二个数字: ");scanf("%d", &num2);```注意,我们在读取运算符之前添加了空格,这是为了消除输入缓冲区中可能残留的换行符等字符。
接下来,我们需要编写代码来进行实际的计算操作。
Android框架上的简单计算器界面设计

为了深入理解和应用Android框架系统的控件以及更加熟练的掌握eclips的开发技巧,所以就决定做一个简单的计算器来了解和介绍一下Android界面设计的基本技巧。
界面的样子如下:这个界面由一个编辑框和很多按钮组成。
他的功能跟Windows下的计算其功能差不多,就是实现+-*/等功能。
这个还实现了一个菜单的功能。
按一下Exit就可以离开这个应用程序了。
Android框架定义了很多布局(Layout)架构。
布局就像容器,里面可以装下很多控件。
布局里面还可以套用其他的布局。
这样就可以实现界面的多样化以及设计的灵活性。
在这个界面中,我们应用了一个LinearLayout的布局,它是垂直向下扩展的。
在这个LinearLayout中,我们可以应用一个EditText和一个TableLayout 作为他的子控件。
在TableLayout布局可以实现以表格的形式来布局空间。
而TableRow是用来定义每一行。
在TableLayout中,列的宽度是取其中这一列中宽度最大的控件的宽度。
这个应用程序正好用到5x5的一个表格,TableLayout将是最好的选择了。
Android界面编程的一个优势是他的布局全部可以用xml文件来描述,实现了即改即现的方式。
ADT又在Eclips里面做了一个Outline的插件。
当你打开布局xml文件的时候,Outline就会显示布局的所有控件以及他们的结构。
双击其中的控件就会打开空间的属性。
你就可以直接编辑属性来轻松实现界面的布局。
所改的属性也将立刻在xml文件中显示出来。
以下是描述这个简单计算器的界面xml文件给大家做个参考:android:background="#ff808080"><EditText android:id="@+id/input"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:gravity="right"android:editable ="false"/><TableLayout xmlns:android="/apk/res/android" android:layout_width="fill_parent"android:layout_height="fill_parent"><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><Button android:id="@+id/seven"android:text="7"android:layout_height="wrap_content"android:layout_width="63sp"/><Button android:id="@+id/eight"android:text="8"android:layout_width="63sp"android:layout_height="wrap_content" /><Button android:id="@+id/nine"android:text="9"android:layout_width="63sp"android:layout_height="wrap_content" /><Button android:id="@+id/divide"android:text="/"android:layout_width="63sp"android:layout_height="wrap_content" /><Button android:id="@+id/sqrt"android:text="sqrt"android:layout_width="63sp"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><Button android:id="@+id/four"android:text="4"android:layout_width="63sp"android:layout_height="wrap_content" /><Button android:id="@+id/five"android:text="5"<Button android:id="@+id/multiply"android:text="*"android:layout_width="63sp"android:layout_height="wrap_content" /> <Button android:id="@+id/remainder"android:text="%"android:layout_width="63sp"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><Button android:id="@+id/one"android:text="1"android:layout_width="63sp"android:layout_height="wrap_content" /> <Button android:id="@+id/two"android:text="2"android:layout_width="63sp"android:layout_height="wrap_content" /> <Button android:id="@+id/three"android:text="3"android:layout_width="63sp"android:layout_height="wrap_content" /> <Button android:id="@+id/minus"android:text="-"android:layout_width="63sp"android:layout_height="wrap_content" /> <Button android:id="@+id/convert"android:text="1/x"android:layout_width="63sp"android:layout_height="wrap_content" /> </TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><Button android:id="@+id/zero"android:text="0"android:layout_width="63sp"android:layout_height="wrap_content" /> <Button android:id="@+id/digit"android:text="."。
在Android Studio中开发一个简单的计算器应用

在Android Studio中开发一个简单的计算器应用随着智能手机的普及,计算器应用成为了人们生活中不可或缺的工具之一。
为了满足用户的需求,我们决定在Android Studio中开发一个简单的计算器应用。
这个应用将提供基本的四则运算功能,并且界面简洁易用。
首先,在Android Studio中创建一个新的项目。
选择合适的应用名称和包名。
接下来,我们需要设计应用的用户界面。
我们希望用户界面简洁明了,方便用户使用。
可以考虑使用一个TextView显示计算结果,以及多个Button用于输入数字和操作符。
在开发计算器的过程中,我们需要处理用户的输入并进行相应的计算。
为此,我们可以使用一个EditText控件来接收用户的输入,并通过按钮的点击事件来获取用户点击的数字和操作符。
然后,我们可以使用Java中的数学库来进行四则运算,并将结果显示在TextView中。
通过Android Studio提供的布局编辑器,我们可以轻松地将TextView和Button布局在界面上。
可以使用LinearLayout或RelativeLayout进行布局,根据需要设置各个控件之间的位置和大小。
此外,还可以设置按钮的点击事件,使其能够响应用户的输入。
在处理用户输入时,需要考虑一些特殊情况。
例如,当用户输入非法字符或进行除法时需要进行错误处理。
可以通过在代码中添加条件判断语句来实现这些功能。
另外,还可以添加一些额外的功能,如清除输入、撤销操作等,以提升用户体验。
在开发过程中,可以使用Android Studio提供的调试工具来检查代码是否正常运行。
可以设置断点,逐步执行代码,并查看变量的值。
这些工具能够帮助我们快速定位和修复错误。
为了使应用具有良好的用户体验,我们还可以添加一些动画效果和过渡效果。
例如,当用户点击按钮时,可以添加按钮缩放、淡入淡出等动画效果,使界面更加生动有趣。
除了基本功能以外,我们还可以考虑添加更多的功能来完善应用。
android开发计算器课程设计

android开发计算器课程设计一、教学目标本课程旨在通过学习Android开发计算器,使学生掌握Android应用开发的基本知识和技能,能够独立设计和开发简单的Android计算器应用。
1.理解Android开发的基本概念和框架。
2.掌握Android计算器的界面设计和事件处理。
3.熟悉Android计算器的数据存储和运算逻辑。
4.能够使用Android Studio进行基本的Android应用开发。
5.能够设计并实现一个简单的Android计算器应用。
6.能够对Android计算器应用进行调试和优化。
情感态度价值观目标:1.培养学生的创新意识和问题解决能力。
2.培养学生对Android开发技术的兴趣和热情。
3.培养学生的团队合作精神和自主学习能力。
二、教学内容本课程的教学内容主要包括Android开发的基本概念和框架、Android计算器的界面设计和事件处理、Android计算器的数据存储和运算逻辑等。
1.Android开发的基本概念和框架:介绍Android Studio的使用、Android应用的结构和生命周期、Activity和Service的概念和用法。
2.Android计算器的界面设计:介绍布局文件的使用、控件的属性和事件处理。
3.Android计算器的数据存储和运算逻辑:介绍SharedPreferences和SQLite数据库的使用、运算器的实现和数据的存储。
三、教学方法本课程将采用讲授法、案例分析法和实验法等多种教学方法相结合的方式进行教学。
1.讲授法:通过讲解和演示,使学生理解Android开发的基本概念和框架、Android计算器的界面设计和事件处理、Android计算器的数据存储和运算逻辑。
2.案例分析法:通过分析典型的Android计算器应用案例,使学生掌握Android计算器的界面设计和事件处理、数据存储和运算逻辑的实现。
3.实验法:通过实际操作和调试,使学生掌握Android计算器的界面设计和事件处理、数据存储和运算逻辑的实现。
基于Android计算器功能的实现

基于Android计算器功能的实现摘要随着移动通信技术和网络技术的迅速发展,手机系统发生了巨大的变化,从进行简单通话功能的非智能手机到现在集上网、聊天、收发邮件、视屏电话会议等各种服务于娱乐为一身的智能手机系统。
由Google公司牵头众多实力雄厚的软硬件厂商加盟成立了OMS联盟,并推出的Android平台手机系统。
作为时代的新生儿,Android手机操作平台有着得天独厚的优势广阔的发展前景。
本文正是基于Android平台开发的计算器。
论文主要阐述以面向对象的程序开发语言eclipse为开发工具, 设计并实现基于Android的计算器。
论文首先介绍了Android开发平台和环境以及Android 工程的重要部分,其次对计算器系统和程序进行了分析和设计,再次对计算器进行了系统的实现,并对系统进行了严格的验证,最后本文对所做工作进行了总结。
关键词:手机,Android,用户界面,计算器THE CALCULATOR FUNCTION REALIZATION BASEDON ANDROIDABSTRACTWith the development of mobile communication technology and network technology, great changes have taken place on the mobile phone system. The original mobile phone only has the functions of communication and Smartphone has the functions of internet, chat, video conferencing and other services and entertainment. Android mobile phone platform which was developed by Google Company and many equipment manufacturers of hardware and software vendors, as the era of neonatal has a unique advantage and prospects for development. This article is based on the Android platform calculator. The thesis describes object-oriented programming language eclipse development tools, design and implement a calculator based on the Android. The paper first introduces the Android development platform and environment as well as an important part of the Android project, followed by the calculator systems and procedures analysis and design, the realization of the system once again the calculator, and strict verification, Finally, a summary of the work done.Key words: mobile phone, Android, UI, calculator目录1 引言 (1)1.1 课题背景及意义 (1)1.1.1 课题背景 (1)1.1.2 课题意义 (1)1.2 智能手机平台系统 (2)1.3 课题来源 (3)2 基于Android平台计算器相关技术概述 (4)2.1 Android手机平台 (5)2.1.1 Android起源及优势 (5)2.1.2 Android体系结构 (5)2.1.3 Android平台功能特征 (7)2.2 Android SDK介绍 (9)2.3 XML技术 (10)2.4 JNI技术 (11)3 基于Android平台计算器设计 (12)3.1 系统的总体设计 (12)3.2 程序流程和系统功能设计 (13)3.2.1 程序流程设计 (13)3.2.2 系统功能设计 (14)3.3基于Android平台系统具体设计 (15)3.3.1 总体模块详细设计 (15)3.3.2 输入模块详细设计 (16)3.3.3 显示模块详细设计 (16)3.3.4 计算模块详细设计 (17)4 计算器系统实现 (18)4.1 Android应用程序构成 (18)4.2 Android应用系统文件结构 (19)4.3 构建Android应用程序用户界面 (21)4.3.1 Android应用程序界面组件 (21)4.3.2 Android应用系统界面组件 (22)4.3.3 Android用户界面的实现 (24)4.4 计算模块的实现 (28)4.5 计算器演示 (30)5 结论 (32)谢辞 (33)参考文献 (34)附录1: (36)附录2: (38)1 引言1.1 课题背景及意义1.1.1 课题背景随着社会的发展,经济水平的提高,手机越来越普及。
制作一个简易计算器——基于AndroidStudio实现

制作⼀个简易计算器——基于AndroidStudio实现⼀个计算器Android程序的源码部分分为主⼲和细节两部分。
⼀、主⼲1. 主⼲的构成计算器的布局事件(即计算器上的按钮、⽂本框)监听实现计算2. 详细解释 假设我们的项⽬名为Calculator,⽽布局名称(Layout Name)为默认的activity_main 。
即设置如下图所⽰: 在这种前提下,有:设置计算器布局的⽂件:Calculator/app/src/main/res/layout/activity_main.xml事件监听和计算实现在同⼀个⽂件⾥:Calculator/app/src/main/java/下的⼀个⼦⽬录⾥的MainActivity.java 即如下图所⽰:计算器布局代码(写在activity_main.xml⽂件⾥):1<?xml version="1.0" encoding="utf-8"?>2<ScrollView xmlns:android="/apk/res/android"3 android:layout_width="fill_parent"4 android:layout_height="wrap_content"5>6<LinearLayout7android:layout_width="fill_parent"8 android:layout_height="fill_parent"9 android:orientation="vertical">10<EditText11android:id="@+id/input"12 android:layout_width="fill_parent"13 android:layout_height="wrap_content"14 android:layout_gravity="center"15 android:editable="false"16 android:hint="@string/shuru"/>1718<EditText19android:id="@+id/output"20 android:layout_width="fill_parent"21 android:layout_height="wrap_content"22 android:layout_gravity="center"23 android:editable="true"24 android:gravity="right"25 android:hint="@string/shuchu"/>2627<RelativeLayout28android:layout_width="fill_parent"29 android:layout_height="wrap_content">3031<Button32android:id="@+id/seven"33 android:layout_width="80dp"34 android:layout_height="70dp"35 android:layout_alignParentLeft="true"36 android:text="@string/seven"37 android:textSize="40sp"/>3839<Button44 android:text="@string/eight"45 android:textSize="40sp"/>4647<Button48android:id="@+id/nine"49 android:layout_width="80dp"50 android:layout_height="70dp"51 android:layout_toRightOf="@id/eight"52 android:text="@string/nine"53 android:textSize="40sp"/>5455<Button56android:id="@+id/add"57 android:layout_width="80dp"58 android:layout_height="70dp"59 android:layout_alignParentRight="true"60 android:layout_toRightOf="@id/nine"61 android:text="@string/add"62 android:textSize="40sp"/>6364<Button65android:id="@+id/four"66 android:layout_width="80dp"67 android:layout_height="70dp"68 android:layout_alignParentLeft="true"69 android:layout_below="@id/seven"70 android:text="@string/four"71 android:textSize="40sp"/>7273<Button74android:id="@+id/five"75 android:layout_width="80dp"76 android:layout_height="70dp"77 android:layout_below="@id/eight"78 android:layout_toRightOf="@id/four"79 android:text="@string/five"80 android:textSize="40sp"/>8182<Button83android:id="@+id/six"84 android:layout_width="80dp"85 android:layout_height="70dp"86 android:layout_below="@id/nine"87 android:layout_toRightOf="@id/five"88 android:text="@string/six"89 android:textSize="40sp"/>9091<Button92android:id="@+id/subtract"93 android:layout_width="80dp"94 android:layout_height="70dp"95 android:layout_alignParentRight="true"96 android:layout_below="@id/add"97 android:layout_toRightOf="@id/six"98 android:text="@string/subtract"99 android:textSize="40sp"/>100101<Button102android:id="@+id/one"103 android:layout_width="80dp"104 android:layout_height="70dp"105 android:layout_alignParentLeft="true" 106 android:layout_below="@id/four"107 android:text="@string/one"108 android:textSize="40sp"/>109110<Button111android:id="@+id/two"112 android:layout_width="80dp"113 android:layout_height="70dp"114 android:layout_below="@id/five"115 android:layout_toRightOf="@id/one" 116 android:text="@string/two"117 android:textSize="40sp"/>118119<Button120android:id="@+id/three"121 android:layout_width="80dp"122 android:layout_height="70dp"123 android:layout_below="@id/six"128<Button129android:id="@+id/multiply"130 android:layout_width="80dp"131 android:layout_height="70dp"132 android:layout_alignParentRight="true" 133 android:layout_below="@id/subtract" 134 android:layout_toRightOf="@id/three" 135 android:text="@string/multiply"136 android:textSize="40sp"/>137138<Button139android:id="@+id/zero"140 android:layout_width="80dp"141 android:layout_height="70dp"142 android:layout_alignParentLeft="true" 143 android:layout_below="@id/one"144 android:text="@string/zero"145 android:textSize="40sp"/>146147<Button148android:id="@+id/clear"149 android:layout_width="80dp"150 android:layout_height="70dp"151 android:layout_below="@id/two"152 android:layout_toRightOf="@id/zero" 153 android:text="@string/clear"154 android:textSize="40sp"/>155156<Button157android:id="@+id/result"158 android:layout_width="80dp"159 android:layout_height="70dp"160 android:layout_below="@id/three"161 android:layout_toRightOf="@id/clear" 162 android:text="@string/result"163 android:textSize="40sp"/>164165<Button166android:id="@+id/divide"167 android:layout_width="80dp"168 android:layout_height="70dp"169 android:layout_alignParentRight="true" 170 android:layout_below="@id/multiply" 171 android:layout_toRightOf="@id/result" 172 android:text="@string/divide"173 android:textSize="40sp"/>174175<Button176android:id="@+id/dot"177 android:layout_width="80dp"178 android:layout_height="70dp"179 android:layout_alignParentLeft="true" 180 android:layout_below="@id/zero"181 android:text="@string/dot"182 android:textSize="40sp"/>183<Button184android:id="@+id/writeButton"185 android:layout_width="wrap_content" 186 android:layout_height="wrap_content" 187 android:layout_alignParentLeft="true" 188 android:layout_below="@id/dot"189 android:text="@string/write"190 android:textSize="40sp"/>191<Button192android:id="@+id/readButton"193 android:layout_width="wrap_content" 194 android:layout_height="wrap_content" 195 android:layout_alignParentRight="true" 196 android:layout_below="@id/dot"197 android:text="@string/read"198 android:textSize="40sp"/>199200<CheckBox201android:id="@+id/appendBox"202 android:text="@string/appendBox"203 android:layout_width="wrap_content" 204 android:layout_height="wrap_content" 205 android:layout_alignParentBottom="true" 206 android:layout_toLeftOf="@+id/divide" 207 android:layout_toStartOf="@+id/divide"212213<EditText214215android:layout_width="match_parent"216 android:layout_height="wrap_content"217 android:id="@+id/textView"/>218219<EditText220221android:layout_width="match_parent"222 android:layout_height="wrap_content"223 android:id="@+id/displayView"/>224225<EditText226android:id="@+id/errorzero"227 android:layout_width="fill_parent"228 android:layout_height="wrap_content"229 android:layout_gravity="center"230 android:editable="false"231 android:gravity="center"232/>233<EditText234android:id="@+id/resultText"235 android:layout_width="fill_parent"236 android:layout_height="wrap_content"237 android:layout_gravity="center"238 android:editable="false"239 android:gravity="left"240 android:text="@string/resultText"241/>242</LinearLayout>243</ScrollView>View Code事件监听和实现计算代码(写在MainActivity.java⽂件⾥)1package com.example.lenovo.calculator;2345import android.app.Activity;6import android.content.Context;7import android.os.Bundle;8import android.view.View;9import android.view.View.OnClickListener;10import android.widget.Button;11import android.widget.CheckBox;12import android.widget.EditText;1314import java.io.FileInputStream;15import java.io.FileNotFoundException;16import java.io.FileOutputStream;17import java.io.IOException;1819public class MainActivity extends Activity {20/**21 * Called when the activity is first created.22*/23private EditText output = null;24private EditText input = null;25private Button btn0 = null;26private Button btn1 = null;27private Button btn2 = null;28private Button btn3 = null;29private Button btn4 = null;30private Button btn5 = null;31private Button btn6 = null;32private Button btn7 = null;33private Button btn8 = null;34private Button btn9 = null;35private Button btnadd = null;36private Button btnsubtract = null;37private Button btnmultiply = null;38private Button btndivide = null;39private Button btnclear = null;40private Button btnresult = null;41private Button btndot = null;46private Button writeButton = null;47private Button readButton = null;48private CheckBox appendBox = null;49private EditText textView = null;50private EditText displayView = null;51public String FILE_NAME = "fileDemo.txt";525354private String str = "";//保存数字55private String strold = "";//原数字56private char act = ' ';//记录“加减乘除等于”符号57private int count = 0;//判断要计算的次数,如果超过⼀个符号,先算出来⼀部分58private Float result = null;//计算的输出结果59private Boolean errBoolean = false;//有错误的时候为true,⽆错为false60private Boolean flagBoolean = false;//⼀个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前⾯是数字才可以响应运算消息 61private Boolean flagDot = false; //⼩数点标志位62636465 @Override66public void onCreate(Bundle savedInstanceState) {67super.onCreate(savedInstanceState);68 setContentView(yout.activity_main);6970 output = (EditText) findViewById(R.id.output);71 input = (EditText) findViewById(R.id.input);7273 errorzero = (EditText) findViewById(R.id.errorzero);74 resultText = (EditText) findViewById(R.id.resultText);75 writeButton = (Button) findViewById(R.id.writeButton);76 readButton = (Button) findViewById(R.id.readButton);77 textView = (EditText) findViewById(R.id.textView);78 displayView = (EditText) findViewById(R.id.displayView);79 appendBox = (CheckBox) findViewById(R.id.appendBox);8081 btn0 = (Button) findViewById(R.id.zero);82 btn1 = (Button) findViewById(R.id.one);83 btn2 = (Button) findViewById(R.id.two);84 btn3 = (Button) findViewById(R.id.three);85 btn4 = (Button) findViewById(R.id.four);86 btn5 = (Button) findViewById(R.id.five);87 btn6 = (Button) findViewById(R.id.six);88 btn7 = (Button) findViewById(R.id.seven);89 btn8 = (Button) findViewById(R.id.eight);90 btn9 = (Button) findViewById(R.id.nine);91 btnadd = (Button) findViewById(R.id.add);92 btnsubtract = (Button) findViewById(R.id.subtract);93 btnmultiply = (Button) findViewById(R.id.multiply);94 btndivide = (Button) findViewById(R.id.divide);95 btnclear = (Button) findViewById(R.id.clear);96 btnresult = (Button) findViewById(R.id.result);97 btndot = (Button) findViewById(R.id.dot);98//设置按钮侦听事件99 btn0.setOnClickListener(listener);100 btn1.setOnClickListener(listener);101 btn2.setOnClickListener(listener);102 btn3.setOnClickListener(listener);103 btn4.setOnClickListener(listener);104 btn5.setOnClickListener(listener);105 btn6.setOnClickListener(listener);106 btn7.setOnClickListener(listener);107 btn8.setOnClickListener(listener);108 btn9.setOnClickListener(listener);109//执⾏运算110 btnadd.setOnClickListener(listener);111 btnsubtract.setOnClickListener(listener);112 btnmultiply.setOnClickListener(listener);113 btndivide.setOnClickListener(listener);114 btnclear.setOnClickListener(listener);115 btnresult.setOnClickListener(listener);116117 btndot.setOnClickListener(listener);118119 writeButton.setOnClickListener(writelistener);120 readButton.setOnClickListener(readlistener);121122123// ATTENTION: This was auto-generated to implement the App Indexing API.124// See https://g.co/AppIndexing/AndroidStudio for more information.125 }130public void onClick(View v) {131// TODO Auto-generated method stub132switch (v.getId()) {133//输⼊数字134case R.id.zero:135 num(0);136break;137case R.id.one:138 num(1);139break;140case R.id.two:141 num(2);142break;143case R.id.three:144 num(3);145break;146case R.id.four:147 num(4);148break;149case R.id.five:150 num(5);151break;152case R.id.six:153 num(6);154break;155case R.id.seven:156 num(7);157break;158case R.id.eight:159 num(8);160break;161case R.id.nine:162 num(9);163break;164165case R.id.dot:166 dot();167break;168//执⾏运算169case R.id.add:170 add();171break;172case R.id.subtract:173 sub();174break;175case R.id.multiply:176 multiply();177break;178case R.id.divide:179 divide();180break;181case R.id.clear:182 clear();183break;184//计算结果185case R.id.result:186 result();187if (!errBoolean && flagBoolean) {188 output.setText(String.valueOf(result));189 }190 resultText.setText(strold + act + str + "=" + result+" "); 191break;192193default:194break;195196 }197 input.setText(strold + act + str);198 output.setText(String.valueOf(result));199200201 }202 };203204private OnClickListener writelistener = new OnClickListener() {205 @Override206public void onClick(View view) {207//textView.setText("");208209 FileOutputStream fos = null;214 fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE); 215 }216 String text = resultText.getText().toString();217 fos.write(text.getBytes());218 textView.setText("⽂件写⼊成功,写⼊长度:" + text.length()); 219//resultText.setText("");220221 } catch (FileNotFoundException e) {222 e.printStackTrace();223 } catch (IOException e) {224 e.printStackTrace();225 }226finally {227if (fos != null)228try {229 fos.flush();230 fos.close();231 } catch (IOException e) {232 e.printStackTrace();233 }234 }235 }236 };237private OnClickListener readlistener = new OnClickListener() {238 @Override239public void onClick(View view) {240241 displayView.setText("");242 FileInputStream fis = null;243try {244 fis = openFileInput(FILE_NAME);245if (fis.available() == 0) {246return;247 }248byte[] readBytes = new byte[fis.available()];249while (fis.read(readBytes) != -1) {250251 }252 String text = new String(readBytes);253 displayView.setText(text);254 textView.setText("⽂件读取成功,写⼊长度:" + text.length()); 255256 } catch (FileNotFoundException e) {257 e.printStackTrace();258 } catch (IOException e) {259 e.printStackTrace();260 }261262 }263 };264265private void dot() {266// TODO Auto-generated method stub267268if (!flagDot) {269 str = str + ".";270 flagBoolean = false;271 flagDot = true;272 }273 }274275private void clear() {276// TODO Auto-generated method stub277 str = strold = "";278 count = 0;279 act = ' ';280 result = null;281 flagBoolean = false;282 flagDot = false;283 input.setText(strold + act + str);284 output.setText("");285 errorzero.setText("");286 displayView.setText("");287 textView.setText("");288 resultText.setText("");289 }290291private void divide() {292// TODO Auto-generated method stub293if (flagBoolean) {299300private void multiply() {301// TODO Auto-generated method stub 302if (flagBoolean) {303 check();304 act = '*';305 flagBoolean = false;306 }307 }308309private void sub() {310// TODO Auto-generated method stub 311if (flagBoolean) {312 check();313 act = '-';314 flagBoolean = false;315 }316 }317318private void add() {319// TODO Auto-generated method stub 320if (flagBoolean) {321 check();322 act = '+';323 flagBoolean = false;324 }325 }326327private void check() {328// TODO Auto-generated method stub 329if (count >= 1) {330 result();331 str = String.valueOf(result);332 }333 strold = str;334 str = "";335 count++;336 flagDot = false;337 errorzero.setText("");338 }339340//计算输出结果341private void result() {342// TODO Auto-generated method stub 343if (flagBoolean) {344 Float a, b;345346 a = Float.parseFloat(strold);347 b = Float.parseFloat(str);348349if (b == 0 && act == '/') {350 clear();351 errorzero.setText("除数不能为零!"); 352//output.setText("除数不能为零!"); 353354355//errBoolean=true;356 }357358359if (!errBoolean) {360switch (act) {361case '+':362 result = a + b;363break;364case '-':365 result = a - b;366break;367case '*':368 result = a * b;369break;370case '/':371 result = a / b;372break;373374default:375break;376 }377 }383private void num(int i) {384// TODO Auto-generated method stub385 str = str + String.valueOf(i);386 flagBoolean = true;387 errorzero.setText("");388 }389390 }View Code⼆、细节 仅仅将主⼲部分代码copy下来并不能运⾏程序,因为主⼲代码调⽤了⼀些⽂件的代码,这些⽂件就是我们所说的细节。
简易计算器—窗体应用程序

这是用窗体应用程序写的代码
联系人:王义波 QQ 群:155413201
class Program {
static void Main(string[] args) {
try {
char c; double sum; Console.Write("请输入第一个数:"); double num1 = Convert.ToDouble(Console.ReadLine()); Console.Write("请输入第二个数:"); double num2 = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("请您选择要计算的运算符:1、+ 2、- 3、* 4、/ "); c = Convert.ToChar(Console.ReadLine()); switch (c) {
case '4': if (num2 == 0) { Console.WriteLine("除数不能为0,请重新输入!"); break; } else { sum = num1 / num2; Console.WriteLine("这两个数的商为:{0}", sum); break; }
default: break;
这是用窗体应用程序写的代码
using System; using System.Drawing; using System.Data; using ponentModel; using System.Collections.Generic; using System.Windows.Forms; namespace ConsoleApplication2 {
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Android开发实战:创建一个简单的计算器应用
在如今移动互联网的时代,手机已经成为每个人生活中不可或缺的一部分。
而现代手机中最重要的应用之一,莫过于计算器。
在这篇文章中,我将向大家介绍如何使用Android开发来创建一个简单的计算器应用,让我们一起开始这个富有挑战的实战项目吧!
1. 搭建开发环境
要开始开发一个Android应用,首先需要搭建开发环境。
目前,Android Studio是最常用的开发工具。
你可以去官方网站上下载并安装它。
安装完成后,你可以打开Android Studio,创建一个新的项目来开始你的开发之旅。
2. 设计应用界面
在开发Android应用的过程中,视觉化界面设计是非常重要的一步。
你可以使用Android Studio提供的可视化编辑器来设计你的应用界面。
这里,我们将创建一个简单的计算器应用,所以我们的界面将包含一些按钮和文本框。
3. 添加事件处理器
一旦界面设计完成,接下来需要为按钮添加事件处理器。
在计算器应用中,按钮被点击后,需要相应地执行一些计算操作。
你可以在Java代码中编写事件处理器,然后将其与按钮关联起来。
在事件处理器中,你可以使用适当的逻辑来执行所需的计算操作,并将结果显示在文本框中。
4. 实现基本的计算功能
一个计算器应用的核心是它的计算功能。
在这个实战项目中,我
们将实现基本的加减乘除运算。
你可以使用Java语言中的运算符来实
现这些功能,并确保逻辑正确,并处理一些异常情况,如除以零等。
5. 优化用户体验
在设计应用界面以及实现计算功能之后,你可以考虑进一步优化
用户体验。
例如,你可以添加一些动画效果来让用户感觉更加流畅,
或者添加一些提示信息来引导用户操作。
你还可以考虑添加更多的功能,如清除按钮或百分比计算等,以提高应用的实用性和用户满意度。
6. 调试和测试
在完成应用的开发之后,你需要进行调试和测试来确保应用的正
常运行。
你可以使用Android Studio的调试工具来定位和解决代码中
的错误。
此外,你还可以模拟不同的使用情况和输入来测试应用的性
能和稳定性。
7. 发布和推广
最后,当你对应用的性能和质量满意之后,你可以考虑发布和推
广你的应用。
你可以通过Google Play等应用商店来发布你的应用,
并为用户提供下载和安装。
除此之外,你还可以在社交媒体和在线论
坛等平台上宣传你的应用,以吸引更多的用户使用。
通过这个实战项目,你将学到许多有关Android应用开发的知识
和技能。
从搭建开发环境到实现计算功能,再到优化用户体验,这个
项目将带领你逐步掌握Android开发的各个方面。
希望你能从中获得丰富的经验和乐趣,并且能够在将来的开发工作中运用这些知识。
加油吧,Android开发者!。