您的位置:首页 > 移动开发 > Android开发

Android中使用DialogFragment 来创建对话框

2014-11-07 17:15 441 查看
使用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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: