基于安卓的计算器的设计与实现.docx

上传人:b****9 文档编号:25439519 上传时间:2023-06-08 格式:DOCX 页数:28 大小:144.81KB
下载 相关 举报
基于安卓的计算器的设计与实现.docx_第1页
第1页 / 共28页
基于安卓的计算器的设计与实现.docx_第2页
第2页 / 共28页
基于安卓的计算器的设计与实现.docx_第3页
第3页 / 共28页
基于安卓的计算器的设计与实现.docx_第4页
第4页 / 共28页
基于安卓的计算器的设计与实现.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

基于安卓的计算器的设计与实现.docx

《基于安卓的计算器的设计与实现.docx》由会员分享,可在线阅读,更多相关《基于安卓的计算器的设计与实现.docx(28页珍藏版)》请在冰豆网上搜索。

基于安卓的计算器的设计与实现.docx

基于安卓的计算器的设计与实现

 

安卓应用程序设计

——简易计算器的实现

 

院(系)名称

专业 名称

学生 姓名

学生学号

课程名称

 

2016年6月日

1.系统需求分析

Android是以Linux为核心的手机操作平台,作为一款开放式的操作系统,随着Android的快速发展,如今已允许开发者使用多种编程语言来开发Android应用程序,而不再是以前只能使用Java开发Android应用程序的单一局面,因而受到众多开发者的欢迎,成为真正意义上的开放式操作系统。

计算器通过算法实行简单的数学计算从而提高了数学计算的效率,实现计算器的界面优化,使界面更加友好,操作更加方便。

基于android的计算器的设计,系统具有良好的界面;必要的交互信息;简约美观的效果。

使用人员能快捷简单地进行操作,即可单机按钮进行操作,即时准确地获得需要的计算的结果,充分降低了数字计算的难度和节约了时间。

2.系统概要设计

2.1计算器功能概要设计

根据需求,符合用户的实际要求,系统应实现以下功能:

计算器界面友好,方便使用,,具有基本的加、减、乘、除功能,能够判断用户输入运算数是否正确,支持小数运算,具有清除功能。

图2.1系统功能图

整个程序基于Android技术开发,除总体模块外主要分为输入模块、显示模块以及计算模块这三大部分。

在整个系统中总体模块控制系统的生命周期,输入模块部分负责读取用户输入的数据,显示模块部分负责显示用户之前输入的数据以及显示最终的计算结果,计算模块部分负责进行数据的运算以及一些其他的功能。

具体的说,总体模块的作用主要是生成应用程序的主类,控制应用程序的生命周期。

输入模块主要描述了计算器键盘以及键盘的监听即主要负责读取用户的键盘输入以及响应触屏的按键,需要监听手机动作以及用指针事件处理方法处理触屏的单击动作。

同时提供了较为直观的键盘图形用户界面。

 

显示模块描述了计算器的显示区,即该区域用于显示用户输入的数据以及最终的计算结果,同时负责显示一些其他的信息。

 

计算模块则主要描述了计算器的整体,实现了计算器的界面,负责计算用户输入数据,包括加、减、乘、除等各种功能,记忆数据的相关功能,清零的功能。

 

图2.2三大模块

2.2 输入模块设计

系统如果想完成计算器中各种功能,首先用户要能进行数据输入,由于是在触屏手机上开发计算器程序,所以要求输入可以直接使用触屏进行,所以在设计的时候就要充分的考虑这一点。

正是由于考虑到这个特殊的地方,所以在进行模块设计中,选择编写输入模块类的时候会特意选取使用可以支持触屏输入的特殊增强型图形用户界面类。

 

输入模块主要的任务是描述计算器键盘以及实现键盘的监听,即当用户点击按键或者屏幕的时候监听会去调用相应的处理办法,本模块还需要为系统提供一个较为直观的键盘图形用户界面。

输入模块的功能图如图

 

图2.3输入模块

2.3显示模块设计

作为手机计算器系统,显示部分也是必不可少的一部分。

没有显示部分就没有办法显示用户输入的数字是否正确,甚至不能显示计算出的结果,由此可见显示模块即包括输入的部分(因个人技术原因不能显示表达式的形式)也包括输出的部分。

 

显示模块主要完成的任务是描述计算器的显示区,该区域用于显示用户输入的数据以及最终的计算结果和一些其他信息。

同时本模块还将提供调用和设置显示的具体方法

 

 

显示数据设置显示

图2.4显示模块

