Android中使用DialogFragment 来创建对话框

使用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 {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public 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 {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public 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 {

		@Override
		public void onClick(View arg0) {
			dismiss();
		}
		
	}

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

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值