2017-2018-2 20165312 實驗四《Android程序設計》實驗報告

2017-2018-2 20165312 實驗四《Android程序設計》實驗報告

1、安裝Android Studio並進行Hello world測試和調試程序

安裝Android Studio

能夠參考婁老師的博客Android開發簡易教程或者參考《Java和Android開發學習指南》第二十四章,裏面都有詳細步驟,一步一步來就很簡單~html

新建一個project項目後,project窗口主要有兩個主要的節點:appGradle Scripts。app節點中包含了應用程序中全部的組件,咱們編寫程序時也是主要使用app。app節點下面有包含3個節點java

  • manifests包含一個AndroidManifest.xml清單文件
  • java包含了全部的Java應用程序和測試類
  • res包含了資源文件,在這個目錄下還有一些目錄:
    • drawable
    • layout
    • menu
    • values
    • mipmap

以上就初步瞭解了Android Studio的構造android

進行Hello World測試

步驟:android-studio

  • 打開layout->activity_main.xml
  • 修改android:text
<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="
       Hello World! 20165312曹歌
       Hello World! 20165311李嘉昕
       Hello World! 20165313張晨暉"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
  • 運行MainActivity.java

進行調試程序

日誌消息

調試一個應用程序最簡單的辦法就是使用日誌消息。在開發過程當中,能夠在Android Studio主屏幕的底部看到Android DDMS視圖。關於LogCat,不一樣日誌級別的消息以不一樣的顏色來顯示,每條消息都有一個標籤,這使得很容易找到一條消息。app

跟蹤程序
  • 在任意一行設置斷點
  • 打開Run->Debug便可進行調試程序
  • 在Android Studio下方的Debug欄便可進入代碼、瀏覽變量

  • 具體的調試代碼方法操做我的認爲是和IDEA調試代碼方法相同的~

2、Activity測試

活動(activity)是包含了用戶界面組件的一個窗口,一個典型的Android應用程序,都是從啓動一個活動開始的。應用程序所建立的第一個窗口,叫作主活動。appliaction元素定義兩個活動,其中之一使用intent-filter元素聲明的爲主活動。ide

啓動一個activity涉及實例化活動類,而且調用生命週期方法:佈局

  • onCreat()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onRestart()
  • onDestory()

每個控制文件的Activity都須要有對應的啓動程序文件(.java)相應的佈局文件(.xml)學習

構建項目,運行教材相關代碼

教材相關代碼連接測試

建立 ThirdActivity, 在ThirdActivity中顯示本身的學號,修改代碼讓MainActivity啓動ThirdActivity

步驟:gradle

  • AndroidManifest.xml清單文件中添加一個activity
<activity
            android:name=".ThirdActivity">
        </activity>
  • 添加一個ThirdActivity.java文件
package cn.edu.besti.is.cg;
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
    }
}
  • 添加一個third_activity.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: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=".ThirdActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20165312曹歌"/>

</RelativeLayout>
  • 修改MainActivity.java使其可以自啓動ThirdActivity.java
package cn.edu.besti.is.cg;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

import static cn.edu.besti.is.cg.R.id.textView1;

public class MainActivity extends Activity implements
        OnTouchListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(textView1);
        tv.setOnTouchListener(this);
    }

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

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("message", "Message from First Screen");
        startActivity(intent);
        return true;
    }
}

3、UI測試

所謂UI,就是爲主活動構建用戶交叉(user interface)

修改代碼讓Toast消息中顯示本身的學號信息

Toast:一個消費彈出對話框,用於顯示一條消息做爲給用戶的反饋,Toast並不會代替當前的內容。

修改activity_main.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.edu.besti.is.cg.ui.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="歡迎來到20165312的世界"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

修改MainActivity.java

package cn.edu.besti.is.cg.ui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast toast = Toast.makeText(MainActivity.this, "20165312曹歌", Toast.LENGTH_LONG);
        toast.show();
    }
}

4、佈局測試

Android中的一些佈局:

  • LinearLayout將全部子視圖以相同的方向(水平或者垂直)對齊的一個佈局
  • RelativeLayout根據子視圖的一個或者多個同級視圖的位置來排列他的一個佈局
  • FrameLayout將每個子視圖放在另外一個子視圖頂部的一種佈局
  • TableLayout將子視圖按照行和列來組織的一種佈局
  • GridLayout將子視圖放置到一個柵格中的一種佈局

修改佈局讓P290頁的界面與教材不一樣

修改activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button android:text="Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="100dp" />
    <ImageButton
        android:src="@android:drawable/btn_star_big_on"
        android:alpha="0.45"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="300dp" />
</FrameLayout>

5、事件處理測試

運行教材代碼

MainActivity.java

package cn.edu.besti.is.cg.cellview;
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" />
</RelativeLayout>

實驗中遇到的問題

出現Executing tasks: app:assembleDebug的問題,百度以後找到androidStudio出現Executing tasks: app:assembleDebug

build.gradle(module:app)文件中

testApplicationId「com.cn.skypiea.test" 
testInstrumentationRunner "android.test.InstrumentationTestRunner"

註釋以後,就能夠成功打包了。

出現Execution failed for task ':app:preDebugAndroidTestBuild的問題,百度以後找到一篇博客,Build->Rebuild Project以後便可正常運行
出現R標紅的問題

其實吧我以爲R挺玄乎的,我嘗試過好幾種解決辦法,第一次我從新運行了一遍而後R就自動不標紅了,反正就是挺奇怪的;第二次標紅我修改了package的內容而後就能夠了;第三次我採起了Ctrl+Enter新建一個東西就ok了,因此多嘗試。。。

最後我找到了一篇博客 Android studio中R變成紅色studio中R變成紅色供你們參考。。

相關文章
相關標籤/搜索