Android教程(1) – HelloWorld及Android項目結構介紹

來源:互聯網
上載者:User

時間過的真快,斷斷續續做Android的事情也一年多了,剛開始為了參加Google的Android比賽和我哥們Zeaster一起做了一個軟體Zinfo,這個是一個SNS的構思,資訊共用平台,更多的內容可以去訪問我們的網站infosword
,不過去年就已經就停止開發了(木有賺到"刀了",只能先做點別的養家糊口咯-.-),如果有哪位兄弟對他感興趣,或者有更多更好的想法歡迎給我寫Email [haric.zhu@gmail.com],將來也許能重新整理整理繼續開發。

一直想寫部落格分享一下自己學習心得和開發經驗,卻一直因為懶惰而沒能堅持。因為一直在網上從各種大蝦那學習經驗,再潛水下去自己都覺得良心不安,所以打算在這裡把自己對Android的一些經驗和想法跟大家分享,也督促自己好好學習,天天向上。

 

[本文]

閑話少說,先來個加強版的HelloWorld介紹一下Android的概況和程式結構吧,這個HelloWorld程式很簡單很簡單,介面:

              


 

按一下按鈕,彈出一個對話方塊,裡面寫著Hello World! -.-

 

下面就按步驟走一下開發流程,在這個流程中我會詳細解釋Android的項目結構

1 安裝開發環境:

google推薦我們使用(Eclipse with the ADT plugin),ADT就是Android的開發外掛程式,提供了一些調試工具什麼的,在google code的android網站有詳細的介紹,按他標準來,我就不多嘴了[
http://code.google.com/android/intro/installing.html
]

2 建立項目:

開啟eclipse,建立項目->其他->然後選擇Android Project,這時候彈出來一個Title叫New Android Project的對話方塊,填寫如所示:

3 介紹項目結構:

大家看下面的項目結構圖示 :

 


 
 

src裡com.haric.hello下有一個HelloWorld.java,他的名字就來自於我們建立項目的時候填寫的Acivity name, 這個HelloWorld就繼承自Activity(Android Framework裡面最重要的一個類,詳細資料可以參考 -> (Activity
),  我們簡單地理解為它是一個UI的容器,直接跟使用者打交道最前端的類。

 

還有一個R.java,這個類是系統根據res檔案夾中的內容自動為你產生的,我們先講一下res檔案夾,res是resources的縮寫,顧名思義,你程式中所需要的文字,圖片,布局檔案等等資源都是放在這個檔案夾下面的,你現在看到這個檔案夾下面有
drawable   - 這個是放圖片的
layout       - 這個是放布局檔案的
values       - 下面放字串(strings.xml ),顏色(colors.xml ),數組(arrays.xml )

 

(res下的東東不止這些,更多關於Resouces的相容請參考(Resources
))

 

Android 幫我們把這些資源都管理起來,內容資源化的作用是很明顯的,做國際化方便了,使用同一個資源的時候也方便也更節省空間的(全域的引用),res檔案夾中內容變化,R.java都會重新編譯同步更新,所以這個類不需要你去手動更新了。

 

最後是AndroidManifest.xml. 你每次添加一個Acivity都需要在這個檔案中描述一下,這個檔案很重要東東還比較多,將來會詳細講述這個檔案的。(To Me:這個地方留個連結到時候鏈過去-.-)

 

4 設計介面布局:

接下來咱們要使用R.java了,首先我們在layout裡建立一個檔案hello_  world.xml, 編輯內容如下:

Xml代碼
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   
  8.     <Button android:id="@+id/Button01"  
  9.             android:text="@string/click_me"  
  10.             android:layout_width="wrap_content"  
  11.             android:layout_height="wrap_content">  
  12.     </Button>  
  13. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <Button android:id="@+id/Button01"            android:text="@string/click_me"            android:layout_width="wrap_content"            android:layout_height="wrap_content">    </Button></LinearLayout>

 有同學問了那個main.xml呢,我建議你把它的名字改成hello_world.xml或者刪刪掉新加一個hello_world.xml,[tips]
因為項目中會有很多個Activity,最好讓你的layout檔案的名字跟相應的Activity掛上鉤,方便將來編輯和使用。

 

做完這一步啦,結果是啥?肯定編譯不過唄,嘿嘿,認真的同學肯定注意到
"android:text="@string/click_me" 這行,這個是指定你Button上顯示的文字,我們需要在res/values/strings.xml中加一行<string name="click_me">Click me</string>這樣就能在layout檔案中引用這個string資源了,好了布局弄完了,該在程式中使用它了

 

5 在程式中使用布局:

開啟HelloWorld.java,編輯內容如下
 

Java代碼
 
  1. public void onCreate(Bundle savedInstanceState) {   
  2.        super.onCreate(savedInstanceState);   
  3.        setContentView(R.layout.hello_world);   
  4. }  
public void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.hello_world);}

 

  R.layout.hello_world 就指向 hello_world.xml,同理 R.string.click_me就向"Click

  me", 運行一下(右鍵點擊你的項目,run as -> Android Application)看到一個按鈕了吧

6 為按鈕添加點擊事件:
  要為按鈕添加點擊事件,我們需要先得到這個對象,然後設定監聽器,再編寫onClick事件
  完成後代碼如下:

  

Java代碼
 
  1. import android.app.Activity;   
  2. import android.app.AlertDialog;   
  3. import android.os.Bundle;   
  4. import android.view.View;   
  5. import android.view.View.OnClickListener;   
  6. import android.widget.Button;   
  7.   
  8. public class HelloWorld extends Activity {
      
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {   
  12.         super.onCreate(savedInstanceState);   
  13.         setContentView(R.layout.main);   
  14.            
  15.         Button button = (Button)findViewById(R.id.Button01);   
  16.         button.setOnClickListener(new OnClickListener(){   
  17.             public void onClick(View arg0) {   
  18.                 openDialog();   
  19.             }   
  20.         });   
  21.     }   
  22.        
  23.     private void openDialog(){   
  24.         AlertDialog.Builder builder = new AlertDialog.Builder(this);   
  25.         builder.setTitle("Hello");   
  26.         builder.setMessage("Hello World \n");   
  27.         builder.setNegativeButton("OK",null);   
  28.         builder.show();   
  29.     }    
  30. }  
import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class HelloWorld extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Button button = (Button)findViewById(R.id.Button01);        button.setOnClickListener(new OnClickListener(){public void onClick(View arg0) {openDialog();}        });    }        private void openDialog(){    AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Hello");builder.setMessage("Hello World \n");builder.setNegativeButton("OK",null);builder.show();    } }

 

Button button = (Button)findViewById(R.id.Button01);這句話就是用來擷取layout中設定的介面控制項對象的,這個id是在button中指定的android:id="@+id/Button01" 。

 

Android的UI用起來跟SWT是很像的,以後我會挑一些常用的,有趣的UI控制項詳細地介紹一下。今天先寫到這裡,每次我都會把相應項目的原始碼貼到最下面。

 

  • hello-world-plus.rar (24.6 KB)
  • 描述: 本文原始碼
  • 下載次數: 785
  • 大小: 16.4 KB
  • 大小: 5.2 KB
  • 大小: 31.4 KB
  • 查看圖片附件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.