Android课程设计报告河北科技大学.docx
- 文档编号:7584185
- 上传时间:2023-05-11
- 格式:DOCX
- 页数:14
- 大小:20.71KB
Android课程设计报告河北科技大学.docx
《Android课程设计报告河北科技大学.docx》由会员分享,可在线阅读,更多相关《Android课程设计报告河北科技大学.docx(14页珍藏版)》请在冰点文库上搜索。
Android课程设计报告河北科技大学
河北科技大学
课程设计报告
学生姓名:
学号:
专业班级:
计算机班
课程名称:
Android智能手机软件开发
学年学期:
2011学年第一学期
指导教师:
高凯
2011年12月
课程设计成绩评定表
学生姓名
学号
成绩
专业班级
起止时间
2011.09-2012.01
设计题目
设计编写一个简单的计算器
指
导
教
师
评
语
指导教师:
年月日
一、概述
设计编写一个能在Android系统下运行的计算器,除了能实现四则运算外,还带有菜单的功能。
菜单中拥有的Item有:
1退出键——按下时弹出一个对话框提示是否确认退出;2帮助键——按下后显示帮助信息,并能从帮助界面返回到主界面;3About键——按下后弹出就算器的版本信息;4换肤键——按下后更换皮肤。
二、项目设计与实现
先写好布局文件,代码如下:
xmlversion="1.0"encoding="utf-8"?
>
android:
orientation="vertical"
android:
layout_width="fill_parent"
android:
layout_height="fill_parent"
>
orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="5" > layout_width="fill_parent" android: layout_height="fill_parent" android: id="@+id/editText1" android: layout_weight="2" android: background="@android: color/darker_gray" > layout_width="fill_parent" android: layout_height="fill_parent" android: id="@+id/editText2" android: singleLine="true" android: layout_weight="1" android: background="@android: color/darker_gray" android: gravity="right" > orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="1"> orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="12"> src="@drawable/clean" android: id="@+id/buttonclean" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1" /> src="@drawable/delete" android: id="@+id/deletebutton" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/left" android: id="@+id/buttonl" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/right" android: id="@+id/buttonr" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="11"> src="@drawable/nine" android: id="@+id/button9" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/eight" android: id="@+id/button8" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/seven" android: id="@+id/button7" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/cheng" android: id="@+id/buttoncheng" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="11"> src="@drawable/six" android: id="@+id/button6" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/five" android: id="@+id/button5" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/four" android: id="@+id/button4" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/chu" android: id="@+id/buttonchu" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="11"> src="@drawable/three" android: id="@+id/button3" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/two" android: id="@+id/button2" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/one" android: id="@+id/button1" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/jia" android: id="@+id/buttonjia" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> orientation="horizontal" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_weight="11"> src="@drawable/zero" android: id="@+id/button0" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/dian" android: id="@+id/buttondian" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/deng" android: id="@+id/buttondeng" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> src="@drawable/jian" android: id="@+id/buttonjian" android: layout_height="fill_parent" android: layout_width="fill_parent" android: layout_weight="1"/> 说明: 此Activity采用嵌套式LinearLayout布局,最外层的LinearLayout嵌入六个LinearLayout,此时LinearLayout的android: orientation="vertical"即采用垂直布局。 嵌入的六个LinearLayout除第一个采用垂直布局外其他的均采用水平布局。 第一个LinearLayout控制两个EditText的位置,剩下的五个LinearLayout控制20个按钮的布局。 为使软件能适应不同分辨率的手机,所有控件的layout_width和layout_height属性都设为fill_parent,而控制按钮大小通过设置layout_weight的大小来控制。 这样控件的大小就只和屏幕大小和控件占屏幕的比例有关。 显示效果如下: Activity的界面 计算器的基本功能实现 接下来就是对各个按钮绑定监听器,实现算术式的输入功能和计算输入的算术式值的功能。 根据各个按钮的功能可将将它们分为四类: Clean按钮为一类,Delete为一类,等号按钮为一类,其他的作为基本算式输入按钮可看做一类。 由此写四个监听器类,分别实现按钮的各个功能。 Clean功能是清空两个Edittext中的内容,实现原理也就是Clean按钮绑定的监听器,代码如下: //清除功能 classclearListenerimplementsOnClickListener { publicvoidonClick(Viewv) { //TODOAuto-generatedmethodstub edittext1.setText(null); edittext2.setText(null); tem=""; tem1=""; } } Delete功能相关代码是为: //删除功能,删除之后光标不移动 classdeleteListenerimplementsOnClickListener { publicvoidonClick(Viewv) { //TODOAuto-generatedmethodstub intindex=edittext1.getSelectionStart();//获得光标位置 intl=tem.length(); if(l==0||index==0) { edittext1.setText(tem); edittext1.setSelection(0); } elseif(l==1) { tem=""; edittext1.setText(tem); } else { Stringtem2=""; tem2=tem.substring(index,l); tem=tem.substring(0,index-1); edittext1.setText(tem+tem2); edittext1.setSelection(tem.length()); tem=tem+tem2; //edittext1.setSelection(index-1); } } } 点击等号按钮后,能计算输入算式的值并将结果显示在第二个文本框中,实现代码如下: //等号功能 classdengListenerimplementsOnClickListener { publicvoidonClick(Viewarg0){ //TODOAuto-generatedmethodstub Stringstr=edittext1.getText().toString(); ExpressionParserep=newExpressionParser(str); try{ doubleresult=ep.parse(); Stringresult_str=String.valueOf(result); edittext2.setText(result_str); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); edittext2.setText("ERROR"); } } } 其中ExpressionParser为一个封装类,其实现的功能为计算出用字符串表示的素数表达式的值。 计算用字符串表示的计算式的基本原理为: 1.创建两个堆栈,一个用来放数据(numStack),一个用来放算术符(chStack); 2.从左到右读入算术式,如果读到的是数字,则压入(push)到数据堆栈(numStack)中。 若读到的是算术符,先判断chStack栈顶元素,若栈顶元素优先级大于读到的算术符,则先将栈顶元素和numStack中两个数拿出来计算,再将读到的算术符压入chStack中,若读到的算术符优先级大于栈顶元素,则将读到的算术符压入chStack中。 如果读到的是右括号则不断将chStack和numStack中的内容拿出来计算,直到从chStack中拿出左括号为止; 3.若读到了算术式的最后,则将两堆栈中的内容全拿出来计算,最后结果放在numStack中。 这里的左括号的优先级最低,其次为加号和减号,最高为乘号和除号。 基本算式输入按钮实现的功能不仅要能输入基本的数字和算术符,还要有简单的纠错功能,如不能连续输入两个小数点、右括号数必须和左括号数对应、不能连续输入两个加减乘除等。 Menu设计 使用Menu菜单实现退出、查看帮助文档、更换皮肤和查看版本信息的功能,如图6.12所示。 图6.12菜单界面 菜单可以采用xml实现,先在res文件夹中新建AndroidXMLFile,选择Menu类型,如图6.13所示: 图6.13新建XML文件 之后,此XML文件保存在layout文件夹下面的Menu文件夹下。 然后在menu.xml文件中添加需要添加的item,这里添加“Exit”、“Help”、“Skin”、“About”四个Item,相关代码如下: xmlversion="1.0"encoding="utf-8"? > id="@+id/group1"> id="@+id/exit" android: title="Exit" android: icon="@drawable/exit3"/> id="@+id/about" android: title="About" android: icon="@drawable/about3"/> id="@+id/help" android: tit
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 课程设计 报告 河北 科技大学