2.4计算模块设计

系统要完成整个的计算器的功能,那么计算模块就是整个系统的重要模块。

没有计算模块系统就不能顺利的完成计算,就无法达到用户的要求。

所以计算模块的设计也是本次系统设计的重点。

 

系统想要能在屏幕上显示出计算器的样子要有整体的布局。

在本模块中要完成布局的具体设计,使用户能够看到整个计算器的界面。

同时系统想完成 计算功能必须要处理用户的输入,当输入模块的监听传到计算模块中时,计算模块就要根据相应的方法进行进一步的处理。

 

图2.5计算模块

 

3.设计与实现部分

3.1应用程序构成

 Android应用程序一般是由Activity、Serviece、Content Provider、Intent、Intent Receiver、Notification六个组件构成。

但并非每一个Android应用程序都包含这六个组件。

 

(1)Activity(活动):

应用程序的表示层。

Activity是Android的基本组件。

每一个activity代表一个单独的屏幕。

每一个activity都是继承于Activity的独立类。

这个activity类将会显示出几个Views控件组成的用户接口,并对事件作出响应。

Android使用了Intent,实现在屏幕与屏幕之间跳转。

Activity跳转时只需要调用startActivity(Intent myIntent)方法。

然后,系统会在所有安装的应用程序中定义的IntentFilter中查找,找到最匹配myIntent的Intent对应的activity。

新的activity接收到myIntent的通知后,开始启动运行。

当startActivity方法被调用将触发解析myIntent的动作,这个机制提供了两个关键的好处:

能够重复利用从其他组件中以Intent的形式产生的一个请求和可以在任何时候被一个具有相同IntentFilter的新的startActivity取代。

(2)Service(服务):

Android应用程序的后台运行组件。

Service没有用户界面的程序。

服务组件在后台运行,它们可以更新数据源和可见的Activity,并触发Notification。

它们被用来执行一些需要在后台持续的常规处理。

例如媒体播放器这个activity会使用Context. startService()方法,连接到一个service上(如果这个service还没有运行将启动它)。

当连接到一个service之后,我们还可以通过service提供的接口与它进行通讯。

(3)Content Provider(内容提供器):

用来管理和分享应用程序数据库。

Content Provider是在应用程序之间共享数据的首选方法。

通过配置Content Provider允许其他应用程序访问存储的数据,也可以使用其他应用程序提供的Content Provider来访问它们存储的数据。

Android平台也提供了系统自带的Content Provider,如联系人信息等。

(4)Intent(意向):

Activity之间的粘合剂。

使用Intent,可以在系统范围内向目标活动或者服务广播消息,说明希望执行某个动作的意图。

之后系统就会确定那些最适合执行动作的目标。

(5)Notification(通知):

一种用户通知框架。

通过允许向用户发送信号,而不会过分吸引他们的注意力或者打断他们当前的活动。

它们是服务或者广播接收器吸引用户注意的首选方法。

例如,当设备接收到一个文本消息或者来电的时候,它可以通过闪灯、发出声音、显示图标或则会显示对话框信息的方式提示用户。

(6)Intent Receiver(意向接收器):

通过Intent Receiver实现应用能够对一个外部的事件作出响应,Intent Receiver在AndroidManifest.xml中注册,但也可以在代码中使用Context.registerReceiver()进行注册。

当一个intent receive被触发时,应用不必对请求调用intent receive,系统会在需要的时候启动你的应用。

各种应用还可以通过使用Context.broadcastIntent()将它们自己的intent receive广播给其他应用程序。

3.2功能的具体实现

(1)计算器布局设计:

分别实用Button按钮操作17个按钮,分别是+、—、*、/、=、0-9、清除按钮,以及EditTest的文本域,并通过代码布局调整布局,使布局更加清晰,简洁。

布局方式如图所示:

代码实现如下:

android="

xmlns:

tools="

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

paddingBottom="@dimen/activity_vertical_margin"

android:

paddingLeft="@dimen/activity_horizontal_margin"

android:

paddingRight="@dimen/activity_horizontal_margin"

android:

paddingTop="@dimen/activity_vertical_margin"

tools:

context=".MainActivity">

android:

id="@+id/editText1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignParentLeft="true"

android:

layout_alignParentRight="true"

android:

layout_alignParentTop="true"

android:

layout_marginTop="17dp"

android:

ems="10"/>

android:

id="@+id/button1"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignLeft="@+id/editText1"

