使用DialogFragment托管dialog

普通的 AlertDialog 在横竖屏切换时会被销毁,如果dialog上面有数据,也将丢失。解决方案是使用DialogFragmenthttp://developer.android.com/reference/android/app/DialogFragment.html) 

使用 dialogFragment 通常需要复写两个方法:


onCreateView(LayoutInflater, ViewGroup, Bundle)onCreateDialog(Bundle)


如果你想自定义dialog样式,只需要复写 onCreateView ,注入一个自定义的view即可,然后通过调用DialogFragment#show() 方法即可。

这里我们不需要自定义,只需要托管 AlertDialog 即可,所以我们仅仅需要复写 onCreateDialog 方法。在这个方法内部我们需要通过 AlertDialog.Builder 构建一个dialog并返回,dialog的参数可以通过 setArguments 注入。具体代码如下:

[Java]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.taobao.dialogfragmentdemo;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class AlertDialogFragment extends DialogFragment
{
   private static final String PARAM_TITLE = "title" ;
   private static final String PARAM_CONTENT = "content" ;
   private static DialogCallback mCallback;
   public AlertDialogFragment()
   {
   }
   public static AlertDialogFragment newInstance(String title,String content,DialogCallback callback)
   {
     AlertDialogFragment instance = new AlertDialogFragment();
     Bundle bundle = new Bundle();
     bundle.putString(PARAM_TITLE,title);
     bundle.putString(PARAM_CONTENT,content);
     instance.setArguments(bundle);
     mCallback = callback;
     return instance;
   }
   @NonNull
   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState)
   {
     Bundle params = getArguments();
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     builder.setTitle(params.getString(PARAM_TITLE)); //没有做非空判断,按需添加
     builder.setMessage(params.getString(PARAM_CONTENT));
     builder.setPositiveButton( "确定" , new DialogInterface.OnClickListener()
     {
       @Override
       public void onClick(DialogInterface dialog, int which)
       {
         if (mCallback != null )
           mCallback.onPostiveClick();
       }
     });
     builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener()
     {
       @Override
       public void onClick(DialogInterface dialog, int which)
       {
         if (mCallback != null )
           mCallback.onNegativeClick();
       }
     });
     return builder.show();
   }
   public interface DialogCallback
   {
     public void onPostiveClick();
     public void onNegativeClick();
   }
}

想使用也很简单:

[Java]  纯文本查看  复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
AlertDialogFragment dialog = AlertDialogFragment.newInstance( "标题" , "这是fragment托管的alertdialog" , new AlertDialogFragment.DialogCallback()
{
   @Override
   public void onPostiveClick()
   {
     Toast.makeText(MainActivity. this , "确定" ,Toast.LENGTH_SHORT).show();
   }
   @Override
   public void onNegativeClick()
   {
     Toast.makeText(MainActivity. this , "取消" ,Toast.LENGTH_SHORT).show();
   }
});
dialog.show(getSupportFragmentManager(), "dialog" );

还可以通过 DialogFragment#show 的第二个参数tag来找到对应的dialogFragment:

[Java]  纯文本查看  复制代码
?
1
2
3
4
5
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment prevDialog = getSupportFragmentManager().findFragmentByTag( "dialog" );
  if (prevDialog != null )
  {
       transaction.remove(prevDialog);
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值