版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、淮海工学院计算机工程学院实验报告书课程名: 手持设备软件开发 题 目: 实验2:用户界面程序设计 班 级: 学 号: 姓 名: 评语:成绩: 指导教师: 批阅时间: 年 月 日 手持设备软件开发实验报告 - 15 -一、实验目的与要求掌握Android用户界面程序设计的一般方法;掌握Android提供的各种View和ViewGroup的基本用法;掌握用户界面组件事件处理的一般方法。二、实验内容1. 在Eclipse中导入并运行ActivityLifeCycle示例程序,通过Logcat View观察Activity生命周期回调方法调用的时机和次序,并进行简要的分析。a) 启动ActivityL
2、ifeCycle,按返回按键b) 启动ActivityLifeCycle,按Home按键,然后再使用Recents按键返回c) 启动ActivityLifeCycle,按下结束程序按钮d) 启动ActivityLifeCycle,将模拟器的方向改为横向显示(按Ctrl+F12组合键)2.编写简单的计算器程序,界面如右面截图所示。输入运算数据或选择操作符后自动进行计算,结果显示42对应的位置。要求竖屏方向的布局使用LinearyLayout实现,横屏方向的布局使用RelativeLayout或者GridLayout实现。3. (选做)编写程序99 Bottles of Beer(项目名字为nnb
3、ob),显示99 Bottles of Beer的歌词。单击Sing按钮显示对应的歌词,单击Start Over按钮重新开始。要求:屏幕旋转之后程序能够进行正常显示后续歌词;横屏和竖屏采用不同的布局显示;歌词显示时超出屏幕的部分可以自动滚动显示。完整的歌词可以参考网址:http:/www.99-bottles-of- 3、 实验步骤与实验结果1, (1)启动ActivityLifeCycle,按返回按键(2) 启动ActivityLifeCycle,按Home按键,然后再使用Recents按键返回(3) 启动ActivityLifeCycle,按下结束程序按钮(4)启动ActivityLife
4、Cycle,将模拟器的方向改为横向显示(按Ctrl+F12组合键)2,源代码MainActivity.javapackage com.example.calculator1;import android.os.Bundle;import android.app.Activity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;public c
5、lass MainActivity extends Activity private int n=0;private RadioButton rb1=null;private RadioButton rb2=null;private RadioButton rb3=null;private RadioButton rb4=null;private EditText et1=null;private EditText et2=null;private EditText et3=null;private Button bt1=null;private RadioGroup rg=null;Over
6、rideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);if(this.getResources().getConfiguration().orientation=Configuration.ORIENTATION_LANDSCAPE) setContentView(R.layout.activity_two); / 横屏 else if(this.getResources().getConfiguration().orientation=Configuration.OR
7、IENTATION_PORTRAIT) / 竖屏 setContentView(R.layout.activity_main); et1=(EditText)findViewById(R.id.textView1);et2=(EditText)findViewById(R.id.textView2);et3=(EditText)findViewById(R.id.textView3);rb1=(RadioButton)findViewById(R.id.RadioButton1);rb2=(RadioButton)findViewById(R.id.RadioButton2);rb3=(Rad
8、ioButton)findViewById(R.id.RadioButton3);rb4=(RadioButton)findViewById(R.id.RadioButton4);rg=(RadioGroup)findViewById(R.id.RadioGroup1);bt1=(Button)findViewById(R.id.bt1);bt1.setOnClickListener(new View1();rg.setOnCheckedChangeListener(new View2();class View1 implements View.OnClickListenerOverridep
9、ublic void onClick(View arg0) / TODO Auto-generated method stubdouble x1=Double.parseDouble(et1.getText().toString().trim();double x2=Double.parseDouble(et2.getText().toString().trim();double x3=0;if(n=1)x3=x1+x2;else if(n=2)x3=x1-x2;else if(n=3)x3=x1*x2;else if(n=4)x3=x1/x2;elsex3=0;et3.setText(&qu
10、ot;"+x3);class View2 implements RadioGroup.OnCheckedChangeListenerOverridepublic void onCheckedChanged(RadioGroup group, int checkid) / TODO Auto-generated method stubif(checkid=rb1.getId()n=1;else if(checkid=rb2.getId()n=2;else if(checkid=rb3.getId()n=3;elsen=4;activity_main.xml(竖屏)<LinearL
11、ayout xmlns: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="dime
12、n/activity_horizontal_margin" android:paddingTop="dimen/activity_vertical_margin" android:orientation="vertical"tools:context=".MainActivity" ><TextView android:id="+id/textView" android:layout_width="match_parent" android:layout_height=&q
13、uot;wrap_content" android:text="竖屏" /> <EditText android:id="+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RadioGroup android:id="+id/RadioGroup1" android:layout_width="wrap_content&
14、quot; android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="+id/RadioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+"/> <RadioButton and
15、roid:id="+id/RadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-"/> <RadioButton android:id="+id/RadioButton3" android:layout_width="wrap_content" android:layout_height="wrap_con
16、tent" android:text="*"/> <RadioButton android:id="+id/RadioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/"/> </RadioGroup> <EditText android:id="+id/textView2" androi
17、d:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="计算" android:te
18、xtSize="20sp"/> <EditText android:id="+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>activity_two.xml(横屏)<?xml version="1.0" encoding="utf-8"?><RelativeLayout
19、 xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content&quo
20、t; android:text="横屏" /> <EditText android:id="+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="id/textView" /> <RadioGroup android:id="+id/RadioGroup1" android:layout_
21、width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="id/textView1" > <RadioButton android:id="+id/RadioButton1" android:layout_width="wrap_content" android:layout_height=&quo
22、t;wrap_content" android:text="+" android:layout_alignParentLeft="true" /> <RadioButton android:id="+id/RadioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:layout_toRight
23、Of="id/RadioButton1"/> <RadioButton android:id="+id/RadioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:layout_toRightOf="id/RadioButton2"/> <RadioButton android:id=&qu
24、ot;+id/RadioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:layout_toRightOf="id/RadioButton3"/> </RadioGroup> <EditText android:id="+id/textView2" android:layout_width="
25、;match_parent" android:layout_height="wrap_content" android:layout_below="id/RadioGroup1"/> <Button android:id="+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:te
26、xt="计算" android:textSize="20sp" android:layout_below="id/textView2"/> <EditText android:id="+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="id/bt1"/> </Rela
27、tiveLayout>配置文件在 AndroidManifest.xml文件中的 主Activity中加入 android:configChanges="orientation|keyboardHidden"四、思考题1、如何为Activity在横屏和竖屏方向创建不同的用户界面布局?答:可以通过java代码来判断当前是横屏还是竖屏然后来加载相应的xml布局文件。因为当屏幕变为横屏的时候,系统会重新呼叫当前Activity的onCreate方法,你可以把以下方法放在你的onCreate中来检查当前的方向,然后可以让你的setContentView来载入不同的layout
28、 xml。if(this.getResources().getConfiguration().orientation=Configuration.ORIENTATION_LANDSCAPE) Log.i("info", "landscape"); / 横屏 else if(this.getResources().getConfiguration().orientation=Configuration.ORIENTATION_PORTRAIT)
29、60; Log.i("info", "portrait"); / 竖屏 然后把layout中的xml文件分别考到以上目录,修改布局就可以了代码中不做任何更改。 在 AndroidManifest.xml文件中的 主Activity中加入 android:configChanges="orientation|keyboardHidden"Android应用程序中使用资源类型有哪些?如何在Java程序中使用资源,如何在XML资源文件中引用资源。Android 资源类型1
30、.字符串资源>>1.普通字符串>>2.字符串数组获取方式:getResources().getStringArray(R.array.planets_array)>>3.复数字符串资源某些自然语言中,不同的数字在使用方法上会有所不同,比如one book,two books。当数量大于1时,会使用不同的名词或其它复数形式;/ 引用数字为1的复数字符串getResources().getQuantityString(R.pluarlas.numberOfp,1);/ 引用数字为其它值的复数字符串getResources().getQuantityString(
31、R.pluarlas.numberOfp,10,10);>>4.占位符格式化字符串2、Layout资源1、如果根节点是View,除了<requestFocus>标签外,不能添加任何子标签,<requestFocus>可能被添加到布局文件的任何View中,表示该标签对应的控件在显示时处于焦点状态,整个布局文件只能有一个<requestFocus>标签2、根节点是ViewGroup,常用的布局都是ViewGroup的子类3、重用布局文件如果想重用某个布局文件,可以使用<include>标签<include layout="
32、layout/xx_layout" />如果想让一个布局文件被另一个布局文件引用(使用<include>标签),可以使用<merge>作为被引用布局文件的根节点,由于<merge>并不会生成任何标签(在大量引用布局文件时不至于生成大量无用的标签),但是xml文件必须要有一个根节点,因此<merge>所起的作用就是作为xml文件的根节点,以使xml文件在编译时不至于出错,可以把<merge>当成<FrameLayout>使用;3.图像资源在图像资源中可以存储图像文件,还可以使用xml格式的图像资源来控件图像的
33、状态和行为;>>1.普通图像资源Drawable da = getResources().getDrawable(R.drawable.xxx);>>2.xml图像资源xml图像资源其实就是在drawable目录中指定的xml文件,此种方式可以额外指定图像的某些属性,如图像拉动、排列方式;<bitmap xmlns:android="android:src="drawable/ic_launcher"android:tileMode="repeat" ></bitmap>>>3.Nin
34、e-Patch图像资源Nine-Patch图像资源文件必须以9.png作为文件扩展名,如abc.9.png该图像资源的主要作用是:防止图像的某一部分被拉伸;确定将图像作为背景图的控件中内容显示的位置;Android SDK本身提供了一个Draw 9-patch的工具,启动<sdk目录>toolsdraw9patch.bat命令启动该工具;可以通过此工具在png图的四周绘制1个像素粗的直线,上边缘和左边缘的直线分别表示图像在水平和垂直方向可位值的范围。如果水平或垂直方向的某个区域不需要拉伸,则可不绘制相应的直线;右边缘和下边缘的直线分别表示图像所在控件中内容的显示范围,内容只在右边缘
35、和下边缘绘制直线的区域显示,表示内容显示范围和拉伸范围的两给直线有一个重要区别就是表示内容显示范围的直线中间不能断开,而表示拉伸范围的直线中间可以断开;Nine-Patch图像资源与普通图像资源引用方法相同,在引用时只写文件名,活力.9.png;>>4.XML Nine-Patch图像资源Nine-Patch图像资源也有与其对应的xml图像资源,使用<nine-patch>标签来引用Nine-Patch格式的图像,有一个设置抖动的android:dither属性;>>5.图层资源图层资源类似于<FrameLayout>不同的是<FrameL
36、ayout>标签中可以包含任意的控件,而图层资源每一层都只有是图像,定义图层资源必须使用<layer-list>作为资源文件的根节点,<layer-list>标签中包含多个<item>标签,每一个标签表示一个图像,最后一个<item>标签显示在最顶层;默认情况下,图像会尽量充满显示图像的范围,图像可能会有拉伸,为了避免图像拉伸,可以在<item>标签中使用<bitmap>标签引用图像;复制代码某些情况下,可以使用图层来代替<FrameLayout>>>6.图像状态资源,处理控件不同状态下的显示
37、状态>>7.图像级别(Level)资源 图像资源状态只能指定几种有限的状态,可以通过图像级别指定更多的状态;图像级别是一个整数的区间,可以通过ImageView.setImageLevel或Drawable.setLevel方法切换不同状态的图像;图像级别资源是xml文件,必须以<level-list>为根节点,每一个item表示一个级别区间,下面是一个xml文件;通过ImageView.setImageLevel(level),根据level所在的区间设定显示的图像资源,如果level不在任一区间内则清空ImageView当前图像;<level-list xml
38、ns:android="<item android:maxLevel="2" android:minLevel="0" android:drawable="drawable/hell" /><item android:maxLevel="4" android:minLevel="3" android:drawable="drawable/hell" /></level-list>>>8.淡入淡出(Cross-fade)
39、资源也是切换两个图像(不支持多于两个图像的切换),并且使这两个图像以淡入淡出效果进行切换,如电灯在开关时逐渐变亮或逐渐变暗;<transition xmlns:android=" ><item android:drawable="drawable/hell"/><item android:drawable="drawable/hell"/></transition>>>9.嵌入(insert)图像资源使用场景:要显示的图像要求要小于装载图像的View(图小于View区域),也是通过xm
40、l资源定义,只有一个节点inset。<inset xmlns:android="android:drawable="drawable/hell"android:insetLeft="10dip" > <!-图像距离左边的距离,延伸->上/下/右的距离-></inset>>>10.剪切(Clip)图像资源,使用剪切图像资源可以只显示图像的一部分,如可以通过此来制作进度条;<clip xmlns:android="android:clipOrientation="hor
41、izontal" / 指定截取的方向android:drawable="drawable/hell" / 指定要截取的图像android:gravity="left" > / 指定截取的方式,在此为从左侧开始截取</clip>ClipDrawable cd = .;cd.setLevel(1000);上面ClipDrawable.setLevel(level)设置截取的图像宽度,ClipDrawable预设了最大值10000(表示不进行截取),最小值为0(表示不显示);>>11. 比例(Scale)图像资源<
42、scale xmlns:android="android:drawable="drawable/hell"android:scaleGravity="center" / 设置图像显示的位置android:scaleHeight="70%" / 设置图像显示的高度android:scaleWidth="80%" > / 设置图像显示的宽度</scale>>>12.形状资源13.菜单资源菜单不仅可以在onCreateContextMenu或onCreateOptionsMenu方
43、法中通过代码创建,还可以在res/menu目录中建立相应的菜单资源文件,并在上面两个方法中加载菜单资源;菜单资源文件必须以<menu>标签作为根节点,每一个菜单项用一个<item>表示,如果要定义子菜单,可以在<item>标签中包含<menu>标签;如果想将多个菜单项划为一组,可以使用<group>包含多个<item>标签;14.样式与主题(style/theme)>>1.样式styleandroid中样式和css中样式作用是一样的,都是用于为界面元素定义显示风格,它是一个包含一个或者多个控件属性的集合。定义样式需要在res/values/sty
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 武汉铁路桥梁职业学院《外国文学史Ⅱ》2024-2025学年第一学期期末试卷
- 云南师范大学《物流配送》2024-2025学年第一学期期末试卷
- 防灾科技学院《动物检疫检验学》2024-2025学年第一学期期末试卷
- 湖南省东安县天成实验学校2025年高一上生物期末经典模拟试题含解析
- 2023年绍兴辅警协警招聘考试真题附答案详解(轻巧夺冠)
- 2025年浙江省绍兴市重点中学生物高一第一学期期末检测模拟试题含解析
- 浙江工贸职业技术学院《遥感数字图像处理Ⅱ》2024-2025学年第一学期期末试卷
- 吉林省北大附属长春实验学校2026届高二化学第一学期期末综合测试试题含解析
- 天津机电职业技术学院《英语写作记叙文》2024-2025学年第一学期期末试卷
- 石河子工程职业技术学院《模拟电子技术基础课程设计》2024-2025学年第一学期期末试卷
- 贵州省贵阳市、六盘水市、黔南州2026届化学高三第一学期期中监测试题含解析
- 化验室安全培训
- 2025内蒙古巴彦淖尔市五原县招聘社区工作者50人考试笔试参考题库附答案解析
- 2025新疆医科大学第四附属医院(新疆维吾尔自治区中医医院)招聘事业单位人员10人笔试考试参考题库及答案解析
- 2025年山东省教育厅直属事业单位省教育发展服务中心第二批公开招聘人员(9名)笔试考试参考题库及答案解析
- 前端开发部门介绍
- 2025广东惠州市惠城区人民政府江南街道办事处招聘社区“两委”班子储备人选4人笔试考试参考题库及答案解析
- 华为ICT大赛2025-2026中国区(云赛道)高分备考试题库500题(含答案解析)
- ICU气管切开护理技术及并发症预防
- 元代服饰的讲解
- 粮食入仓安全培训课件
评论
0/150
提交评论