实验四 Android程序设计


课程:Java程序设计 班级:1752班 姓名:李得琛 学号:20175206
指导教师:娄嘉鹏
实验日期:2019年5月16日
实验序号:实验四
实验名称:Android程序设计

软件准备工作及问题汇总

在安装andriod软件中,出现了软件安装完成,启动时一直download Components的问题,在查找了一系列资料后,(后面给出网址),我进入安装软件目录的bin文件中,以记事本形式打开idea文件,在最后输入了 disable.andriod.first.run=true的命令,然后直接加载了页面,不会出现之前一直下载不结束的画面,具体需要的SDK软件在进入软件之后可以在里面下载,所以该问题得到了解决。

在启动手机模拟时,也出现无法下载的情况,似乎多试几次就可以了,我反正是多下了几次就成功了,截图如下

打开AVD

下载过程

下载结束后,软件可用

到目前为止准备工作OK,开始实验

实验四 Android程序设计-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 Stuidio调试应用程序

实验步骤

第一个实验较为容易,在参考了《Java和Android开发学习指南(第二版)第二十四章后,仅需要在字符串Hello world 后面加上自己的学号,自己学号前后一名同学的学号即可成功完成实验。
在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! 20175206李得琛(本人) 20175205侯颖 20175207冷南 "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

实验截图

代码

20175206李得琛

实验四 Android程序设计-2

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

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

实验步骤

第二个实验是用MainActivity启动ThirdActivity,所以要运行MainActivity

1.先对MainActivity中代码进行修改,在程序中中新加代码创建intent对象,加上按钮键端口,代码如下:

package com.example.ldcapplication;

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, ldcActivityDemo.class); // 创建一个Intent对象
                startActivity(intent);
            }

        })
        ;}
}

在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 20175229 20175230"
        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!20175229  20175230" />

</android.support.constraint.ConstraintLayout>


新建ldcActivitydemo,代码如下:

package com.example.ldcapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ldcActivityDemo extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ldc_demo);
    }
}

在新建文件layout中对应的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=".ldcActivityDemo">
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="20175206李得琛"
        tools:layout_editor_absoluteX="150dp"
        tools:layout_editor_absoluteY="150dp"
        tools:ignore="MissingConstraints" />

</android.support.constraint.ConstraintLayout>

最后在manifests下的AndroidMainfest.xml进行注册

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ldcActivityDemo" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这样实验就完成了

实验截图

由此可以得出运行结果

加载初始页面

另一个Activity

代码

20175206李得琛

实验四 Android程序设计-3

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

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

实验步骤

在对书上的案例进行分析以后,第三个实验较为简单,就是在第二个实验的基础上进行Toast消息提示,所以在MainActivity中进行修改
引入方法import android.widget.Toast;
快速调用Toast.makeText().show();

即可得出实验结果,因为是弹框实验,三秒左右就会自动消失,所以要及时截图

修改的代码

package com.example.ldcapplication;

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, "20175206!", 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, ldcActivityDemo.class); // 创建一个Intent对象
                startActivity(intent);
            }

        })
        ;}

}

实验截图

代码

20175206李得琛

实验四 Android程序设计-4

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

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

实验步骤

因为是根据书上的例子进行改动,所以先将书上的案例抄下来进行修改,在查询资料后,对andriod布局有了一定得了解,如下是代码的布局规则:

