Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> DialogFragment示例代碼

DialogFragment示例代碼

編輯:中級開發

Fragment作為Android 3.0的重要新特性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="http://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