Android中使用DialogFragment 来创建对话框

来源:互联网 发布:阴茎上白色米粒知乎 编辑:程序博客网 时间:2024/05/29 12:31

使用DialogFragment来管理对话框,当旋转屏幕和按下回退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期。且DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment(可以在大屏幕和小屏幕显示出不同的效果)。

使用DialogFragment创建对话框有两种方式:

   1、继承DialogFragment类并重写onCreateDialog方法,在其内部使用AlertDialog创建对话框,代码如下:

package com.mei.dialogtest;import android.app.AlertDialog;import android.app.Dialog;import android.app.DialogFragment;import android.content.DialogInterface;import android.os.Bundle;public class MyDialogFragment extends DialogFragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {// TODO Auto-generated method stub AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());         builder.setTitle(R.string.mydialog_title)         .setMessage(R.string.test_mydialog)         .setPositiveButton(android.R.string.ok,                 new DialogInterface.OnClickListener() {                         @Override                         public void onClick(DialogInterface dialog, int which) {                            dismiss();                         }                 })         .setNegativeButton(android.R.string.cancel,                 new DialogInterface.OnClickListener() {                         @Override                         public void onClick(DialogInterface dialog, int which) {                             dismiss();                         }                 });                  return builder.create();}}

   在需要出发对话框的地方调用:

protected void showMyDialog() {if (dialogFragment == null) {dialogFragment = new MyDialogFragment();dialogFragment.show(getFragmentManager(), "dialog");}}


   2、继承DialogFragment类并重写onCreateView方法,在其内部使用AlertDialog创建对话框,代码如下:

   首先创建dailog.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"    android:layout_width="160dp"    android:layout_height="220dp"    tools:ignore="MergeRootFrame" >        <span style="white-space:pre"></span><TextView             android:id="@+id/dialogTextView"            android:layout_width="160dp"            android:layout_height="160dp"            /><LinearLayout    android:orientation="horizontal"            android:layout_width="160dp"            android:layout_height="wrap_content" >        <Button             <span style="white-space:pre"></span>android:id="@+id/ok_button"            <span style="white-space:pre"></span>android:layout_width="80dp"            <span style="white-space:pre"></span>android:layout_height="wrap_content"            <span style="white-space:pre"></span>/>            <Button            <span style="white-space:pre"></span>android:id="@+id/cancel_button"            <span style="white-space:pre"></span>android:layout_width="80dp"            <span style="white-space:pre"></span>android:layout_height="wrap_content"            <span style="white-space:pre"></span>/> </LinearLayout>   </LinearLayout>
   继承DialogFragment类实现MyDialogFragment:

package com.mei.dialogtest;import android.app.DialogFragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MyDialogFragment extends DialogFragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View dialogView = inflater.inflate(R.layout.dailog, container);Button okButton = (Button)dialogView.findViewById(R.id.ok_button);Button cancelButton = (Button)dialogView.findViewById(R.id.cancel_button);TextView dialogText = (TextView)dialogView.findViewById(R.id.dialogTextView);okButton.setText(R.string.ok_str);okButton.setOnClickListener(new DialogButtonOnClickListener());cancelButton.setText(R.string.cancel_str);cancelButton.setOnClickListener(new DialogButtonOnClickListener());dialogText.setText(R.string.test_mydialog);return dialogView;}private final class DialogButtonOnClickListener implements OnClickListener {@Overridepublic void onClick(View arg0) {dismiss();}}}
    在需要出发对话框的地方调用:

protected void showMyDialog() {if (dialogFragment == null) {dialogFragment = new MyDialogFragment();dialogFragment.show(getFragmentManager(), "dialog");}}





0 0
原创粉丝点击