Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 中階開發

DialogFragment 完整示例程式碼

編輯:中階開發

作為 Android 3.0 的重要新特性,Fragment 相關內容 Android123 已用三篇文章做過介紹。如果你還不熟悉 Fragment,可以先參考《Fragment 對比 Activity - Android 碎片介紹》一文打好基礎。今天要分享的是 Fragment 子類 DialogFragment 的完整示例工程程式碼。

警告視窗 FragmentAlertDialog

  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>

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved