实验一Android开发环境搭建与Helloworld.docx

上传人:b****2 文档编号:17336908 上传时间:2023-07-24 格式:DOCX 页数:18 大小:1.08MB
下载 相关 举报
实验一Android开发环境搭建与Helloworld.docx_第1页
第1页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第2页
第2页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第3页
第3页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第4页
第4页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第5页
第5页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第6页
第6页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第7页
第7页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第8页
第8页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第9页
第9页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第10页
第10页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第11页
第11页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第12页
第12页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第13页
第13页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第14页
第14页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第15页
第15页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第16页
第16页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第17页
第17页 / 共18页
实验一Android开发环境搭建与Helloworld.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验一Android开发环境搭建与Helloworld.docx

《实验一Android开发环境搭建与Helloworld.docx》由会员分享,可在线阅读,更多相关《实验一Android开发环境搭建与Helloworld.docx(18页珍藏版)》请在冰点文库上搜索。

实验一Android开发环境搭建与Helloworld.docx

实验一Android开发环境搭建与Helloworld

实验一Android开发环境搭建与Helloworld

一、实验目的

学会搭建并安装Android开发环境,并实现HelloWorld应用开发。

2、主要仪器设备及套数

计算机

三、实验容

搭建开发环境;安装JavaJDK,下载Eclipse,解压Eclipse;官方下安装ADT(AndroidDevelopmentTools);安装Android;安装手机USB驱动;建立新项目,实现HelloWorld

4、程序代码

package.example.helloandroid;

importandroid.support.v7.app.ActionBarActivity;

importandroid.os.Bundle;

importandroid.view.Menu;

importandroid.view.MenuItem;

publicclassMainActivityextendsActionBarActivity{

Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

Override

publicbooleanonCreateOptionsMenu(Menumenu){

//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}

Override

publicbooleanonOptionsItemSelected(MenuItemitem){

//Handleactionbaritemclickshere.Theactionbarwill

//automaticallyhandleclicksontheHome/Upbutton,solong

//asyouspecifyaparentactivityinAndroidManifest.xml.

intid=item.getItemId();

if(id==R.id.action_settings){

returntrue;

}

returnsuper.onOptionsItemSelected(item);

}

}

5、实验结果

六、实验小结

这个实验很简单,主要是要我们学习Android开发环境的搭建,了解Android应用开发程序的开发过程,生成Android应用程序框架以及配置相应的运行参数。

 

实验二界面设计:

空间与布局

1、实验目的

Android编程基础,UI设计;使学生了解Android编程原理;掌握界面控件设计;掌握控件的事件处理编程。

2、主要仪器设备及套数

计算机

3、实验容

了解各种空间的基本功能:

Menu,TextViewEditText,Button,Radiobutton,List;了解布局layout的应用;利用布局安排各种控件,设计良好用户界面。

四、程序代码

Package.example.Edittext;

Importandroid.os.Bundle;

Importandroid.app.Activity;

Importandroid.view.Menu;

Importandroid.widget.RadioGroup;

Importandroid.widget.RadioGroup.OnCheckedChangeListener;

Importandroid.widget.TextView;

publicclassMainActivityextendsActivity{

RadioGroupbutton01;

TextViewshow;

Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//获取界面上button01,show两个组件对象

Button01=(RadioGroup)findviewByid(R.Id.button01);

Show=(TextView)findviewByid(R.Id.button01);

//为RadioGro组件的oncheck事件绑定时间监听器

Button01.setoncheckedchangelistener(newOnCheckedChangeListener()

{

Override

publicvoidonCreateOptionMenu(RadioGroupgroup,intcheckedId){

//根据勾选的单选按钮来动态改变tip字符串的值

Stringtip=checkedId==R.id.boy?

“男人”:

“女人”;

//修改show组件中的文本

Show.setText(tip);

}

});

}

Override

publicbooleanonCreateOptionMenu(Menumenu){

getMenuInflater().inflate(R.menu.main,menu);

returntrue;

}

}