android:

layout_below="@+id/editText1"

android:

layout_marginTop="30dp"

android:

text="1"/>

android:

id="@+id/button2"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button1"

android:

layout_alignBottom="@+id/button1"

android:

layout_toRightOf="@+id/button1"

android:

text="2"/>

android:

id="@+id/button3"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button2"

android:

layout_alignBottom="@+id/button2"

android:

layout_toRightOf="@+id/button2"

android:

text="3"/>

android:

id="@+id/button4"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignParentLeft="true"

android:

layout_below="@+id/button1"

android:

text="4"/>

android:

id="@+id/button5"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button4"

android:

layout_alignBottom="@+id/button4"

android:

layout_alignLeft="@+id/button2"

android:

text="5"/>

android:

id="@+id/button6"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@+id/button3"

android:

layout_toRightOf="@+id/button5"

android:

text="6"/>

android:

id="@+id/button7"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@+id/button4"

android:

layout_toLeftOf="@+id/button5"

android:

text="7"/>

android:

id="@+id/button8"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button7"

android:

layout_alignBottom="@+id/button7"

android:

layout_alignLeft="@+id/button5"

android:

text="8"/>

android:

id="@+id/button9"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button8"

android:

layout_alignBottom="@+id/button8"

android:

layout_toRightOf="@+id/button5"

android:

text="9"/>

android:

id="@+id/button10"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignLeft="@+id/button7"

android:

layout_below="@+id/button7"

android:

text="0"/>

android:

id="@+id/button11"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@+id/button7"

android:

layout_toRightOf="@+id/button7"

android:

text="."/>

android:

id="@+id/button12"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button11"

android:

layout_alignBottom="@+id/button11"

android:

layout_alignLeft="@+id/button9"

android:

text="="/>

android:

id="@+id/button13"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBottom="@+id/button3"

android:

layout_toRightOf="@+id/button3"

android:

text="+"/>

android:

id="@+id/button14"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignBaseline="@+id/button6"

android:

layout_alignBottom="@+id/button6"

android:

layout_toRightOf="@+id/button6"

android:

text="-"/>

android:

id="@+id/button15"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_above="@+id/button12"

android:

layout_alignLeft="@+id/button14"

android:

text="*"/>

android:

id="@+id/button16"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@+id/button9"

android:

layout_toRightOf="@+id/button9"

android:

text="/"/>

android:

id="@+id/button17"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_alignRight="@+id/button16"

android:

layout_below="@+id/button11"

android:

text="清除"/>

(2)计算功能实现

加法实现:

从editTest中获取数字赋值于num1,等待数字num2的输入。

代码如下:

caseR.id.button13:

Stringmystringadd=edit.getText().toString();

if(mystringadd.equals(null))

{

return;

}

num1=Double.valueOf(mystringadd);

edit.setText(null);

op=1;

isClickEqu=false;

break;

减法实现、乘法实现、除法实现和加法实现大体相同,但是op的赋值不同分别是2、3、4。

然后进行等号的计算实现,在同时记录下文本中的num2的数值,然后设置switch方法,case中op的值分别为1、2、3、4代表的分别加、减、乘、除。

然后根据case选项进行相应的加、减、乘、除运算,代码实现如下所示:

caseR.id.button12:

//等号,算法实现

StringmyStringeq=edit.getText().toString();

if(myStringeq.equals(null)){

return;

}

num2=Double.valueOf(myStringeq);

edit.setText(null);

switch(op){

case0:

Result=num2;

break;

case1:

Result=num1+num2;

break;

case2:

Result=num1-num2;

break;

case3:

Result=num1*num2;

break;

case4:

Result=num1/num2;

break;

}

edit.setText(String.valueOf(Result));

isClickEqu=true;

break;

}

4.运行界面截图

运行结果界面:

除法:

20除以4的结果

乘法3.89乘以1.2的结果

减法3.6减去0.6

除数为零:

点击清除之后的效果

5.核心代码

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.Toast;

publicclassMainActivityextendsActivityimplementsOnClickListener{

Buttonbutton1,button2,button3,button4,button5,button6,button7,button8;

Buttonbutton9,button10,button11,button12,button13,button14,button15,button16,button17;

EditTextedit=null;

doublenum1=0,num2=0,Result=0;

booleanisClickEqu=false;

intop=0;

@Override

protectedvoidonCreate(BundlesavedInstance

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > IT认证

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1