剛開始接觸android,想寫弄個小玩意出來紅練習一下,剛好有一份獨數遊戲的教程,就先以這個小玩意練刀了。
先看下模擬器裡的效果吧:
^_^
VIEW SLIDE SHOW |
DOWNLOAD ALL |
|
安裝環境也是費了一些時間,從來未用過Java, 所以安裝過程我也記錄在了另一篇文章裡。第一次遇到的問題就是要寫介面,Android的介面用的是XML檔案的標識的, 大家都知道XML檔案的效率其實並不高的,呵呵Google這樣神一樣的公司當然也知道了,其實 Google是把XML檔案壓縮成二進位的放進apk裡的,XML只是用來在開發的時候用,所以效率就不是問題了。 順便介紹一個工具droiddraw畫介面比eclipse裡內建的那個要好很多。
按鈕的響應:
要在onCreate檔案裡添加:
1: View aboutButton = this.findViewById(R.id.btn_about);
2: aboutButton.setOnClickListener((OnClickListener) this);
然後寫onClick方法:
1: public void onClick(View v)
2: {
3: // TODO Auto-generated method stub
4: switch(v.getId())
5: {
6: case R.id.btn_about:
7: Intent iAbout = new Intent(this, About.class);
8: startActivity(iAbout);
9: break;
10: case R.id.btn_exit:
11: finish();
12: break;
13: case R.id.btn_newgame:
14: openNewGameDialog();
15: return;
16: }
17: }
顯示一個介面用了
Intent iAbout = new Intent(this, About.class); startActivity(iAbout);
關於 Intent 可以看看 Google的Dev Guide裡的介紹:
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:
*
action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
*
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
詳細可自行查閱…/android/content/Intent.html
訪問資源:
Android訪問資源還是很方便的,只要在res/目錄下添加好後就可以在代碼裡直接存取了,比如添加一個字串在res/values/strings.xml檔案裡添加一個<string name="easy_label">簡單</string>這樣的結點,在代碼裡用R.id.easy_label就可以得到“簡單”這個字串,夠簡單吧!
其它的類似。
AndroidManifest.xml
又是一個神奇的檔案,每一個activity要在這個檔案裡註冊一下,要不然啟動並執行時候會崩潰^_^
<activity android:name=".About"
android:label="@string/about_title"
android:theme="@android:style/Theme.Dialog">
</activity>
還可以指定介面的Style.
arrays.xml
可以把一個列表放在裡面,用R.array.XX 載入後會直接載入成數組,很方便。
遊戲的邏輯 就不講了,到處都是,我關心的是如何在Android上開發。
源碼我稍等會弄上來,歡迎大家拍磚、交流^_^!