五、实验结果

运行:

效果:

六、实验小结

通过这次试验,我对安卓编程有了进一步的认识,对各控件的熟练应用会帮我们大大减少少编程量,另外界面的布局还会影响到设计界面的美观。

 

实验三Android触控监听器的使用

一、实验目的

1.掌握Android项目中界面显示的基本方法;

2.掌握OnTouchListener监听器的设计与使用

3.掌握Android手机硬件API的调用方法。

二、主要仪器设备及套数

计算机

三、实验容

在Android平台下设计实现滑动的方块应用

1.使用屏幕触控,图拽方块移动。

2.暂不考虑横屏切换

4、程序代码

控制文件:

package.ex06_03;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.view.View.OnTouchListener;

publicclassMainActivityextendsActivity{

intx1=150,y1=50;

TestViewtestView;

Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

testView=newTestView(this);

testView.setOnTouchListener(newmOnTouch());

testView.getXY(x1,y1);

setContentView(testView);

}

privateclassmOnTouchimplementsOnTouchListener

{

publicbooleanonTouch(Viewv,MotionEventevent)

{

if(event.getAction()==MotionEvent.ACTION_MOVE)

{//在屏幕上滑动(拖动)

x1=(int)event.getX();

y1=(int)event.getY();

testView.getXY(x1,y1);

setContentView(testView);

}

if(event.getAction()==MotionEvent.ACTION_DOWN)

{//点击

x1=(int)event.getX();

y1=(int)event.getY();

Log.i("x=",String.valueOf(x1));

Log.i("y=",String.valueOf(y1));

testView.getXY(x1,y1);

setContentView(testView);

}

returntrue;

}

}

package.ex06_03;

importandroid.content.Context;

importandroid.graphics.Canvas;

importandroid.graphics.Color;

importandroid.graphics.Paint;

importandroid.view.View;

 

publicclassTestViewextendsView

