
DialogFragment的基本用法
1. 创建DialogFragment
public class DialogA extends DialogFragment implements DialogInterface.OnClickListener { @OverrIDe public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialoga_Title) .setPositivebutton(R.string.ok,this) .setNegativebutton(R.string.cancel,this); return builder.create(); } @OverrIDe public voID onClick(DialogInterface dialog,int ID) { switch(ID) { case AlertDialog.button_NEGATIVE: Toast.makeText(getActivity(),"Negative",Toast.LENGTH_SHORT).show(); break; case AlertDialog.button_POSITIVE: Toast.makeText(getActivity(),"Positive",Toast.LENGTH_SHORT).show(); break; default: break; } } }说明:自定义一个DialogFragment,并重写它的onCreateDialog()方法。
2. 调用该DialogFragment
下面是在FragmentActivity中调用该DialogFragment对话框。
public class DialogTest extends FragmentActivity { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); showDialog(); } private voID showDialog() { FragmentManager fm = getSupportFragmentManager(); DialogA dialoga = new DialogA(); dialoga.show(fm,"fragmenta"); }}自定义DialogFragment布局
下面介绍自定义DialogFragment的布局的方法
点击查看:自定义DialogFragment布局的完整代码
1. 设置布局文件
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <TextVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:hint="@string/dialoga_intro" /> <ImageVIEw androID:ID="@+ID/image" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/ic_action_vIDeo" /></linearLayout>
2. 使用布局
public class DialogA extends DialogFragment implements DialogInterface.OnClickListener { @OverrIDe public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); LayoutInflater inflater = getActivity().getLayoutInflater(); builder.setVIEw(inflater.inflate(R.layout.dialoga,null)) .setMessage(R.string.dialoga_Title) .setPositivebutton(R.string.ok,this); return builder.create(); } @OverrIDe public voID onClick(DialogInterface dialog,Toast.LENGTH_SHORT).show(); break; default: break; } }}DialogFragment和Activity的交互
下面介绍自定义DialogFragment和Activity交互的方法
点击查看:DialogFragment和Activity交互的完整代码
1. 定义通信接口
在DialogFragment中定义它们之间的通信接口。
public interface NoticeDialogListener { public voID onDialogPositiveClick(DialogFragment dialog); public voID onDialogNegativeClick(DialogFragment dialog);} // Use this instance of the interface to deliver action eventsNoticeDialogListener mListener;// OverrIDe the Fragment.onAttach() method to instantiate the NoticeDialogListener@OverrIDepublic voID onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (NoticeDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface,throw exception throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener"); } } 2. 在DialogFragment中调用该接口
@OverrIDepublic voID onClick(DialogInterface dialog,int ID) { switch(ID) { case AlertDialog.button_POSITIVE: //Toast.makeText(getActivity(),Toast.LENGTH_SHORT).show(); mListener.onDialogPositiveClick(DialogA.this); break; case AlertDialog.button_NEGATIVE: //Toast.makeText(getActivity(),Toast.LENGTH_SHORT).show(); mListener.onDialogNegativeClick(DialogA.this); break; default: break; } }3. 在Activity中实现该接口
public class DialogTest extends FragmentActivity implements DialogA.NoticeDialogListener { ... @OverrIDe public voID onDialogPositiveClick(DialogFragment dialog) { Toast.makeText(this,"Positive Callback",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onDialogNegativeClick(DialogFragment dialog) { Toast.makeText(this,"Negative Callback",Toast.LENGTH_SHORT).show(); }}Dialog与DialogFragment的对比
从代码的编写角度看,Dialog使用起来要更为简单,但是Google则是推荐尽量使用DialogFragment(对于AndroID 3.0以下的版本,可以结合使用support包中提供的DialogFragment以及FragmentActivity)。今天试着用这两种方式来创建对话框,发现DialogFragment果然有一个非常好的特性(在手机配置变化,导致Activity需要重新创建时,例如旋屏,基于DialogFragment的对话框将会由FragmentManager自动重建,然而基于Dialog实现的对话框则没有这样的能力)。
下面是两段实例代码:
他们使用的界面都一样:(dialog.xml)
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <ImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:src="@drawable/ic_launcher" /> </linearLayout>
1.基于Dialog实现的对话框
public class MainActivity extends Activity { private button clk; private Dialog dialog; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); clk = (button) findVIEwByID(R.ID.clk); dialog = new Dialog(this); dialog.setContentVIEw(R.layout.dialog); clk.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { dialog.show(); } }); }}当我们点击按钮时,会d出对话框(内容为androID logo),当我们旋转屏幕后,Activity重新创建,整个Activity的界面没有问题,而对话框消失了。
除此之外,其实还有一个问题,就是在logcat中会看到异常信息:AndroID..leaked .. window,这是因为在Activity结束之前,AndroID要求所有的Dialog必须要关闭。我们旋屏后,Activity会被重建,而上面的代码逻辑并没有考虑到对话框的状态以及是否已关闭。
于是将上述代码修改为:
public class MainActivity extends Activity { private button clk; private Dialog dialog; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); clk = (button) findVIEwByID(R.ID.clk); dialog = new Dialog(this); dialog.setContentVIEw(R.layout.dialog); clk.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { dialog.show(); } }); //用户恢复对话框的状态 if(savedInstanceState != null && savedInstanceState.getBoolean("dialog_show")) clk.performClick(); } /** * 用于保存对话框的状态以便恢复 */ @OverrIDe protected voID onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(dialog != null && dialog.isShowing()) outState.putBoolean("dialog_show",true); else outState.putBoolean("dialog_show",false); } /** * 在Activity销毁之前,确保对话框以关闭 */ @OverrIDe protected voID onDestroy() { super.onDestroy(); if(dialog != null && dialog.isShowing()) dialog.dismiss(); }}
2. 基于DialogFragment的对话框
与上面的对话框使用同样的界面布局,此处仅仅展现一个简单对话框,因此只重写了onCreateVIEw方法
public class MyDialogFragment extends DialogFragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw v = inflater.inflate(R.layout.dialog,container,false); return v; }}public class MainActivity extends FragmentActivity { private button clk; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); clk = (button) findVIEwByID(R.ID.clk); clk.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { MyDialogFragment mdf = new MyDialogFragment(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); mdf.show(ft,"df"); } }); }}
这两段代码可以实现第一种方式的同样功能,此处我们并没有去关心对话框的重建,以及Activity销毁前对话框是否已关闭,这一切都是由FragmentManager来管理。
其实DialogFragment还拥有fragment的优点,即可以在一个Activity内部实现回退(因为FragmentManager会管理一个回退栈)
以上是内存溢出为你收集整理的详解Android应用中DialogFragment的基本用法全部内容,希望文章能够帮你解决详解Android应用中DialogFragment的基本用法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)