转载:http://blog.csdn.net/lmj623565791/article/details/37815413
1、 概述
DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。
2、 好处与用法
使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期。且DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment(可以在大屏幕和小屏幕显示出不同的效果)。上面会通过例子展示这些好处~使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。onCreateView即使用定义的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。
3,重写onCreatView创建Dialog
创建弹窗dialog
public class ConcreteDialogFragment extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.dialog_fragment,null);
return view;
}
}
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name_ed"
android:minEms="5"
android:hint="EditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
activity中点击按钮触发:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ConcreteDialogFragment dialogFragment=new ConcreteDialogFragment();
FragmentManager manager=getSupportFragmentManager();
dialogFragment.show(manager, "DialogFragmentActivity");
}
});
运行结果如下:
去掉标题:
public class ConcreteDialogFragment extends DialogFragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.dialog_fragment,null);
<strong>getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);</strong>
return view;
}
}
4,在onCreateDialog中 创建Dialog
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=LayoutInflater.from(getActivity());
AlertDialog alertDialog=builder.setView(inflater.inflate(R.layout.dialog_fragment,null))
.setPositiveButton("Sign In", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Sign In", Toast.LENGTH_SHORT).show();
}
}).create();
return alertDialog;
}
示意图:
5,传递数据给Activity
使用接口:
在fragment定义接口
TranslateListener translateListener;
@Override
public void onAttach(Activity activity) {
<strong>translateListener=(TranslateListener) activity;</strong>
super.onAttach(activity);
}
<strong>public interface TranslateListener{
void showMsg(String msg);
}</strong>
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=LayoutInflater.from(getActivity());
AlertDialog alertDialog=builder.setView(inflater.inflate(R.layout.dialog_fragment,null))
.setPositiveButton("Sign In", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
<strong>translateListener.showMsg("translateListener");</strong>
Toast.makeText(getActivity(), "Sign In", Toast.LENGTH_SHORT).show();
}
}).create();
return alertDialog;
}
activity实现接口:
public class DialogFragmentActivity extends BaseActivity <strong>implements TranslateListener</strong>{
<strong>@Override
public void showMsg(String msg) {
showToast(this, msg);
</strong>}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) findViewById(R.id.button01);
button.setText("show DialogFragment");
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ConcreteDialogFragment dialogFragment=new ConcreteDialogFragment();
FragmentManager manager=getSupportFragmentManager();
dialogFragment.show(manager, "DialogFragmentActivity");
}
});
}
}