实验二Activity及UIb.docx

上传人:b****3 文档编号:10353836 上传时间:2023-05-25 格式:DOCX 页数:15 大小:508.27KB
下载 相关 举报
实验二Activity及UIb.docx_第1页
第1页 / 共15页
实验二Activity及UIb.docx_第2页
第2页 / 共15页
实验二Activity及UIb.docx_第3页
第3页 / 共15页
实验二Activity及UIb.docx_第4页
第4页 / 共15页
实验二Activity及UIb.docx_第5页
第5页 / 共15页
实验二Activity及UIb.docx_第6页
第6页 / 共15页
实验二Activity及UIb.docx_第7页
第7页 / 共15页
实验二Activity及UIb.docx_第8页
第8页 / 共15页
实验二Activity及UIb.docx_第9页
第9页 / 共15页
实验二Activity及UIb.docx_第10页
第10页 / 共15页
实验二Activity及UIb.docx_第11页
第11页 / 共15页
实验二Activity及UIb.docx_第12页
第12页 / 共15页
实验二Activity及UIb.docx_第13页
第13页 / 共15页
实验二Activity及UIb.docx_第14页
第14页 / 共15页
实验二Activity及UIb.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验二Activity及UIb.docx

《实验二Activity及UIb.docx》由会员分享,可在线阅读,更多相关《实验二Activity及UIb.docx(15页珍藏版)》请在冰点文库上搜索。

实验二Activity及UIb.docx

实验二Activity及UIb

天津理工大学

计算机与通讯工程学院

 

实验报告

 

2012至2013学年第二学期

 

实验一AndroidActivity及UI设计

 

课程名称

软件主流开发平台与工具

学号

学生姓名

年级

专业

教学班号

实验地点

实验时间

主讲教师

辅导教师

 

实验

(一)

实验名称

Activity及UI

软件环境

Windows

Ecllipse&AndroidSDK

硬件环境

PC

实验目的:

完成Android应用程序建立及界面处理技术

完成Android应用程序建立及界面处理技术,包括以下内容:

●Android应用层程序建立

●界面布局及控件添加

●菜单添加——选项菜单及上下文菜单

●消息处理

●ArrayAdapter使用

实验内容(应包括实验题目、实验要求、实验任务等)

实验要求:

完成“toDoList”功能

参考步骤:

1.建立Android应用程序框架

2.添加控件——EditText负责输入“工作项目”;ListView负责显示“工作列表”

3.定义ArrayList,并通过ArrayAdapter将ListView与ArrayList相关联

4.添加选项菜单,(参见演示软件)

a.当EditText控件Visible时,有两个菜单项,“添加项目”和“取消添加”。

其中添加项目将EditText内容添加到ListView中,“取消项目”将EditText隐藏

b.当EditText控件隐藏时,有一个菜单项,及“添加菜单”项,当按下该菜单时,EditText由隐藏状态改为显示状态

5.为EditText添加按键事件(D-pad键)监听器,当按下该键时,EditText内容添加到“工作列表”中

6.为ListView添加上下文菜单,当长按ListView某一项时,跳出上下文菜单,提示用户可以删除该项目

附:

安装apk:

adbinstall路径/toDoList.apk

实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)

实现方法:

用Intent实现两个Activity(分别为LoginActivity和usrandpasswd)之间的切换和数据传递。

用onActivityResult()函数实现在父Activity中处理子Activity的返回值。

LoginActivity的布局在main.XML中;usrandpasswd的布局在passwd.XML中。

实验步骤:

首先在AndroidManifest.xml中注册这两个Activity,代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

package="com.gaofeifei.login"

android:

versionCode="1"

android:

versionName="1.0">

minSdkVersion="8"/>

android:

icon="@drawable/ic_launcher"

android:

label="@string/app_name">

android:

label="@string/app_name"

android:

name=".LoginActivity">

name="android.intent.action.MAIN"/>

name="android.intent.category.LAUNCHER"/>

android:

label="@string/app_name"

android:

name=".usrandpasswd"

>

其次在main.XML中对LoginActivity写布局,在passwd.XML中对usrandpasswd。

代码如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

orientation="vertical">

android:

id="@+id/showResult"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/hello"/>

android:

id="@+id/login"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="登陆"

/>

 

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

layout_width="fill_parent"android:

layout_height="fill_parent"android:

orientation="vertical">

android:

id="@+id/usrnameTextView"

android:

layout_width="90dip"

android:

layout_height="wrap_content"

android:

textSize="18dip"

android:

text="用户名:

"/>

android:

id="@+id/usrnameEditText"

android:

layout_width="120dip"

android:

layout_height="wrap_content"

android:

layout_below="@id/usrnameTextView"

android:

text=""/>

android:

id="@+id/passwdTextView"

android:

layout_width="90dip"

android:

layout_height="wrap_content"

android:

layout_below="@id/usrnameEditText"

android:

textSize="18dip"

android:

text="密码:

"/>

android:

id="@+id/passwdEditText"

android:

layout_width="120dip"

android:

layout_height="wrap_content"

android:

layout_below="@id/passwdTextView"

android:

text=""/>

android:

id="@+id/passwdButton"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_below="@id/passwdEditText"

android:

layout_marginTop="15dip"

android:

textSize="18dip"

android:

text="确认"/>

最后,实现主界面上有一个“登录”按钮,单击“登录”按钮后打开一个新的Activity;新的Activity上有“输入用户名”和“密码”控件,输入后单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中

classLoginActivity中:

finalintACTIVITY1=1;

privateTextViewshowResult;

showResult=(TextView)findViewById(R.id.showResult);

Buttonlogin=(Button)findViewById(R.id.login);

login.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

Intentintent=newIntent();

intent.setClass(LoginActivity.this,usrandpasswd.class);

startActivityForResult(intent,ACTIVITY1);

}

});

该方法为Button按钮设了一个监听器,Intent显示启动usrandpasswdActivity。

protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){

//TODOAuto-generatedmethodstub

super.onActivityResult(requestCode,resultCode,data);

switch(requestCode)

{

caseACTIVITY1:

if(resultCode==RESULT_OK)

showResult.setText(data.getData().toString());

break;

}

}

该方法实现新的Activity上的用户名和密码,在单击“确定”按钮,返回主界面,并将结果输出在主界面的TextView中。

classusrandpasswd中:

privateEditTextusrname;

privateEditTextpasswd;

usrname=(EditText)findViewById(R.id.usrnameEditText);

passwd=(EditText)findViewById(R.id.passwdEditText);

Buttonbutton=(Button)findViewById(R.id.passwdButton);

button.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

Stringmsg="用户名:

"+usrname.getText().toString()+"\n"+"密码:

"+passwd.getText();

Intentresult=newIntent(null,Uri.parse(msg));

setResult(RESULT_OK,result);

finish();

}

});

此方法是Intent隐式启动。

截图如下:

 

 

源代码:

packagecom.gaofeifei.login;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.TextView;

publicclassLoginActivityextendsActivity{

finalintACTIVITY1=1;

privateTextViewshowResult;

/**Calledwhentheactivityisfirstcreated.*/

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

showResult=(TextView)findViewById(R.id.showResult);

Buttonlogin=(Button)findViewById(R.id.login);

login.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

Intentintent=newIntent();

intent.setClass(LoginActivity.this,usrandpasswd.class);

startActivityForResult(intent,ACTIVITY1);

}

});

}

 

protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){

//TODOAuto-generatedmethodstub

super.onActivityResult(requestCode,resultCode,data);

switch(requestCode)

{

caseACTIVITY1:

if(resultCode==RESULT_OK)

showResult.setText(data.getData().toString());

break;

}

}

}

 

packagecom.gaofeifei.login;

importandroid.app.Activity;

importandroid.content.Intent;

import.Uri;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.*;

publicclassusrandpasswdextendsActivity{

privateEditTextusrname;

privateEditTextpasswd;

protectedvoidonCreate(BundlesavedInstanceState){

//TODOAuto-generatedmethodstub

super.onCreate(savedInstanceState);

setContentView(R.layout.passwd);

usrname=(EditText)findViewById(R.id.usrnameEditText);

passwd=(EditText)findViewById(R.id.passwdEditText);

Buttonbutton=(Button)findViewById(R.id.passwdButton);

button.setOnClickListener(newView.OnClickListener(){

publicvoidonClick(Viewv){

//TODOAuto-generatedmethodstub

Stringmsg="用户名:

"+usrname.getText().toString()+"\n"+"密码:

"+passwd.getText();

Intentresult=newIntent(null,Uri.parse(msg));

setResult(RESULT_OK,result);

finish();

}

});

}

}

实验感受:

 

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

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2