- Android開發與實戰
- 趙書蘭編著
- 1319字
- 2018-12-30 05:24:08
1.6 Android的活動
要了解一個活動所經歷的各個階段,最好的辦法是創建一個新項目,實現各種事件,然后使活動經受各種用戶交互的考驗。
【例1-1】理解一個活動的生命周期。
【實現步驟】
(1)啟動Eclipse,創建一個名為Activities的工程。
(2)在MainActivity.java文件中添加下列加框的語句:


(3)按F11鍵在Android模擬器上調試應用程序。
(4)當活動第一次被加載時,應該可以在LogCat窗口中看到以下內容(單擊Debug透視圖):
08-07 03:41:27.779: DEBUG/Events(378):In the onCreate() event 08-07 03:41:27.779: DEBUG/Events(378):In the onStart() event 08-07 03:41:27.779: DEBUG/Events(378):In the onResume() event
(5)如果在Android模擬器上按Back按鈕,可觀察以下顯示內容:
08-07 03:55:27.779: DEBUG/Events(378):In the onPause() event 08-07 03:55:27.779: DEBUG/Events(378):In the onStop() event 08-07 03:55:27.779: DEBUG/Events(378):In the onDestroy() event
(6)按住Home按鈕不放,同時單擊Activities圖標可以看到以下內容:
08-07 04:05:27.779: DEBUG/Events(378):In the onCreate () event 08-07 04:05:27.779: DEBUG/Events(378):In the onStart () event 08-07 04:05:27.779: DEBUG/Events(378):In the onResume () event
(7)按下Android模擬器上的Phone按鈕,當前活動就會被推到后臺,觀察LogCat窗口中的輸出:
08-07 04:15:27.779: DEBUG/Events(378):In the onPause() event 08-07 04:15:27.779: DEBUG/Events(378):In the onStop() event
(8)注意,onDestroy()事件并沒有被調用,表明這個活動仍舊在內存中。按Back按鈕退出電話撥號程序,活動又再次顯示了。觀察LogCat窗口中的輸出:
08-07 04:18:27.779: DEBUG/Events(378):In the onCreate() event 08-07 04:18:27.779: DEBUG/Events(378):In the onStart () event 08-07 04:18:27.779: DEBUG/Events(378):In the onResume () event
1.6.1 應用活動的樣本與主題
默認情況下,一個活動占據整個屏幕。然而,也可以對活動應用一個對話框主題,使其顯示為一個浮動對話框。例如,打算定制一個活動,以彈出窗口的形式顯示它,用來提醒用戶執行的一些操作。在這種情況下,以對話框形式顯示活動來引起用戶的注意是個很好的方法。
要對活動應用對話框主題,只要修改AndroidManifest.xml文件中Activity元素,添加android:theme屬性即可:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.learn2develop.Activities" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" > <!-- android:theme="@android:style/Theme.Dialog" --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="9" /> </manifest>
這樣可以使用活動顯示一個對話框,如圖1-38所示。

圖1-38 顯示對話框
1.6.2 顯示對話框
用戶經常需要顯示一個對話框窗口,以便從用戶那里得到確認。這時,可以重寫在Activity基類中定義的受保護的onCreateDialog()方法來顯示一個對話框。
【實現步驟】
(1)在Eclipse環境下,創建一個名為ExampeDialog的工程。
(2)打開res/values目錄下的strings.xml,在其中輸入如下代碼。
<resources> <string name="app_name">ExampleDialog</string> <string name="hello_world">ExampleDialog</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">ExampleDialog</string> </resources>
(3)打開res/layout目錄下的main.xml文件,將其中已有的代碼替換為如下代碼。
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/btn_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click to display a dialog" /> </LinearLayout>
(4)打開src/com.example.exampledialog目錄下的MainActivity.java文件,將其中代碼代換為如下代碼。
package com.example.exampledialog; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.app.ProgressDialog; import android.os.Handler; import android.os.Message; public class MainActivity extends Activity { CharSequence[] items = { "Google", "IBM", "Microsoft" }; boolean[] itemsChecked = new boolean [items.length]; /** 當活動第一次被創建時調用 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.btn_dialog); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: return new AlertDialog.Builder(this) .setIcon(R.drawable.ic_action_search) .setTitle("這是一個帶簡單文本的對話框") setPositiveButton("確定", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "確定clicked!",Toast.LENGTH_SHORT).show(); } }); .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "取消clicked!",Toast.LENGTH_SHORT).show(); } }); .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), items[which]+(isChecked?" 選中!":"未選中!"), Toast.LENGTH_SHORT).show(); } } ) .create(); } return null; } }
按F11鍵在Android模擬器上調試應用程序。單擊“Click to display a dialog”按鈕得到如圖1-39所示的效果。

圖1-39 單擊按鈕顯示對話框
以上代碼使用setPositionButton()和setNegativeButton()方法分別設置了兩個按鈕:“確定”和“取消”。還可以通過setMultiChoiceItems()方法設置一個復選框列表供用戶選擇。對于setMultiChoiceItems()方法,需要傳入兩個數組:一個是要顯示的項列表,另一個包含了表明每個項是否被選中的值。當選中一個項時,使用Toast類來顯示一條信息,如圖1-40所示。

圖1-40 使用Toast類來顯示信息效果
1.6.3 顯示進度條對話框
除了前面介紹的普通對話框外,還可以創建進度條對話框。進度條對話框對于顯示一些活動的進度有用,如下載操作的狀態。
【實現步驟】
(1)使用1.6.2節創建的同一個工程,在MainActivity.java文件中添加以下加框代碼。



(2)按F11鍵在Android模擬器上對應用程序進行調試。單擊按鈕即可顯示進度條對話框,如圖1-41所示。注意觀察進度條將累計到100。

圖1-41 進度條對話框