Android 2.1學習筆記(一)

來源:互聯網
上載者:User

1. 環境搭建

Ubuntu+eclipse+ADT

2. Android 啟動很慢?

沒辦法,確實是很慢,等著吧,普遍說法是啟動第一次後不關閉,以後的就是部署了要快不少的,經測試,確實是要快一些的。最不能讓我接受的是居然連點擊Home鍵都是很慢的呢。

3.R.layout.main cannot be resolved

剛寫了一行就出了這麼個錯誤,讓我沒有一點脾氣的來尋找原因。

解決方案:

(1)刪除第三行的“import android.R” 註:我不知道是什麼時候加進來的。

(2)勾選Eclipse中的“Project->Build Automatically”

原因分析:

(1)刪除“import andorid R”之後工程就是從/res檔案夾下自動產生哦資源我呢間裡去解析了,否則它會從Android的資源類裡去找。

使用Eclipse進行Java開發,我們首先要注意到它的編譯是智能的,進入Preferences 並啟用動態構建(workbench>Build automatically)。這樣的話,當我們每次儲存檔案時,Eclipse將會自動編譯該文嘉以及依賴於該檔案的檔案。因此,當我們想運行應用程式的時候,它基本已經儲存好並且是最新版本了。在處理大型代碼量時,著可以節約不少時間。

4.關於布局

買來的書是基於以前版本的(Android SDK 1.0r2),對控制項的布局還是android:layout_x=”61px”,現在已經要android:layout_marginLeft=”61px” 這樣寫了,不然不起作用的。好奇的是還必須是px值,不然編譯器就報錯了,編譯器還能識別字串裡的內容!果然強大。

5. 要在TextView中顯示網址需要在TextView節點中加入android:autoLink = “all”

6. 常數(res\values) 這個檔案夾下定義項目中要用到的所有常數,預先定義好,是編程的一個好習慣。例如:

(1)定義顏色常量(color.xml)

<?xml version="1.0" encoding="utf-8" ><resources><drawable name="darkgray">#808080FF</drawable><drawable name="white">#FFFFFFFF</drawable></resources>

(2)定義字串常量(strings.xml)

<?xml version="1.0" encoding="utf-8" ><resources><string  name="hello">Hello World Android</drawable><string name="app_name">Test Android </drawable><string  name="str_id">帳號</drawable><string name="str_pwd">密碼</drawable></resources>

定義好了怎麼用呢,有兩種引用方式:

(1)標記引用:android:text=”@string/str_id”

(2)代碼引用:

Resources resources = getBaseContenxt().getResources();Drawable HippoDrawable = resources.getDrawable(R.drawable.white);//String aa = resources.getString(R.string.app_name);//String bb = getString(R.string.app_name);TextView tv= (TextView)findViewByID(R.id.text);tv.setBackground(HippoDrawable);// 當然可以通過使用graphics.Color 這個android預設的顏色來使用,但是只有12種常見的顏色/×Color.BLACKColor.BLUEColor.CYANColor.DKGRAYColor.GRAYColor.GREENColor.LIGRAYColor.MAGENTAColor.RENColor.TRANSPARENTColor.WHITEColor.YELLOW×/
7.擷取手機的解析度
這個就是Android與ios的不同,這個系統被不同的手機廠商使用,造出的機器也就大小各異了,而蘋果的系統只有他自己用,所以能做到統一。
DisplayMetrice dm = new DisplayMetrice();getWindowManager().getDefaultDisplay().getMetrice(dm);string strOpt="手機解析度為:"dm.widthPixels + " x " + dm.heightPixels;

8.Style樣式的定義

老是要一個個指定文字的大小,顏色也不是辦法的啊,所以也就有類似CSS樣式來登場了。樣式表檔案取名為:style.xml放在res/values/style.xml 位置

定義樣式樣本:

