[TOC]##實驗報告封面 課程:Java程序設計 班級:1752班 姓名:李得琛 學號:20175206 指導教師:婁嘉鵬 實驗日期:2019年5月16日
實驗序號:實驗四 實驗名稱:Android程序設計html
##軟件準備工做及問題彙總android
在安裝andriod軟件中,出現了軟件安裝完成,啓動時一直download Components的問題,在查找了一系列資料後,(後面給出網址),我進入安裝軟件目錄的bin文件中,以記事本形式打開idea文件,在最後輸入了 disable.andriod.first.run=true
的命令,而後直接加載了頁面,不會出現以前一直下載不結束的畫面,具體須要的SDK軟件在進入軟件以後能夠在裏面下載,因此該問題獲得瞭解決。git
在啓動手機模擬時,也出現沒法下載的狀況,彷佛多試幾回就能夠了,我反正是多下了幾回就成功了,截圖以下app
打開AVD ide
下載過程 佈局
下載結束後,軟件可用 學習
到目前爲止準備工做OK,開始實驗測試
##實驗四 Android程序設計-1 Android Stuidio的安裝測試: 參考《Java和Android開發學習指南(第二版)(EPUBIT,Java for Android 2nd)》第二十四章:ui
###實驗步驟this
第一個實驗較爲容易,在參考了《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)》第二十五章:
###實驗步驟
第二個實驗是用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消息提示,因此在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)》第二十七章:
###實驗步驟
由於是根據書上的例子進行改動,因此先將書上的案例抄下來進行修改,在查詢資料後,對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)》第二十八章:
###實驗步驟
依舊是參考書上的案例進行修改,調整了顏色,位置,文字等多種元素,功能是點擊文字和時鐘顏色都可變化 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% |