2018-2019-2 20175211 實驗四《Android程序設計》實驗報告

1、實驗內容及步驟

一、Android Studio的安裝測試

  • 要求:
    • 參考http://www.cnblogs.com/rocedu/p/6371315.html#SECANDROID,安裝 Android Stuidio
    • 完成Hello World, 要求修改res目錄中的內容,Hello World後要顯示本身的學號,本身學號先後一名同窗的學號,提交代碼運行截圖和碼雲Git連接,截圖沒有學號要扣分
    • 學習Android Stuidio調試應用程序

安裝Android Studio

  • 從官網上下載,一步一步跟着安裝就好了
  • 點擊下圖標誌配置虛擬手機來測試代碼

Android基礎知識(參考:Android Developer Docs

  • 應用組件是 Android 應用的基本構建基塊。每一個組件都是一個不一樣的點,系統能夠經過它進入您的應用。共有四種不一樣的應用組件類型
    • Activity 表示具備用戶界面的單一屏幕
    • Services 服務是一種在後臺運行的組件,用於執行長時間運行的操做或爲遠程進程執行做業
    • Broadcast receivers 內容提供程序管理一組共享的應用數據
    • Content providers 廣播接收器是一種用於響應系統範圍廣播通知的組件
  • 啓動組件
    四種組件類型中的三種 — Activity、服務和廣播接收器 — 經過名爲 Intent 的異步消息進行啓動。Intent 會在運行時將各個組件相互綁定(您能夠將 Intent 視爲從其餘組件請求操做的信使),不管組件屬於您的應用仍是其餘應用。java

  • 清單文件
    在 Android 系統啓動應用組件以前,系統必須經過讀取應用的 AndroidManifest.xml 文件(「清單」文件)確認組件存在。 您的應用必須在此文件中聲明其全部組件,該文件必須位於應用項目目錄的根目錄中。android

Android Studio基本使用(參考:項目概覽

Android Studio界面和Idea基本一致,左側是項目的文件結構目錄
git

咱們主要使用的文件在app目錄下android-studio

  • manifests
    存放清單文件
  • java
    存放java代碼,咱們編寫的代碼放在第一個文件夾下
  • res
    存放各類資源,基本上是xml文件

修改代碼

要輸出咱們本身的學號能夠直接去res->layout->activity_main.xml修改。建立項目的時候,默認有一個居中的TextView組件,內容是helloworld,咱們增長本身的文本,結果以下app

運行截圖

二、Activity測試

  • 要求:
    • 構建項目,運行教材相關代碼
    • 建立 ThirdActivity, 在ThirdActivity中顯示本身的學號,修改代碼讓MainActivity啓動ThirdActivity
    • 提交代碼運行截圖和碼雲Git連接,截圖要有學號水印,不然會扣分

Activity基本知識(參考:Introduction to Activities

  • Activity生命週期
    • onCreate() 在系統建立活動時觸發。必須在此處調用setContentView()以定義活動用戶界面的佈局。onCreate()完成後,接下來回調的永遠是onStart()
    • onStart() onCreate()退出時,活動進入開始狀態
    • onResume() 系統在活動開始與用戶交互以前調用此回調。應用程序的大多數核心功能都是在該onResume()方法中實現的。
    • onPause() 當活動失去焦點並進入暫停狀態時, 系統會調用。例如,當用戶點擊「後退」或「最近」按鈕時,會出現此狀''態。
    • onStop() 當活動再也不對用戶可見時, 系統會調用。
    • onRestart() 當處於「已中止」狀態的活動即將從新啓動時,系統將調用此回調
    • onDestroy() 系統在銷燬活動以前調用此回調

  • 聲明activity
    在清單文件中以下聲明
<manifest ... >
  <application ... >
      <activity android:name=".ExampleActivity" />
      ...
  </application ... >
  ...
</manifest >

MainActivity代碼

public class MainActivity extends Activity implements View.OnTouchListener {

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv1 = findViewById(R.id.textView1);
        tv1.setOnTouchListener(this);
        TextView tv2 = findViewById(R.id.textView2);
        tv2.setOnTouchListener(this);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        if (arg0.getId()==(R.id.textView1)){
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
        if (arg0.getId()==(R.id.textView2)){
            Intent intent = new Intent(this, ThirdActivity.class);
            startActivity(intent);
        }
        return true;
    }
}

TextView對象能夠調用setOnTouchListener方法開啓監聽,當用戶按下該對象時觸發onTouch事件。用if語句判斷用戶觸發的是哪一個事件,跳轉到不一樣的頁面。異步

運行截圖

三、UI測試

  • 要求:
    • 修改代碼讓Toast消息中顯示本身的學號信息
    • 提交代碼運行截圖和碼雲Git連接,截圖要有學號水印,不然會扣分

Toast基本知識

MainActivity代碼

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Context context = getApplicationContext();
        CharSequence text = "I am ... 20175211(響指)!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}

運行截圖

四、佈局測試

  • 要求
    • 修改佈局讓P290頁的界面與教材不一樣
    • 提交代碼運行截圖和碼雲Git連接,截圖要有學號水印,不然會扣分

Layout基本知識(參考:佈局資源

  • 檔案位置
    res/layout/filename.xml
    文件名將用做資源ID。
  • 句法
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@[+][package:]id/resource_name"
    android:layout_height=["dimension" | "match_parent" | "wrap_content"]
    android:layout_width=["dimension" | "match_parent" | "wrap_content"]
    [ViewGroup-specific attributes] >
    <View
        android:id="@[+][package:]id/resource_name"
        android:layout_height=["dimension" | "match_parent" | "wrap_content"]
        android:layout_width=["dimension" | "match_parent" | "wrap_content"]
        [View-specific attributes] >
        <requestFocus/>
    </View>
    <ViewGroup >
        <View />
    </ViewGroup>
    <include layout="@layout/layout_resource"/>
</ViewGroup>
  • 內容
    • ViewGroup
      其餘View元素的容器
    • <View>
      單個UI組件,一般稱爲「小部件」。不一樣種類的View對象包括TextView, Button,和CheckBox。
      • 屬性:
        • android:id
          資源ID。元素的惟一資源名稱,可用於View從應用程序獲取對該元素的引用。詳細瞭解下面的價值android:id。
        • android:layout_height
          維度或關鍵字。必填。元素的高度,做爲維度值(或 維度資源)或關鍵字("match_parent"或"wrap_content")。請參閱下面的有效值。
        • android:layout_width
          維度或關鍵字。必填。元素的寬度,維度值(或 維度資源)或關鍵字("match_parent"或"wrap_content")。
  • Value for android:id
    對於ID值,一般應使用如下語法形式:"@+id/name"+表示這是一個新的資源ID。aapt工具將在R.java類中建立一個新的資源整數(若是它尚不存在)。例如
    <TextView android:id="@+id/nameTextbox"/>
    該nameTextbox名稱如今是附加到此元素的資源ID。而後,您能夠參考TextViewJava中與ID關聯的內容:
    TextView textView = findViewById(R.id.nameTextbox);
    此代碼返回TextView對象。ide

  • Value for android:layout_height and android:layout_width
    高度和寬度值可使用Android支持的任何 維度單位(px,dp,sp,pt,in,mm)或如下關鍵字表示:
    工具

佈局文件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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/choose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:gravity="center|top"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:text="@string/cancel"
            android:layout_marginEnd="30dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/saveButton"
            android:layout_marginLeft="30dp"
            android:text="@string/save"
            android:layout_marginStart="30dp" />

    </LinearLayout>


    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="150dp"
        android:padding="4dp"
        android:layout_below="@+id/choose"
        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_below="@+id/image"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <Button
            android:id="@+id/filerButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="20dp"
            android:text="@string/filter"/>

        <Button
            android:id="@+id/shareButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/share" />

        <Button
            android:id="@+id/deleteButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:text="@string/delete" />

    </LinearLayout>
</RelativeLayout>

運行截圖

五、事件處理測試

  • 要求
    • 運行教材本章相關代碼並截圖

監聽器

  • Android是基於事件的。使用活動中的一個視圖進行的用戶交互,可能會觸發一個事件,包括點擊、長按、觸碰和按鍵等等
  • 要讓程序響應某一個事件,須要爲該事件編寫一個監聽器。也就是要實現嵌入在android.view.View類中的一個接口。好比OnClickListener接口的onClick()方法

MainActivity

package cn.edu.besti.is.multicolorclock;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

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

運行截圖

2、問題及解決方法

問題1:安裝中一系列問題
(1)一開始我打算將Android Studio安裝在虛擬機中,開啓AVD時報錯以下圖

解決:問題出在虛擬機沒有開啓虛擬化。AVD本質上也是一個虛擬機,只有宿主機開啓虛擬化技術才能夠建立虛擬機,因此在VMware更改設置
佈局

(2)接下來報錯以下

解決:問題出在當前用戶權限不夠,沒法使用/dev/kvm目錄,改權限就好了

(3)你覺得這樣就結束了嗎?不,還有問題。建立虛擬機須要大概8個G的空間,我在虛擬機上作哪裏給你找8個G?
解決(並無):更改配置文件,將建立的虛擬手機內存改成2G。可是改完之後,點擊運行,配置文件裏那個數字又變成了默認的大小。我一氣之下把配置文件改爲了只讀,Android Studio直接告訴我沒法寫入文件,報錯。。。你運行個AVD爲何要改配置文件啊?在baidu、google均無果後,我屈服了,在主機裏下了個Android Studio。然而此時距離提交還有24小時(* ̄︶ ̄)

問題2:書上講的也太簡要了吧。。就照着打,什麼意思都不知道,這作實驗有什麼意義。。在有限的篇幅裏想把什麼東西都講了,結果就是什麼都講很差。。
解決:老辦法,看文檔去吧,文檔真的什麼都有。。配合谷歌翻譯,味道好極了o( ̄▽ ̄)d
連接:適用於應用開發者的文檔

3、代碼託管

  • 碼雲連接
    Android項目文件比較多,我也不肯定什麼該傳什麼不應傳。。就把app下的東西都傳上去了

4、實驗心得體會

作實驗、學知識、寫代碼都還好,作出東西的成就感足以支撐我繼續肝下去,可是安裝軟件遇到問題帶來的挫敗感真的是。。。卡了我兩天,心情奇差。還好最後是安好了。經過此次試驗,我對Android開發有了大概的認識,而且有了基礎的開發的能力,有點手癢癢想重拾團隊項目了,看看時間吧。。
相關文章
相關標籤/搜索