{

intx,y;

publicTestView(Contextcontext)

{

super(context);

}

voidgetXY(int_x,int_y)

{

x=_x;

y=_y;

}

/*下面编写绘制小球的代码,参见教材例6-3*/

protectedvoidonDraw(Canvascanvas)

{

super.onDraw(canvas);

/*设置背景为青色*/

canvas.drawColor(Color.CYAN);

Paintpaint=newPaint();

/*去锯齿*/

paint.setAntiAlias(true);

/*设置paint的颜色*/

paint.setColor(Color.BLACK);

/*画一个实心圆*/

canvas.drawRect(x,y,x+30,y+30,paint);

paint.setColor(Color.GREEN);

/*canvas.drawRect(x-6,y-6,3,3,paint);*/

}

界面布局:

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

>

android="schemas.android./apk/res/android"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

android:

orientation="vertical">

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="string/hello"/>

5、实验结果

6、实验小结

简单触摸屏事件指在触摸屏上按下、抬起、滑动的事件,在Android系统中,通过OnTouchListener监听接口来处理屏幕事件当在View的围进行按下、抬起、滑动等动作时都会触发该事件。

在本次试验中我学会了简单应用Android触控监听器来操作屏幕,虽然在实验中遇到了一些困难,但还是受益良多。

 

实验四网络访问与服务

一、实验目的

掌握Android网络访问方法。

二、主要仪器设备及套数

计算机

三、实验容

1.了解手机WEB访问编程,通过HttpResponse类,读入网络数据

2.通过网络进行数据访问

3.了解数据库使用

四、程序代码

publicclassslide8_3extendsActivity{

PublicStingmyhttpget(Stringurl)throwsException{

Stringout=null;

Try{

HttpClientclient=newDefaultHttpClient();

HttpGetrequest=newHttpGet(url);

HttpResponserequest=client.execute(request);

out=EntityUtils.toString(response.getEntity());

}catch(IOExceptione){

e.printStackTrace();

}

returnout;

}

PublicStringmyhttppost(Stringurl,Stringname,Stringvalue)

ThrowEeception{

Stringout=null;

try{

HttpClientclient=newDefaultHttpClient();

HttpPostrequest=newHttpPost(url);

ListpostParameters=newArrayList();

postParameters.add(newBasicNameValuePair(name,value));

UrlEncodedFormEntity=newEncodedFormEntity=newUrlEncodedFormEntity(

postParameters);

request.setEntity(formEntity);

HttpResponseresponse=client.execute(request);

out=EntityUtils.toString(response.getEntity());

}catch(IOExceptione){

e.printStackTrace();

}

returnout;

Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

FinalTextViewtextview=(TextView)findviewbyid(R.id.TextView01);

FinalEditVieweditview=(EditView)findviewbyid(R.id.EditText1);

finalEditTextname=(EditText)findviewbyid(R.id.EditText2);

finalEditTextvalue=(EditText)findviewbyid(R.id.EditText3);

Buttonbtn=(Button)this.findviewbyid(R.id.Button1);

btn.setOnClickListener(newButton.OnClickListener(){

Publicvoidonclick(Viewv){

Try{

textView.setText(myhttpget(editText.getText().toString()+”?

”+name.getText().toString()+”=”+value.getText().toString()));

}catch(Exceptione){

e.printStackTrace();

}

}

});

Buttonbtn2=(Button)this.findviewbyid(R.id.Button2);

Btn2.setOnClickListener(newButton.OnClickListener(){

Publicvoidonclick(Viewv){

Try{

 

textView.setText(myhttppost(editText.getText().toString(),name.getText().toString(),

value.getText().toString()));

}catch(Exceptione){

e.printStackTrace();

}

}

});

}

}

五、实验结果

Get:

Post:

六、实验小结

在这次试验中,我基本掌握了Android访问网络的基本方法,了解了手机WEB访问编程,通过HttpResponse类,读入网络数据,再通过网络进行数据访问,最后还基本学会了数据库使用。

 

实验五Android平台下手电应用的设计开发

一、实验目的

1.掌握Android项目中界面显示的基本方法;

2.掌握OnTouchListener监听器的设计与使用

3.了解Android手机硬件API的调用方法。

二、主要仪器设备及套数

计算机

三、实验容

1.使用屏幕触控

2.调用相机的闪光点硬件,开启手电功能

3.暂不考虑横屏切换

四、程序代码

package.lee.handlight;

importandroid.app.Activity;

importandroid.hardware.Camera;

importandroid.hardware.Camera.Parameters;

importandroid.os.Bundle;

importandroid.os.Process;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.WindowManager;

importandroid.widget.ToggleButton;

publicclassHandLightActivityextendsActivityimplementsOnClickListener{

privateToggleButtontoggleButton;

privateCameracamera=Camera.open();

/**Calledwhentheactivityisfirstcreated.*/

Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

toggleButton=(ToggleButton)this.findViewById(R.id.toggleButton1);

toggleButton.setOnClickListener(this);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

//保持屏幕

}

Override

publicvoidonClick(Viewv){

ToggleButtontb=(ToggleButton)v;

Camera.Parametersparam=camera.getParameters();

if(!

tb.isChecked()){

param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

toggleButton.setBackgroundColor(0x30ffffff);

}else{

param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

toggleButton.setBackgroundColor(0xffffffff);

}

camera.setParameters(param);

}

Override

protectedvoidonPause(){

//camera.release();

//Process.killProcess(Process.myPid());

super.onPause();

}

}

四、实验结果

五、实验小结

能够掌握Android项目中界面显示的基本方法,对监听器的机制和实现监听的四种方式有所了解,会使用Android手机硬件API的调用方法、实现功能,并能独立完成程序增强了自己的动手实践能力,并且成功将实验项目导入手机中且运行成功。

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

当前位置:首页 > 人文社科 > 法律资料

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

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