在Fragment子類別中包括了 ListFragment 、DialogFragment、PreferenceFragment 及 WebViewFragment,上一篇已經討論過ListFragment接著繼續探討DialogFragment應用。
DialogFragment是一個浮動在Activity上對話框形的Fragment,它有著類似Dialog(對話框)同樣的功能,經常使用在與使用者互動的畫面上;例如輸入帳號密碼、問題訊問回答、...等詳見Dialogs官方網頁說明。
另外,在另一個Dialogs官網上有建議AlertDialog(告警、詢問)、DatePickerDialog (年、月、日)及 TimePickerDialog(時間)等對話框(Dialog)建議改採繼承 DialogFragment 方式完成。
實作環境:
Win 7 Sp1
Java version 1.6.0_35
Eclipse 3.7.2
ADT 20.0.1
ASUS Nexus 7 平板 (4.1.2)
HTC Sensation 手機 (4.0.3)
步驟:
1. 用Eclipse建立一個Android 3.x項目以上專案並命名為DialogFragmentExample
2. 在 res/layout中加入一個XML檔案並且將其命名為fragment1.xml,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
3. main.xml內容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
4. 在src/ package目錄下增加 Fragment1.java,內容如下:
package net.learn2develop.DialogFragmentExample;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
public class Fragment1 extends DialogFragment {
static Fragment1 newInstance(String title) {
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString("title", title);
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
return new AlertDialog.Builder(getActivity()).setIcon(R.drawable.icon).setTitle(title)
.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity) getActivity()).doPositiveClick();
}
}).setNegativeButton("不", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((MainActivity) getActivity()).doNegativeClick();
}
}).create();
}
}
5. MainActivity內容如下:
package net.learn2develop.DialogFragmentExample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Fragment1 dialogFragment = Fragment1.newInstance("您是南開數創系的學生嗎?");
dialogFragment.show(getFragmentManager(), "dialog");
}
public void doPositiveClick() {
// ---perform steps when user clicks on OK---
Log.i("DialogFragmentExample", "是,我是南開數創系的學生!!");
}
public void doNegativeClick() {
// ---perform steps when user clicks on Cancel---
Log.i("DialogFragmentExample ", "不,我不是南開數創系的學生!!");
}
}
6. AndroidManifest.xml保持預設。
執行結果:
程式執行時會出現一個詢問對話框,當回答是/否後觀察DDMS上的Log會有不同的紀錄。
程式及文章參考來源:Beginning Android Tablet Application Development.
其他詳見官網 DialogFragment。
註:
1.網路上有一片關於DatePickerDialog是使用DialogFragment範例及程式碼,詳見:Displaying DatePickerDialog using DialogFragment in Android with backward compatibilty support library網頁。
2.在Android Developers官網中Pickers有說明到時間對話框的部分。
參考:
1.DialogFragment
http://developer.android.com/reference/android/app/DialogFragment.html
2.Dialog
http://developer.android.com/reference/android/app/Dialog.html
3.Dialogs
http://developer.android.com/design/building-blocks/dialogs.html
http://developer.android.com/guide/topics/ui/dialogs.html
4.Beginning Android Tablet Application Development
http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Android-Tablet-Application-Development.productCd-1118106733.html
5.Displaying DatePickerDialog using DialogFragment in Android with backward compatibilty support library
http://wptrafficanalyzer.in/blog/displaying-datepickerdialog-using-dialogfragment-in-android-with-backward-compatibilty-support-library/
6.Pickers
http://developer.android.com/guide/topics/ui/controls/pickers.html
==============延伸閱讀=====================
1.第一支Android Fragment程式--HelloFragment
http://cheng-min-i-taiwan.blogspot.tw/2012/04/android-fragment-hellofragment.html
2.Android Fragments 的生命週期
http://cheng-min-i-taiwan.blogspot.tw/2012/12/android-fragments.html
3.Fragment間的資料傳遞
http://cheng-min-i-taiwan.blogspot.tw/2013/01/fragment.html
4.Fragment與Activity間的資料傳遞
http://cheng-min-i-taiwan.blogspot.tw/2013/02/fragmentactivity.html
5.Fragment子類別之ListFragment
http://cheng-min-i-taiwan.blogspot.tw/2013/02/fragmentlistfragment.html
6.Fragment子類別之DialogFragment
http://cheng-min-i-taiwan.blogspot.tw/2013/02/fragmentdialogfragment.html
7.Fragment子類別之PreferenceFragment
http://www.cheng-min-i-taiwan.blogspot.tw/2013/02/fragmentpreferencefragment.html
8.Fragment子類別之WebViewFragment
http://cheng-min-i-taiwan.blogspot.tw/2013/02/fragmentwebviewfragment.html
沒有留言:
張貼留言