標(biāo)題: 第一個(gè)android程序:HelloWorld [打印本頁]

作者: xueren    時(shí)間: 2013-7-31 17:40
標(biāo)題: 第一個(gè)android程序:HelloWorld
    在前面我們已經(jīng)成功搭建了android開發(fā)環(huán)境,接下來我們就用Android的Eclipse插件ADT來創(chuàng)建第一個(gè)android程序:HelloWorld。相信大家對(duì)HelloWorld這個(gè)名字不會(huì)陌生。     1.選擇“文件”>“新建”>“Android Application Project”,打開“New Android Project”
           這里注意Application Name開頭一定要大寫,注意Package name must have at least two identifiers(我就不翻了),只寫helloworld是不行的。

next,next之后finish就行了
在這個(gè)過程中要設(shè)置Activity Name 和Layout Name,如圖

關(guān)于activity,大家要學(xué)好它的生命周期。這里大家先這樣理解:一個(gè)activity就是一個(gè)界面,當(dāng)大家點(diǎn)擊一個(gè)按鈕到另一個(gè)界面時(shí),就到了另一個(gè)activity。

點(diǎn)擊Finish之后我們就完成了一個(gè)最簡(jiǎn)單的Android應(yīng)用項(xiàng)目的創(chuàng)建,注意,到現(xiàn)在一句代碼都木有寫哦!

在“包資源管理器”中,展開HelloWorld,這里有許多目錄,下面我就給大家解釋一下它們各自的含義與作用。
   1. src目錄:源文件文件夾,各種代碼的編寫就是在這里完成的。
   2. gen目錄:自動(dòng)生成的R資源索引類文件夾,這里的東西是不能改動(dòng)的。
                       這里多說一句,建議大家每增加一個(gè)ID就將gen目錄刷新一下,這樣防止因?yàn)閑clpse沒有在gen目錄自動(dòng)生成而報(bào)錯(cuò)的麻煩,萬一報(bào)錯(cuò)那就一定是最后一個(gè)沒自動(dòng)生成
    3.Android2.3.3目錄:Android SDK jar文件。
    4.assets目錄:資源文件夾(這里的資源是不會(huì)在gen目錄中自動(dòng)生成ID的)。
    5.res目錄:資源文件夾 1.drawable-.....是不同分辨率的圖片文件所在地
                                        2.layout是布局文件,默認(rèn)只有一個(gè),程序員根據(jù)自己程序的需要可以添加更多的布局文件。
                                                     打開activity_main.xml如下
                                                     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                     xmlns:tools="http://schemas.android.com/tools"
                                                     android:layout_width="match_parent"               //設(shè)置寬的類型-跟隨父類,這里沒有父類,就默認(rèn)全屏
                                                     android:layout_height="match_parent" >          //設(shè)置高的類型-跟隨父類,這里沒有父類,就默認(rèn)全屏

                                                     <TextView                                
                                                                 android:layout_width="wrap_content"
                                                                 android:layout_height="wrap_content"
                                                                 android:layout_centerHorizontal="true"
                                                                 android:layout_centerVertical="true"
                                                                 android:padding="@dimen/padding_medium"
                                                                 android:text="@string/hello_world"          //引用hello_world字符串,即Hello world!
                                                                 tools:context=".MainActivity" />                 
                                                      </RelativeLayout>
                                        3.字符串:在values目錄下的string.xml文件中。
                                                         打開string.xml如下
                                                          <resources>
                                                                        <string name="app_name">HelloWorld</string>
                                                                        <string name="hello_world">Hello world!</string>
                                                                        <string name="menu_settings">Settings</string>
                                                                        <string name="title_activity_main">MainActivity</string>
                                                                 //<string name="所加的字符串">運(yùn)行時(shí)顯示在界面上的內(nèi)容</string>
                                                           </resources>
                                                      好處:可以創(chuàng)建多個(gè)string.xml,每個(gè)string.xml用不同語言,以方便不同國(guó)家的用戶使用。
  6.AndroidManifest.xml十分重要,內(nèi)容如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="h.elloworld"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />   //有了這句就確認(rèn)了第一個(gè)打開的activity
            </intent-filter>
        </activity>
    </application>
//在此處加應(yīng)用權(quán)限,如入網(wǎng)權(quán)限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
運(yùn)行結(jié)果如圖:

中間就是TextView控件了






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1