实验一Android开发环境搭建及Helloworld.docx
《实验一Android开发环境搭建及Helloworld.docx》由会员分享,可在线阅读,更多相关《实验一Android开发环境搭建及Helloworld.docx(17页珍藏版)》请在冰豆网上搜索。
实验一Android开发环境搭建及Helloworld
实验一Android开发环境搭建与Helloworld
一、实验目的
学会搭建并安装Android开发环境,并实现HelloWorld应用开发。
二、主要仪器设备及套数
计算机
三、实验容
搭建开发环境;安装JavaJDK,下载Eclipse,解压Eclipse;官方下安装ADT〔AndroidDevelopmentTools〕;安装Android;安装手机USB驱动;建立新工程,实现HelloWorld
四、程序代码
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);
}
}
五、实验结果
六、实验小结
这个实验很简单,主要是要我们学习Android开发环境的搭建,了解Android应用开发程序的开发过程,生成Android应用程序框架以及配置相应的运行参数。
实验二界面设计:
空间与布局
一、实验目的
Android编程根底,UI设计;使学生了解Android编程原理;掌握界面控件设计;掌握控件的事件处理编程。
二、主要仪器设备及套数
计算机
三、实验容
了解各种空间的根本功能:
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.暂不考虑横屏切换
四、程序代码
控制文件:
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"/>
五、实验结果
六、实验小结
简单触摸屏事件指在触摸屏上按下、抬起、滑动的事件,在Android系统中,通过OnTouchListener监听接口来处理屏幕事件当在View的围进展按下、抬起、滑动等动作时都会触发该事件。
在本次试验中我学会了简单应用Android触控监听器来操作屏幕,虽然在实验中遇到了一些困难,但还是受益良多。
实验四网络访问与效劳
一、实验目的
掌握Android网络访问方法。
二、主要仪器设备及套数
计算机
三、实验容
1.了解手机WEB访问编程,通过Response类,读入网络数据
2.通过网络进展数据访问
3.了解数据库使用
四、程序代码
publicclassslide8_3extendsActivity{
PublicStingmyget(Stringurl)throwsException{
Stringout=null;
Try{
Clientclient=newDefaultClient();
Getrequest=newGet(url);
Responserequest=client.execute(request);
out=EntityUtils.toString(response.getEntity());
}catch(IOExceptione){
e.printStackTrace();
}
returnout;
}
PublicStringmypost(Stringurl,Stringname,Stringvalue)
ThrowEeception{
Stringout=null;
try{
Clientclient=newDefaultClient();
Postrequest=newPost(url);
ListpostParameters=newArrayList();
postParameters.add(newBasiameValuePair(name,value));
UrlEncodedFormEntity=newEncodedFormEntity=newUrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
Responseresponse=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(myget(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(mypost(editText.getText().toString(),name.getText().toString(),
value.getText().toString()));
}catch(Exceptione){
e.printStackTrace();
}
}
});
}
}
五、实验结果
Get:
Post:
六、实验小结
在这次试验中,我根本掌握了Android访问网络的根本方法,了解了手机WEB访问编程,通过Response类,读入网络数据,再通过网络进展数据访问,最后还根本学会了数据库使用。
实验五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的调用方法、实现功能,并能独立完成程序增强了自己的动手实践能力,并且成功将实验工程导入手机中且运行成功。