班级:1753班
姓名:许钰玮
学号:20175329
指导教师:娄嘉鹏
实验日期:2019年5月13日
实验时间:13:45 - 15:25
实验序号:21
实验名称:Android程序设计html
1. Android Stuidio的安装测试
2. Activity测试
3. UI测试
4.布局测试
5.事件处理测试java
学习Android Stuidio调试应用程序android
此时你能够看到Android Studio的欢迎页已经出来了,就像下面这样:
左侧的Recent Projects将会显示你在这里编辑作的最近项目。右侧的Quick Start则是快速开始选项。app
红色方框选中的Start a new Android Studio project
选项一般是咱们课程里最常使用的,用于建立一个新的Android项目。ide
在此介绍一下其余的选项:工具
1.Open an existing Android Studio Project
:打开已有的Android Studio项目。在经历一段时间的学习后,若是你想继续编辑以前的项目,或者打开一个从网上下载的例子,你能够点击此选项。
2.Check out project from Version Control
:从版本控制库中获取项目。对于团队开发来讲,版本控制是必不可少的工具。此选项容许你从GitHub
、Google Cloud
以及TortoiseSVN
等处同步项目。事实上,Android Studio对于这些版本控制工具的支持也是很好的,你能够在设置中进行设定。
3.Import project(Eclipse ADT, Gradle, etc.)
:导入其余开发环境中的项目。经过该选项你能够将在Eclipse等处生成的项目迁移到Android Studio的开发环境中。
4.Import an Android code sample
:导入Android代码样例。该功能将从Google及其合做伙伴那里读取示例的列表,你能够下载并查看一些优秀的项目并继续编辑它们。
在该对话框中你须要填写待建立的项目名称、公司域名和项目的存放位置。布局
在填写时,有如下事项你须要注意:学习
应用的命名应采用驼峰命名法,首字母必需要大写。
此处的Company Domain
在商业开发中是经常使用的,目的是便于归档。对于初学者而言,你能够理解为下面的Package name
是域名的反转,好比个人域名多是ljp.is.besti.edu.cn
, 包名最好是cn.edu.besti.is.ljp
(上图中没倒过来)
根据实际状况,你能够设置Project location
,也就是项目的位置。一般状况下咱们使用默认值就行。测试
res/layout
文件夹中能够找到。设置好后,点击Finish
按钮完成项目的建立工做。ui
Android Studio会根据这些信息来建立项目,耐心等候它自动建立项目并进入主界面。这时你会在下图所示的对话框中看到一些关于Gradle
的信息。
Gradle是一款获业界高度评价自动化构建工具,它的用处不少,好比引入外部库等等。你能够访问Gradle官网了解更多信息。
稍候一下子,你便能看到Android Studio的主界面了,以下图所示。
配置和启动模拟器
Android模拟器是能够运行在电脑上的虚拟设备,可让你不需使用物理设备便可预览、开发和测试Android应用程序。当你身边并无合适的Android设备时,模拟器就是一个不错的选择。
那么如何使用Android模拟器呢?
在Android Studio的主界面上方的工具栏中,你能够看到一个名为AVD Manager
的按钮,点击它你就能打开Android虚拟设备管理器(AVD: Android Virtual Device)。
咱们常说某个Android手机是4.1
或5.0
的系统,这里的4.1或5.0就是指系统镜像的版本。一样,对于模拟器而言,也须要为其配置某个版本的系统镜像。你能够看到这里只有3个镜像可供选择,请选择第一项——发布名为Lolipop
的Android 5.1.1
镜像。
若是你须要其余版本的系统,你能够在Android SDK Manager中下载对应的系统镜像包,再进入AVD Manager就能看到它们了。
接着,点击右下角的Next
按钮,进入到确认配置的一步。
项目的编译与运行
编译模拟运行以下
Activity
是包含了用户界面组件的一个窗口。一个典型的Android应用程序,都是从启动一个 Activity
开始的。应用程序所建立的第一个窗口,叫作主活动。Activity
涉及到的方法:onCreat()
onPause()
onStart()
onResume()
onStop()
onRestart()
onDestory()
MyActivity.java
代码以下package cn.edu.besti.is.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } }
activity_my.xml
代码以下
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:text="20175329 许钰玮" android:layout_width="200dp" android:layout_height="132dp" tools:layout_editor_absoluteX="255dp" tools:layout_editor_absoluteY="255dp" tools:ignore="MissingConstraints" /> </android.support.constraint.ConstraintLayout>
运行效果
MainActivity.java
代码package cn.edu.besti.is.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast toast = Toast.makeText(MainActivity.this, "20175221曾祥杰", Toast.LENGTH_LONG); toast.show(); } }
activity_main.xml
代码<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.edu.besti.is.myapplication.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
实验截图
activity_main.xml
代码<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="20175221" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="400dp" android:layout_marginLeft="160dp" /> <Button android:text="曾祥杰" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="450dp" android:layout_marginLeft="160dp" /> <ImageButton android:src="@android:drawable/btn_star_big_on" android:background="@android:color/black" android:alpha="0.55" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="160dp" android:layout_marginLeft="183dp" /> <ImageView android:src="@android:drawable/presence_audio_away" android:background="@android:color/holo_blue_bright" android:alpha="0.70" android:layout_width="120dp" android:layout_height="120dp" android:layout_marginTop="200dp" android:layout_marginLeft="140dp" android:padding="4dp" android:id="@+id/imageView" android:layout_centerHorizontal="true" /> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/arrow_down_float" /> <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch" /> </TableRow> </FrameLayout>
运行以下
MainActivity.java
中代码package cn.edu.besti.is.myapplication; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { int counter = 0; int[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it // is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } public void changeColor(View view) { if (counter == colors.length) { counter = 0; } view.setBackgroundColor(colors[counter++]); } }
修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/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="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <AnalogClock android:id="@+id/analogClock1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="90dp" android:onClick="changeColor" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="20175329 许钰玮" android:textSize="30dp" android:layout_marginLeft="90dp" android:layout_marginTop="290dp" android:textColor="#bbbb57"/> </RelativeLayout>
实验效果以下