編輯:中階開發
作為 Android 3.0 的重要新特性,Fragment 相關內容 Android123 已用三篇文章做過介紹。如果你還不熟悉 Fragment,可以先參考《Fragment 對比 Activity - Android 碎片介紹》一文打好基礎。今天要分享的是 Fragment 子類 DialogFragment 的完整示例工程程式碼。
public class FragmentAlertDialog extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_dialog);
View tv = findViewById(R.id.text);
((TextView)tv).setText("android123-DialogFragment Samples");
Button button = (Button)findViewById(R.id.show);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog();
}
});
}
void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance( "Info");
newFragment.show(getFragmentManager(), "Title");
}
public void doPositiveClick() {
Log.i("FragmentAlertDialog", "左鍵按下");
}
public void doNegativeClick() {
Log.i("FragmentAlertDialog", "右鍵按下");
}
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.icon)
.setTitle(title)
.setPositiveButton("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();
}
}
}
上面涉及的 fragment_dialog.XML 布局檔程式碼為:
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="top|center_horizontal" />
<Button android:id="@+id/show"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/show">
<requestFocus />
</Button>
</LinearLayout>
Android 自定義按鈕:背景、9-patch 與異形按鈕實作
在 Android 應用開發中,系統預設的 Button 樣式往往較為制式。若想提升產品的視覺質感與操作體驗,通常需要自定義按鈕背景、形狀與狀態變化。本文整理三種常見做法
Fragment 對比 Activity - Android 碎片介紹
Fragment 是 Android Honeycomb 3.0 新增的概念,名字雖叫「碎片」,卻和 Activity 十分相似。下面 Android123 來介紹 An
用 AIDL 打通 Service 與 Activity:讓 Service 主動回呼 Activity
AIDL 一直看起來有點玄,實際動手之後才發現思路其實很單純:Service 想呼叫 Activity 的方法,靠的是一組「反向的 Binder」。Activity 把自
HTML5 在 Android 上的視覺化 UI:Canvas、CSS3 與表單新特性
HTML5 與行動瀏覽器 HTML5 不只是新的標記語言,也整合了 JavaScript 與 CSS 的新能力。對於 iPhone 與 Android 等行動裝置,Can