<?xml version="1.0" encoding="utf-8" ><resources><style name=DavidStyleText11><item name="android:textSize">18sp</item><item name="android:textColor">#EC9237</item></style><style name=DavidStyleText22><item name="android:textSize">18sp</item><item name="android:textColor">#EC9237</item><item name="android:fromAlphar">0.0</item><item name="android:toAlphar">0.0</item></style></resources>
怎麼用呢?下面為main.xml檔案中的兩個TextView應用不同的樣式。
<?xml version="1.0" encoding="utf-8" ><TextView style = "@style/DavidStyleTest11"/><TextView style = "@style/DavidStyleTest22"/>
8.頁面切換
(1)在同一個Activity 中利用setContentView來置換頁面(在res\layout\下定義main.xml 和mylayout.xml)
通過setContentView(R.layout.mylayout);setContentView(R.layout.main)來在兩個layout中切換。在此有個優點:即所有的程式裡的變數
都存在相同的狀態,無論是類成員變數,類函數等,皆可以在一個Activiy的狀態中直接取得,並沒有參數傳遞的問題。
(2)調用另一個Activity
/× new 一個 Intent 對象,並指定要啟動的Class×/Intent intent = new Intent();intent.setClass(TestAndroid.this,TestAndroid2.class);startActivity(intent);TestAndroid.this.finish();
當系統中新添加Activity時,必須在AndroidManifest.xml裡定義一個新的activity:
<activity android:name=”TestAndroid2”></activity>
 否則系統將會因為找不到Activity而發生編譯錯誤。
另外,當程式中出現兩個以上的Activity時,系統要決定主程式是哪一支(entry point)
<activity android:name=”.TestAndroid” android:lable="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.catetory.LAUNCHER"/></intent-filter></activity>
其中有一行為<category android:name="android.intent.catetory.LAUNCHER"/> ,這就代表程式啟動時候,會先啟動這個Activity,
而不是其他的,這個是必須定義一個的,如果xml中沒有一個定義了,那麼程式將不會啟動的。
(3)不同的Activity之間的資料傳遞
Intent intent = new Intent();intent.setClass(TestAndroid.this,TestAndroid2.class);//new 一個bundle對象,並將要傳遞的資料傳入Bundle bundle = new Bundle();bundle.putDouble("height",height);bundle.putString("Sex",sex);//將Bundle對象assign給Intentintent.putExtras(bundle);startActivity(intent);TestAndroid.this.finish();
那麼在TestAndroid2如何取得來自TestAndroid的資料呢,是這樣子的
//取得Intent中的Bundle對象Bundle bunde = this.getIntent().getExtras();String sex = bunde.getString("Sex");double height = bunde.getDouble("height");

這樣就取到資料了,除了上述簡單的傳遞類型之外,還有String[]和ArrayList<String>等封裝的方式。不知道能不能對Bundle 做C#中的擴充方法來傳遞自己的對象?需要研究一下。

(4)返回資料到前一個Activity

在第一個Activity這樣開始資料初始化

Intent intent = new Intent();intent.setClass(TestAndroid.this,TestAndroid2.class);//new 一個bundle對象,並將要傳遞的資料傳入Bundle bundle = new Bundle();bundle.putDouble("height",height);bundle.putString("Sex",sex);//將Bundle對象assign給Intentintent.putExtras(bundle);//調用Activity TestAndroid2startActivityForResult(intent,0);
在第二個Activity中取到資料後(按上一種方法取),使用完畢,這樣返回
//返回result 回上一個activityTestAndroid2.this.setResult(RESULT_OK, intent);//結束這個activityTestAndroid2.this.finish();

回到第一個Activity是這樣來取得返回的資料

//覆蓋 onActivityResult()Protcted void onActivityResult(int requestCode,int resultCode,Intent data){switch(resultCode){ case: RESULT_OK:Bundle bunde = data.getExtras();String sex=bunde.getString("Sex");double height = bunde.getDouble("height");...}...}

如果要在次頁面加上一個“回上頁”的按鈕,而非通過模擬器的回複鍵,且回上頁後又能保留之前輸入的相關資訊,那麼就必須使用startActivityForResult()來喚起一個Activity。利用這個方法,前一哦Activity1便會有一個等待次Activity2的返回,而返回的資料就可以達到我們想要的結果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.