編輯:進階開發
DialogFragment 是 Android 3.0 新增的 Fragment 子類,位於 android.app.DialogFragment 套件中。它讓對話框也能享有 Fragment 的優勢,但實作方式與傳統的 Dialog 略有不同。本文介紹兩種常見用法:一般對話框與類似 AlertDialog 的警示對話框。
public static class MyDialogFragment extends DialogFragment {
int mNum;
static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
int style = DialogFragment.STYLE_NORMAL, theme = 0;
switch ((mNum - 1) % 6) {
case 1: style = DialogFragment.STYLE_NO_TITLE; break;
case 2: style = DialogFragment.STYLE_NO_FRAME; break;
case 3: style = DialogFragment.STYLE_NO_INPUT; break;
case 4: style = DialogFragment.STYLE_NORMAL; break;
case 5: style = DialogFragment.STYLE_NORMAL; break;
}
switch ((mNum - 1) % 6) {
case 4: theme = android.R.style.Theme_Light; break;
case 5: theme = android.R.style.Theme; break;
}
setStyle(style, theme);
}
}
要特別注意:與 Activity 不同,Fragment 的版面配置是在 onCreateView() 中設定,透過 inflater 把 XML 版面映射成 View:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("Dialog #" + mNum + ": using style " + getNameForNum(mNum));
Button button = (Button) v.findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((FragmentDialog) getActivity()).showDialog();
}
});
return v;
}
在 Activity 中顯示對話框的 showDialog() 方法:
void showDialog() {
mStackLevel++;
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog) getActivity()).doPositiveClick();
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog) getActivity()).doNegativeClick();
}
})
.create();
}
}
在 Activity 中呼叫顯示:
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.show(getFragmentManager(), "dialog");
}
public void doPositiveClick() {
Log.i("FragmentAlertDialog", "Positive click!");
}
public void doNegativeClick() {
Log.i("FragmentAlertDialog", "Negative click!");
}
請留意兩者的分工:一般對話框透過 onCreateView() 設定自訂版面;而警示對話框則是重寫 onCreateDialog() 直接回傳一個 AlertDialog。掌握這個差別,就能依需求靈活選擇合適的實作方式。
Android 定位服務實戰:取得當前位置、追蹤更新與地址反查
Android 內建的位置 API 讓開發者能替應用加上「位置感知」能力,而不必深入研究底層的定位技術細節。在 Google Play 服務的加持下,自動定位追蹤、地理編
Android開發免費資源與工具整理:書籍、模擬器、UI設計與範例
免費資源是學習Android的加速器 Android開發門檻相對友好,原因之一是社群提供大量免費書籍、範例、模擬器與UI素材。對初學者而言,善用這些資源,可以快速建立開發
谷歌Android 2.0使用心得:版本節奏與NDK的取捨
Google正式推出Android 2.0系統的SDK(軟體開發套件,供開發人員除錯系統之用),這讓越來越普及的GPhone變得更好用,也大幅方便了使用者。 應用數量的
Android 原始碼結構(1):為什麼 Eclipse 說找不到源碼
簡介 用 Android SDK 開發應用時,我們常需要除錯,甚至要進到某個 API 函式內部去追。但如果目標版本的 SDK 沒有關聯對應版本的源碼,Eclipse 就會