• 2018-2019-2 20175230实验四 《Android开发基础》实验报告


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

    实验步骤

    • 4.运行MainActivity.java

    • 5.修改res目录中的内容,Hello World后要显示自己的学号,自己学号前后一名同学的学号

    • 6.运行MainActivity.java

    实验内容

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

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

    实验步骤

    • 1.在activity_main.xml里新建一个按钮
      修改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!20175230 20175231"
            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!20175230  20175231" />
    </android.support.constraint.ConstraintLayout>
    
    • 2.MainActivity.class中创建intent对象
      修改MainActivity.class中的代码
      整体代码如下
    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);
                }
    
            })
        ;}
    }
    
    • 3.新建活动SecondActivityDemo
      右击app->new->Activity->Empty Activity,然后名字改为SecondActivityDemo,创建成功后左边目录区多了两个文件SecondActivityDemo.javaactivity_second_Demo.xml

    其中 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 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>
    
    • 4.在AndroidMainfest.xml注册
      修改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>
    
    • 5.运行MainActivity.java

    实验四 Android程序设计-3

    实验内容

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

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

    实验步骤

    • 1.打开MainActivity.java
    • 2.引入方法import android.widget.Toast;
    • 3.快速调用Toast.makeText(MainActivity.this, "20175230", Toast.LENGTH_SHORT).show();
      完整代码如下
    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, "20175230!", 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);
                }
            })
        ;}
    }
    

    实验四 Android程序设计-4

    实验内容

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

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

    实验步骤

    • 1.布局的类型
      线性布局LinearLayout:线性布局是一个视图组(ViewGroup),能在水平或者垂直的单一方向上将所有子元素排成一行
      相对布局RelativeLayout:相对布局是一个将子视图显示在相对的位置上的布局
      表格布局TableLayout:表格布局是在行、列中组合子元素的视图
      绝对布局AbsoluteLayout:绝对布局能让你指定子元素的精确位置
      帧布局FrameLayout:帧布局是一个屏幕上的占位符,你可以用它来显示单一视图
      列表视图ListView:列表布局是可以滚动的,是用于显示子元素列表的视图组
      网格视图GridView:网格视图是在二维可滚动的网格中显示子元素的视图组
    • 2.修改activity_main.xml代码
      完整代码如下
    <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/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="20165318"
            android:layout_marginTop="70dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
        <Button
            android:id="@+id/saveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="滕星"
            android:layout_below="@+id/cancelButton"
            android:layout_alignLeft="@+id/cancelButton"
            android:layout_alignStart="@+id/cancelButton"
            android:layout_marginTop="23dp" />
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="45dp"
            android:padding="4dp"
            android:src="@android:drawable/presence_audio_away"
            android:id="@+id/imageView"
            android:layout_below="@+id/saveButton"
            android:background="@android:color/white"
            android:layout_centerHorizontal="true" />
        <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/holo_blue_dark"
            android:orientation="horizontal" >
            <Button
                android:id="@+id/filterButton"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Open" />
            <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>
    

    实验四 Android程序设计-5

    实验内容

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

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

    实验步骤

    • 1.修改MainActivity.java代码
    package cn.edu.besti.is.tx.myapplication;
    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);
        }
        public void changeColor(View view) {
            if (counter == colors.length) {
                counter = 0;
            }
            view.setBackgroundColor(colors[counter++]);
        }
    }
    
    • 2.修改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"
        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="match_parent"
            android:layout_height="match_parent"
            android:text="20175230滕星"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="300dp"
            android:textSize="21dp"/>
    </RelativeLayout>
    
    • 3.运行MainActivity.java

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

    • 1.安装Android Stuidio后,与老师所给的简易教程不太一样,比如创建项目和创建活动
    • 问题一解决方法 可能是由于版本不一样,按照Android Stuidio所给提示一步步运行,最后界面一样
  • 相关阅读:
    C# 編譯錯誤 CS0016 解決方案
    Javascript 日期
    Javascript Url处理
    Linq中in用法
    oracle中的排名函数用法
    webservices [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    C#中的多态
    C# Math类简介 (转)
    客服工单任务系统发展简史 商商
    jQuery LigerUI 使用教程表格篇(2) 服务器数据交互
  • 原文地址:https://www.cnblogs.com/tengxing/p/10869600.html
Copyright © 2020-2023  润新知