1. 程式人生 > >自學Android開發的入門過程

自學Android開發的入門過程

最近一週以來,在研究Android開發的入門,因為本身做java的,所以相對來說應該會比較容易。

習慣用的IDE是eclipse,所以自然就通過它來整合Android開發的環境。各種百度後確認了整合思路:下載sdk的自己的電腦,在IDE中安裝ADT外掛並指定sdk,通過ADV下載不同版本的tools,新建Android專案,編寫程式碼並執行。

一切看起來是那麼的自然,通暢。可但是,迎面撲來的事實像潤物細無聲的春雨灑向大地後長出來的竹筍,各種問題出現的也是那麼自然,那麼酣暢淋漓。

sdk下載的慢,adt下載的慢,tools下載的慢,sdk和adt之間的版本問題,tools不同版本對專案的影響,新建專案時選擇的版本導致R類的報錯……這還算是我能記住的,在實際中遇到,解決的小點已經記不得了。好在我都一一解決了,專案可以正常跑起來。但可是最讓我不能容忍的是:layout.xml檔案在eclipse中居然像青銅般倔強的不顯示視覺化控制元件,只能程式碼編輯。從功能上說並不影響什麼,可是我騙不了自己的心,不完美的東西總會讓人遺憾。已經到了這步,輕言放棄純屬扯淡。根據提示是說ADT版本低於sdk版本導致的。於是乎升級ADT,下載低版本tools別標記為首要任務,下載慢也認了。一晚後懷揣著理想的心再次開啟layout檔案,有種想罵人的衝動。不是說好的嘛,我ADT到24了,sdk降到23,為什麼還是提示adt過低。

於是乎,果斷轉投了Android studio。不是不能輕易放棄嗎,那個……呃,我是有理由的(說的再好聽也TM沒做成)

幸好之前用過idea,Android studio用起來還算順手。這個專門開發Android的軟體整合度很高,指定個sdk就行。直接建專案開始操作,反正到時候需要什麼它會提示下載。於是乎,按照之前想好的,新建了一個 計算器 的專案後,發現我靠,這樣一個專案是不是過於簡單,我可不是說只能執行個專案就算,我是要學開發的。於是乎就有了下面的專案目錄:


剛新建完成時,只有 MainActivity類 和 activity_main.xml檔案(其他東西暫時忽略,不懂,也用不到。)按照我的理解,這兩個加在一起就相當於網頁中的 js 和 html標籤。

相同的是:在js 和 MainActivity 中定義事件,在html 和 activity_main.xml 中建立佈局

不同的是:MainActivity是一個class,在其中指定是對應的哪個xml佈局檔案。

至於帶first,second的是我自建的,帶content的我都不知道怎麼出現的。

一個app的首頁就是 activity_main.xml 和 MainActivity 中定義的內容,相當於index.html。模擬機的頁面如下:


簡單,很簡單,它什麼都沒有,就一個能跳轉的控制元件。對應的 layout_main.xml 的程式碼是:

<?xml version="1.0" encoding="utf-8"?>
<!--線性佈局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
<!--按鈕控制元件,定義了其中的屬性-->
    <Button
        android:id="@+id/next_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="跳轉"
        android:textAlignment="center"
        android:textColor="@android:color/holo_red_light"
        android:textSize="30sp"
        android:textStyle="bold" />

</LinearLayout>
MainActivity類中的程式碼:
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 next = null;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//該方法指定activity與layout檔案的對應關係

        next = (Button) findViewById(R.id.next_to);//通過id查詢已經定義好的控制元件
        next.setOnClickListener(new OnClickListener() {//繫結監聽事件,內容為點選該控制元件跳轉到另一個activity,不傳參
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,FirstActivity.class);
                startActivity(intent);
            }
        });
    }

}
效果是點選 “跳轉” 按鈕,會調到另一個Activity中:包含 first 的,純跳轉,沒有引數,跳到的頁面:

這個裡面包含了對於基本佈局的應用,和 6 個控制元件(水平和垂直佈局中各有2個),對應的 activity_first.xml 內容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<!--第一個:水平-->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ui_basic_name"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ui_name"/>

    </LinearLayout>
<!--第二個:垂直-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/ui_basic_address"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ui_address"/>
    </LinearLayout>

    <Button
        android:id="@+id/just1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳轉到另一個Activity" />

    <Button
        android:id="@+id/nextapp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="啟動另一個app" />

