2018-2019-2 20175218 实验四《Android程序设计》实验报告

2018-2019-2 20175218 实验四《Android程序设计》实验报告

姓名 陈敬勇
班级 1752
学号 20175218
实验序号 实验四
实验名称 Android程序设计

实验内容与要求

  • 参考Android开发简易教程点击进入链接
  • 完成云班课中的检查点,也可以先完成实验报告,直接提交。注意不能只有截图,要有知识点,原理,遇到的问题和解决过程等说明。实验报告中一个检查点要有多张截图。
  • 发表实验报告博客。

一、Android程序设计-1

1、实验要求

Android Stuidio的安装测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十四章:

  • 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECANDROID,安装 Android Stuidio
  • 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号,自己学号前后一名同学的学号,提交代码运行截图和码云Git链接,截图没有学号要扣分
  • 学习Android Studio调试应用程序

2、实验步骤

1、下载安装 Android Studio

  1. 因为工具箱里有Android Studio,所以我是在工具箱里下载的。首先打开工具箱,找到Android Studio,然后点击install就可以下载了。如下图:

  1. 下载好之后,直接打开,出现 Data Sharing 窗口点击 Send usage statistics to Google 。如下图:

  1. 第一次运行会出现警告 SDK 窗口,点击 Cancel 。如下图:

  1. 最后达到使用界面,在工具箱里的下载安装就算完成了。如下图:

2、使用 Android Studio

先说明一下使用界面的那几个选项的功能

  • Start a new Android Studio project 创建一个新的Android项目
  • Open an existing Android Studio Project 打开已有的Android Studio项目
  • Check out project from Version Control 从版本控制库中获取项目
  • Import project(Eclipse ADT, Gradle, etc.) 导入其他开发环境中的项目
  • Import an Android code sample 导入Android代码样例
  1. 选择 Start a new Android Studio project 创建项目。如下图:

  1. 选择 Empty Activity 创建一个空的活动。如下图:

  1. 在 Name 栏填入 HelloWorld ,在 Language 栏选择 Java ,其他的不用动,会自动生成。

  1. 到了下面这个界面就成功创建项目了。如下图:

3、配置和启动模拟器

  1. 首先在工具栏中找到 AVD Manager(右上角比较小的一个图标),点击打开它。如下图:

  1. 点击 Create Virtual Device... 创建一个虚拟设备。如下图:

  1. 选择自己想要的虚拟手机型号。如下图:

  1. 下载配置的虚拟手机型号的所需。如下图:

  1. 下载好之后点击 Finish 即可下一步。如下图:

  1. 返回到上一界面后,就可以点 Next了。如下图:

  1. 到达这一界面时,三角开始标还是白色的,点击它会自动弹出一个窗口,然后下载所需的东西。如下图:

  1. 下载好之后就一直 Next ,最后 Finish 就可以回到上一界面,此时的那个三角标已经变成绿色的了,虚拟设备也打开了。如下图:

  1. 根据要求,修改学号,改好后运行,这时会弹出设备选择窗口,选择之前选定的设备即可。如下图:

  1. 运行后,在虚拟设备上,可以看到学号信息。如下图:

4、调试程序

  1. 跟 IDEA 相似,想要调试程序,首先要设置断点,然后直接 run >> debug ,在下面 Debug 栏中,就可以看到调试情况。如下图:

5、查看 logcat 日志

  1. 在下面,也就是 Debug 旁边有个 logcat ,这个可以查看操作日志。如下图:

3、实验代码

activity_main.xml

<?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:text="Hello World! 20175217 20175218 20175219"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

4、运行截图

5、码云链接

点击进入链接

二、Android程序设计-2

1、实验要求

Activity测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十五章:

  • 构建项目,运行教材相关代码
  • 创建 ThirdActivity, 在ThirdActivity中显示自己的学号,修改代码让MainActivity启动ThirdActivity
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分

2、实验步骤

  1. 修改 activity_main.xml 的代码,增加一个 Button。
  2. 对MainActivity.java 进行修改,创建intent对象。
  3. 新建一个 activity 名字设为 SecondActivityDemo ,创建好会生成两个文件,一个 SecondActivityDemo.java ,一个 activity_second_demo.xml ,然后对它们的代码进行修改,增添学号。

3、实验代码

activity_main.xml

<?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! 20175217 20175218 20175219"
        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! 20175217 20175218 20175219" />

</android.support.constraint.ConstraintLayout>

MainActivity.java

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);
            }

        })
    ;}
}

SecondActivityDemo.java

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);
    }
}

activity_second_demo.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"
    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="20175218"
        tools:layout_editor_absoluteX="153dp"
        tools:layout_editor_absoluteY="311dp"
        tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>

AndroidMainfest.xml

<?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>

4、运行截图

5、码云链接

点击进入链接

三、Android程序设计-3

1、实验要求

UI测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十六章:

  • 构建项目,运行教材相关代码
  • 修改代码让Toast消息中显示自己的学号信息
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分

2、实验步骤

  1. 在MainActivity.java中,对import android.widget.Toast进行修改,引入方法。
  2. 在MainActivity.java中,对Toast.makeText(MainActivity.this进行修改, 修改学号信息为"20175218!", Toast.LENGTH_SHORT).show(),快速调用。

3、实验代码

MainActivity.java

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, "20175218!", 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);
            }

        })
    ;}
}

4、运行截图

5、码云链接

点击进入链接

四、Android程序设计-4

1、实验要求

布局测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十七章:

  • 构建项目,运行教材相关代码
  • 修改布局让P290页的界面与教材不同
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分

2、实验步骤

通过图形界面进行操作

  1. 在 activity_second_demo.xml 中,选择下面的 Design(就在Text旁边),这就进入了图形界面设计窗口。在这里可以直接对虚拟设备的界面和功能进行设计,比如改个背景,就点击上面那个界面图形,然后在右边的功能栏里选择 foreground ,然后就可以设计自己想要的背景界面。

3、运行截图

五、Android程序设计-5

1、实验要求

事件处理测试: 参考《Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)》第二十八章:

  • 构建项目,运行教材相关代码
  • 提交代码运行截图和码云Git链接,截图要有学号水印,否则会扣分

2、实验步骤

  1. 对 MainActivity.java 和 activity_main.xml进行修改,增加要修改的界面元素,这里要设计的是一个钟,然后加入改变的颜色元素,代码实现。
  2. 添加一个 menu 文件

3、实验代码

MainActivity.java

package com.example.helloworld;
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[counter++]);
    }
}

activity_main.xml

<?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! 20175217 20175218 20175219"
        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! 20175217 20175218 20175219" />
    <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>
</android.support.constraint.ConstraintLayout>

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

</menu>

4、运行截图

  1. 最开始白色

  1. 点击一下变黄色

  1. 再点击一下变黑色

  1. 再点击一下变蓝色

5、码云链接

点击进入链接

六、实验过程中遇到的问题及解决方法

1、问题一

  • 用工具箱安装时,少了好多配置和下载的东西,导致后期做实验不顺利
  • 解决方法:在实验中由于之前是工具箱安装的,所以很多设置都是系统自己设置的,实验中,当遇到提醒和警告时,要根据它的要求进行修改,以及下载所需要下载的东西。

2、问题二

  • 在做第四部分实验时,用代码做总是有错误,而导致运行不了
  • 解决方法:直接在图形界面做,方便又快捷

3、问题三

  • 做第五部分实验时,代码中的 menu 提醒错误
  • 解决方法:选择提醒的 menu ,会出现一个提示,选择创建一个 menu 文件,直接创建就可以。创建后, menu 就可以运行了。

七、代码托管

码云链接

八、实验心得

对于这次实验,总的来说不是很难,唯一难点就是自学这些操作,包括这个软件的用法、实验步骤等等。但是只要按照教程,一步一步做下去就基本上可以做出来。然后就是,可以自己上网查询资料,对关于这方面的知识进行拓展学习。通过这个实验,我也了解到了Android程序设计的一些基本知识,包括如何配置自己想要的虚拟设备、如何在虚拟设备上修改界面和创建一些功能等等,这些经验和知识对我学习Java都有很大的帮助,也让我的知识面得到了很好地扩展。最后就是希望自己在以后的实验中能够做得更好。

posted @ 2019-05-17 10:22  20175218陈敬勇  阅读(549)  评论(0编辑  收藏  举报