相对位置规则: 
android:layout_above 将该控件的底部至于给定ID的控件之上 
android:layout_below 将该控件的顶部至于给定ID的控件之下 
android:layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐 
android:layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐 
兄弟控件对齐规则: 
android:layout_alignBaseline 将该控件的baseline和给定ID的控件的baseline对齐 
android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘对其 
android:layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐 
android:layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐 
android:layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐 
父控件对齐规则: 
android:alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐 
android:layout_alignParentLeft 如果该值为true,则将该控件的左边与父控件的左边对齐 
android:layout_alignParentRight 如果该值为true,则将该控件的右边与父控件的右边对齐 
android:layout_alignParentTop 如果该值为true,则将空间的顶部与父控件的顶部对齐 
中央位置规则: 
android:layout_centerVertical 如果值为真,该控件将被至于垂直方向的中央 
android:layout_centerHorizontal 如果值为真,该控件将被至于水平方向的中央 
android:layout_centerInParent 如果值为真,该控件将被至于父控件水平方向和垂直方向的中央 
重力规则: 
android:gravity[setGravity(int)]设置容器内各个子组件的重力方向 
android:ignoreGravity[setIgnoreGravity(int)]设置容器哪个子组件的不受重力方向影响

因为要修改书上的格局,所以修改之后,MainActivity中代码如下

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

修改布局,对应的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:paddingLeft="2dp"
    android:paddingRight="2dp">

    <Button
        android:id="@+id/nameButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff7700"
        android:text="李得琛"
        android:layout_marginTop="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
    <Button
        android:id="@+id/englishButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20175206"
        android:background="#9000ff"
        android:layout_below="@+id/nameButton"
        android:layout_alignLeft="@+id/nameButton"
        android:layout_alignStart="@+id/nameButton"
        android:layout_marginTop="40dp"/>

    <Button
        android:id="@+id/numberButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nameButton"
        android:layout_alignStart="@+id/nameButton"
        android:layout_alignLeft="@+id/nameButton"
        android:layout_marginStart="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="117dp"
        android:background="#001eff"
        android:orientation="vertical"
        android:text="yes we can" />
    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="45dp"
        android:padding="4dp"
        android:layout_below="@id/numberButton"
        android:layout_centerHorizontal="true"
        android:src="@android:drawable/ic_btn_speak_now" />
    <LinearLayout
        android:id="@+id/filter_button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center|bottom"
        android:background="@android:color/black"
        android:orientation="vertical" >

        <Button
            android:id="@+id/filterButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_above="@+id/filter_button_container"
            android:text="Save" />
        <Button
            android:id="@+id/shareButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Share" />
        <Button
            android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Delete" />
    </LinearLayout>
</RelativeLayout>

修改之后,我们可以看到布局有了很大的改观,甚至在背景颜色也有了一定的改动

实验截图

代码

20175206李得琛

实验四 Android程序设计-5

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

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

实验步骤

依旧是参考书上的案例进行修改,调整了颜色,位置,文字等多种元素,功能是点击文字和时钟颜色均可变化
MainActivity中的代码

package com.example.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        android:id="@+id/analogClock1"
        android:onClick="changeColor"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="60dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="300dp"
        android:text="时钟"
        android:onClick="changeColor"
        android:textAppearance="@android:style/TextAppearance.Widget.TextView"
        android:textSize="30dp" />
</RelativeLayout>

因为在这一个过程运用到了其他的文件xml,所以要自己创建并输入对应的代码来操控程序,需要的文件如下:

menu>menu_main.xml

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

</menu>

dimens

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="activity_vertical_margin" />
    <dimen name="activity_horizontal_margin" />
</resources>

就这两个需要自己创建,其余的启动应用后自动创建,实验完成

实验截图

选取了蓝墨云提交的案例,其实点击文字和时钟均可变色
模块changeColor

   public void changeColor(View view) {
        if (counter == colors.length) {
            counter = 0;
        }

代码

20175206李得琛

参考文献

安装完成,启动时一直download Components解决办法

总结分析

程序还是参考书上的案例一点一点做,最困难的还是软件的安装,各种问题层出不穷,每个人遇到的问题都不大一样,所以只能不断查询资料进行实验,这是实验最困难的地方。

步骤 耗时 百分比
需求分析 10min 12.5%
设计 15min 18.75%
代码实现 30min 37.5%
测试 5min 6.25%
分析总结 20min 25%
原文地址:https://www.cnblogs.com/ldc175206/p/10877562.html