</LinearLayout>
其中的 “姓名:”、“地址:” 不是寫死的,而是在values目錄中的 strings.xml 檔案中定義好的,如下:
<resources>
    
    <string name="app_name">Calputer</string>
    <string name="action_settings">Settings</string>
    
    <string name="ui_basic_name">姓名:</string>
    <string name="ui_basic_address">地址:</string>

</resources>
而 button 中的內容就是寫死的,使用 strings.xml 中預定義的也行。而 “白痴”、“上海” 則來自於FirstActivity中動態新增進去的,如下:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.*;

public class FirstActivity extends Activity {

    private Button activityButton = null;
    private Button app = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

//獲取已經在layout中定義好的控制元件,並新增資料
        TextView nameValue = (TextView) findViewById(R.id.ui_name);
        nameValue.setText("白痴");
        TextView addressValue = (TextView) findViewById(R.id.ui_address);
        addressValue.setText("上海");

//獲取該button,設定監聽事件內容為:跳轉到另一個activity
        activityButton = (Button) findViewById(R.id.just1);
        activityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(FirstActivity.this,SecondActivity.class);

                //用Bundle攜帶資料
                Bundle bundle=new Bundle();
                bundle.putString("test", "熊是怎麼死的?");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

//獲取該button,設定監聽事件內容為:啟動另一個自定義的app,同時傳了一個名為“edit”的引數
        app = (Button) findViewById(R.id.nextapp);
        app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);

                ComponentName cn = new ComponentName("cn.lung.otherapp","cn.lung.otherapp.MainActivity");

                intent.putExtra("edit","這就是我往另一個app傳的內容");
                intent.setComponent(cn);
                startActivity(intent);
            }
        });

    }}
其中不止填充了資料,還對兩個 button 分別設定了點選事件,用來:跳轉activity時傳參 和 啟動另外一個app

第一個button和之前主頁中的類似,只是多了一個要傳遞的引數:“test”,跳轉過去的 activity : second 如下:



可以看到該 activity 拿到了引數,至於它上面的 button 就是在 activity_second.xml 中之前定義的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mycompany.calputer.SecondActivity">

    <Button
        android:id="@+id/app1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="啟動系統自帶的程式:搜尋" />
    

    <TextView
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="@android:color/holo_orange_light"
        android:textSize="24sp"
        android:textStyle="bold" />


</LinearLayout>
這個只是定義佈局,引數則是在 SecondActivity 中接收的:
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity {

    private Button button = null;

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

       button = (Button) findViewById(R.id.app1);
       button.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
               intent.putExtra(SearchManager.QUERY,"想搜啥,趕緊的");
               startActivity(intent);
           }
       });

        Bundle bundle = getIntent().getExtras();//接收引數,並填充進指定控制元件
        String test = bundle.getString("test");
        TextView view = (TextView) findViewById(R.id.test);
        view.setText(test);
    }

}
至於其中的 button 控制元件,我新增的是事件是:點選後呼叫系統自帶的瀏覽器程式。後來我又加了一個呼叫撥號盤的功能,就是在 layout_second.xml 中加入下面程式碼:
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="啟動系統自帶程式:電話" />
    <!--通過autoLink屬性指定該連結為呼叫撥號盤功能,並傳引數-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="1856481" />
顯示頁面就是這樣了:(我把新控制元件夾在了原先兩個控制元件中間)



當點選搜尋控制元件時,就會呼叫了:

當點選電話下面的數字,會呼叫撥號盤:

至於啟動另一個app,在前面已經給出程式碼。先看一下直接開啟另一個App的 activity :


就是定義了兩個控制元件 : TextView(資料填充:“原住民”) 和 EditView (在該專案的 MainActivity 中,接收前面一個app傳來的資料動態填充)

被呼叫後開啟的 activity 頁面:


至此,app內不同 activity 的相互跳轉,引數傳遞,呼叫系統程式和其他app 的功能完畢,我知道還有其他的方法完成這樣的功能,但是目前還不會。

至於資料和服務端的互動,過段時間再研究一下


總結:

1.可以在模擬機中的 tool-option選項中更改連結來改善下載的速度

2.控制元件可以在layout佈局檔案中靜態建立,也可以在activity類中動態建立

3.如果給控制元件設立監聽事件,並且想要傳遞引數進去的話。可以通過自己實現onClickListener介面中的方法,自定義一個監聽

4.引數的傳遞可以通過 Bundle,也可以通過 Intent

LG