2018-2019-2 20175105王鑫浩《Java程序设计》实验四 《Android开发基础》实验报告
<h1 id="实验四-android开发基础">实验四 《Android开发基础》</h1><h2 id="实验报告封面">实验报告封面</h2>
<p>课程:Java程序设计 班级:1751班 姓名:王鑫浩 学号:20175105<br>
指导教师:娄嘉鹏 实验日期:2019年5月17日<br>
实验时间:--- 实验序号:实验四<br>
实验名称:Android开发基础</p>
<hr>
<h2 id="实验要求">实验要求</h2>
<ul>
<li>参考Android开发简易教程</li>
<li>完成云班课中的检查点,也可以先完成实验报告,直接提交。注意不能只有截图,要有知识点,原理,遇到的问题和解决过程等说明。实验报告中一个检查点要有多张截图。</li>
<li>发表实验报告博客,标题“学期(如2018-2019-2) 学号(如20175300) 实验四《Android开发基础》实验报告”</li>
</ul>
<hr>
<h2 id="实验步骤">实验步骤</h2>
<h3 id="下载安装android-studio">下载安装Android Studio</h3>
<blockquote>
<p>安装时需勾选如图所示<code>安装虚拟机</code><br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511122835951-103296910.png" alt="" loading="lazy"><br>
<strong>接下来按照步骤安装即可,无需更改默认勾选选项</strong></p>
</blockquote>
<blockquote>
<p>配置Proxy<br>
第一次启动会要求安装Proxy,按步骤走<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511123308240-1597066960.png" alt="" loading="lazy"></p>
</blockquote>
<ul>
<li><code>Android SDK location</code>部分填的是新项目的存放地</li>
<li><code>JDK location</code>须填JDK的绝对路径<br>
<em>不同版本软件的安装界面可能有所不同,下面不再赘述</em></li>
</ul>
<h3 id="新建项目">新建项目</h3>
<blockquote>
<p>进入界面<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511123802089-610631280.png" alt="" loading="lazy"><br>
红色方框选中的<code>Start a new Android Studio project</code>选项通常是我们课程里最常使用的,用于创建一个新的Android项目。<br>
在此介绍一下其他的选项:<br>
<code>Open an existing Android Studio Project</code>:打开已有的Android Studio项目。在经历一段时间的学习后,如果你想继续编辑之前的项目,或者打开一个从网上下载的例子,你可以点击此选项。<br>
<code>Check out project from Version Control</code>:从版本控制库中获取项目。对于团队开发来说,版本控制是必不可少的工具。此选项允许你从GitHub、Google Cloud以及TortoiseSVN等处同步项目。事实上,Android Studio对于这些版本控制工具的支持也是很好的,你可以在设置中进行设定。<br>
<code>Import project(Eclipse ADT, Gradle, etc.)</code>:导入其他开发环境中的项目。通过该选项你可以将在Eclipse等处生成的项目迁移到Android Studio的开发环境中。<br>
<code>Import an Android code sample</code>:导入Android代码样例。该功能将从Google及其合作伙伴那里读取示例的列表,你可以下载并查看一些优秀的项目并继续编辑它们。</p>
</blockquote>
<blockquote>
<p>新建项目<br>
关键点如下:</p>
</blockquote>
<ul>
<li>项目模板选<em>Empty Activity</em></li>
<li>项目名填<code>Hello World</code></li>
<li><code>Package name</code>直接保持自动生成的名字就好</li>
<li><code>Language</code>选择Java</li>
<li><code>Minimum API level</code>按老师的博客来就好<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511124514343-379191451.png" alt="" loading="lazy"></li>
</ul>
<h3 id="配置和启动模拟器">配置和启动模拟器</h3>
<blockquote>
<p>启动虚拟机<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511215418283-895684570.gif" alt="" loading="lazy"></p>
</blockquote>
<blockquote>
<p>新建虚拟机<br>
<strong>这一步千万千万不要在手机连电脑的情境下做(<s>这是我试了好多好多解决方案得出的结论</s>)</strong><br>
*新建过程中第一步挑选型号(屏幕大小等),第二部挑选系统版本<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511215901970-833270141.png" alt="" loading="lazy"></p>
</blockquote>
<h3 id="项目的编译和运行">项目的编译和运行</h3>
<blockquote>
<p>运行代码</p>
</blockquote>
<ul>
<li><code>HelloWorld</code>在项目中是自带的<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511220456636-1366853522.png" alt="" loading="lazy"></li>
</ul>
<blockquote>
<p>选择虚拟机</p>
</blockquote>
<ul>
<li>如果虚拟机已经打开了,点击<code>Connected Dvices</code>部分的,否则点下面部分的<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511221037509-903903582.png" alt="" loading="lazy"></li>
</ul>
<blockquote>
<p>成功运行<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511221345245-1033735829.png" alt="" loading="lazy"></p>
</blockquote>
<h4 id="环境配置至此完结"><strong>环境配置至此完结</strong></h4>
<hr>
<h2 id="基本操作云班课中检查点自己想的名字">基本操作(云班课中检查点,自己想的名字)</h2>
<blockquote>
<h3 id="修改输出">修改输出</h3>
</blockquote>
<ul>
<li>要求:完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号,自己学号前后一名同学的学号</li>
<li>解决路径:在<code>xml</code>文件中找到<code> android:text=</code>这一语句,按要求修改后面内容即可</li>
<li>代码</li>
</ul>
<pre><code><?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:text="Hello!20175105 20175120"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World!2017510520175120" />
</android.support.constraint.ConstraintLayout>
</code></pre>
<ul>
<li>运行效果<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512114951764-1659724006.png" alt="" loading="lazy"></li>
</ul>
<blockquote>
<h3 id="activity测试">Activity测试</h3>
</blockquote>
<ul>
<li>要求:创建 <code>ThirdActivity</code>, 在<code>ThirdActivity</code>中显示自己的学号,修改代码让<code>MainActivity</code>启动<code>ThirdActivity</code></li>
<li>解决方案:<br>
1.在activity_main.xml里新建一个按钮</li>
</ul>
<pre><code> <Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动另一个activity"
tools:ignore="MissingConstraints" />
</code></pre>
<p>整体代码如下</p>
<pre><code><?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动另一个activity"
tools:ignore="MissingConstraints" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginRight="80dp"
android:text="Hello!20175105 20175120"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World!2017510520175120" />
</android.support.constraint.ConstraintLayout>
</code></pre>
<p>2.<code>MainActivity.class</code>中创建<code>intent</code>对象</p>
<pre><code> button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
MainActivity.this, SecondActivityDemo.class); // 创建一个Intent对象
startActivity(intent);
</code></pre>
<p>整体代码如下</p>
<pre><code>package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
MainActivity.this, SecondActivityDemo.class); // 创建一个Intent对象
startActivity(intent);
}
})
;}
}
</code></pre>
<p>3.新建活动<code>SecondActivityDemo</code><br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512142842795-1960266245.gif" alt="" loading="lazy"><br>
创建成功后你会发现你的文档下多了两个文件,<code>SecondActivityDemo.class</code>与<code>activity_second_Demo</code>,相应代码如下<br>
<code>SecondActivityDemo.java</code></p>
<pre><code>package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivityDemo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_demo);
}
}
</code></pre>
<p><code>activity_second_Demo</code></p>
<pre><code><?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="172dp"
android:layout_height="139dp"
android:text="20175105"
tools:layout_editor_absoluteX="153dp"
tools:layout_editor_absoluteY="311dp"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
</code></pre>
<p>4.在<code>AndroidMainfest.xml</code>注册</p>
<p><strong>注意文件名,不要搞错~</strong><br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512143755661-1593707253.png" alt="" loading="lazy"></p>
<p>完整代码</p>
<pre><code><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.helloworld" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivityDemo"
android:label="Activity">
</activity><!--在这里注册-->
</application> </manifest>
</code></pre>
<p>5.效果展示<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512143823071-1315780076.gif" alt="" loading="lazy"></p>
<blockquote>
<h3 id="toast弹窗设计">Toast弹窗设计</h3>
</blockquote>
<p>以下做法为在<code>MainActivity.java</code>中操作<br>
1.<code>import android.widget.Toast;</code>引入方法<br>
2.<code> Toast.makeText(MainActivity.this, "20175105!", Toast.LENGTH_SHORT).show();</code>快速调用<br>
完整代码</p>
<pre><code>package com.example.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "20175105!", Toast.LENGTH_SHORT).show();
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(
MainActivity.this, SecondActivityDemo.class); // 创建一个Intent对象
startActivity(intent);
}
})
;}
}
</code></pre>
<p>效果展示<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512145339113-1531502042.gif" alt="" loading="lazy"></p>
<blockquote>
<h3 id="界面设计">界面设计</h3>
</blockquote>
<p>这一项感觉直接改代码有些麻烦,直接图形操作界面~<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512145816646-1287626080.gif" alt="" loading="lazy"><br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512145827350-2025685904.gif" alt="" loading="lazy"><br>
结果展示<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512145907668-334688930.png" alt="" loading="lazy"></p>
<blockquote>
<p>事件处理机制<br>
功能描述:在点击屏幕后,时钟背景颜色发生改变<br>
<code>MainActivity</code></p>
</blockquote>
<pre><code>package cn.edu.besti.is.wxh.multicolorclock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AnalogClock;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AnalogClock;
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);
}
}
</code></pre>
<p><code>activity_main</code></p>
<pre><code><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="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
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"
/>
</RelativeLayout>
</code></pre>
<p>效果展示<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512152817820-635318372.gif" alt="" loading="lazy"></p>
<h2 id="问题与解决方案">问题与解决方案</h2>
<blockquote>
<p>问题一:一直出现如下图的提示<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190511222255761-959219771.png" alt="" loading="lazy"><br>
解决方案:试了各种各样的方法,最后发现把手机从电脑的USB口拔掉,再创建一次就好。(物理因素令人恐惧)<br>
问题二:事件处理机制过程中R中menu无法调用<br>
解决方案:根据提示<code>Creat MenuMain.xsl</code>,无需加任何代码,即可解决<br>
问题三:在界面优化中无法移动文本框<br>
解决方案:更改界面模式(瞎起的名字),改成第一个就行。<br>
<img src="https://img2018.cnblogs.com/blog/1594162/201905/1594162-20190512153554355-230541605.gif" alt="" loading="lazy"><br>
修改后可能出现错误提示,这个时候转入代码界面,根据提示添加内容即可。</p>
</blockquote>
<h2 id="感悟和收获">感悟和收获</h2>
<p>本次实验中,配置了自己的Android虚拟机,学会了利用Android Studio在虚拟机上运行app,感觉打开了新的世界~</p>
<h2 id="再多说一句">再多说一句</h2>
<p><strong>千万千万不要连着手机创建虚拟机!</strong><br>
文件汉化包,提取码<code>suwz</code><br>
无需解压,直接把文件放入<code>lib</code>即可</p>
<h2 id="参考资料">参考资料</h2>
<p>1.《Java和Android开发学习指南(第二版)》</p>
<p>2.Android开发简易教程</p><br><br>
来源:https://www.cnblogs.com/wxhblogs/p/10848431.html
頁:
[1]