解鎖Android Dialog的各種使用姿勢

前言

在Android開發(fā)中,Dialog被使用的非常頻繁,因此這里總結(jié)了一下Dialog的幾種使用方式

方式一 Android自帶Dialog---AlertDialog

  • AlertDialog底層采用了構(gòu)造者模式,通過構(gòu)造者在創(chuàng)建AlertDialog對象,全程采用鏈式調(diào)用的方法,非常的方便
  1. item_mydialog.xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@drawable/shape_mydialog"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_height="wrap_content">
    
    
    <TextView
     android:id="@+id/tv_title"
     android:text="標題"
     tools:text="標題"
     android:layout_marginStart="10dp"
     android:textColor="@android:color/holo_green_light"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    
    <TextView
     android:id="@+id/tv_msg"
     android:text="這是提示的內(nèi)容"
     android:layout_marginStart="10dp"
     android:layout_marginTop="10dp"
     tools:text="這是提示的內(nèi)容"
     android:textColor="@android:color/black"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    
    <LinearLayout
     android:layout_marginTop="10dp"
     android:layout_gravity="end"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
    <TextView
     android:id="@+id/tv_confirm"
     android:text="確定"
     android:layout_marginEnd="10dp"
     tools:text="確定"
     android:textColor="@android:color/holo_green_light"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    
    <TextView
     android:id="@+id/tv_cancel"
     android:text="取消"
     android:layout_marginEnd="10dp"
     tools:text="取消"
     android:textColor="@android:color/holo_green_light"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    </LinearLayout>
    
    </LinearLayout>
    

2.在適當?shù)牡胤竭M行如下調(diào)用

new AlertDialog.Builder(this)
            .setView(R.layout.item_mydialog)//此處也可以傳入一個View,根據(jù)構(gòu)造方法來,傳入View可以對View的子控件進行相關(guān)操作
            .show();

效果圖:


效果圖

方式二 繼承Dialog類,自定義Dialog

  1. Xml布局使用的是方式一中的

  2. MyDialog代碼如下

    public class MyDialog extends Dialog{
    
    private TextView tvTitle,tvMsg,tvConfirm,tvCancel;
    
    private OnMyDialogClickListener onMyDialogClickListener;
    
    public MyDialog setOnMyDialogClickListener(OnMyDialogClickListener onMyDialogClickListener) {
     this.onMyDialogClickListener = onMyDialogClickListener;
     return this;
    }
    
    public MyDialog(@NonNull Context context) {
     super(context, R.style.MyDialogStyle);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.item_mydialog);
    
     initView();
    
    }
     /**
      * 初始化
      */
    private void initView() {
      tvTitle = findViewById(R.id.tv_title);
      tvMsg = findViewById(R.id.tv_msg);
      tvConfirm = findViewById(R.id.tv_confirm);
      tvCancel = findViewById(R.id.tv_cancel);
      tvConfirm.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             dismiss();
             if(onMyDialogClickListener != null){
                 onMyDialogClickListener.onConfirmClick(MyDialog.this);
             }
         }
     });
    
     tvCancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             dismiss();
             if(onMyDialogClickListener != null){
                 onMyDialogClickListener.onCancelClick(MyDialog.this);
             }
         }
     });
    
    
     setCanceledOnTouchOutside(true);
     setCancelable(true);
     }
    
    
    
    public MyDialog setTitle(String title){
     tvTitle.setText(title);
     return this;
    }
    
    public MyDialog setMsg(String msg){
     tvMsg.setText(msg);
     return this;
     }
    
    
    
    @Override
    protected void onStart() {
     super.onStart();
     //設(shè)置Dialog顯示大小位置動畫等操作
     Window window = getWindow();
     Display display = window.getWindowManager().getDefaultDisplay();
     window.setLayout((int) (display.getWidth()*0.9),WindowManager.LayoutParams.WRAP_CONTENT);
    }
    
    public interface OnMyDialogClickListener{
     void onConfirmClick(Dialog dialog);
    
     void onCancelClick(Dialog dialog);
    }
    }
    
  3. MyDialogStyle樣式

    <style name="MyDialogStyle" parent="@android:style/Theme.Dialog">
     <item name="android:windowNoTitle">true</item>
     <item name="android:windowIsFloating">true</item> <!--這個說明提示框是否是浮動的-->
     <item name="android:windowIsTranslucent">true</item> <!--這個說明提示框是否是透明的-->
     <item name="android:windowBackground">@android:color/transparent</item><!--這個說明提示框的背景顏色是什么-->
     <item name="android:backgroundDimEnabled">true</item><!--這個說明是否充許對話框的背景變暗。為true則充許變暗-->
    </style>
    

4.在適當?shù)奈恢眠M行如下調(diào)用即可

MyDialog myDialog = new MyDialog(this);
 if(myDialog.isShowing()){
      myDialog.dismiss();
  }else {
      myDialog.show();
  }

效果圖:


效果圖

注意

以上兩種方式創(chuàng)建的Dialog,官方并不推薦使用,因為沒有生命周期管理,所以我們必須在使用完,或退出界面時手動對Dialog進行銷毀

方式三 繼承DialogFragment創(chuàng)建對話框

概述

  • DialogFragment在android 3.0時被引入。是一種特殊的Fragment,用于在Activity的內(nèi)容之上展示一個模態(tài)的對話框。典型的用于:展示警告框,輸入框,確認框等等。
  • 在DialogFragment產(chǎn)生之前,我們創(chuàng)建對話框:一般采用AlertDialog和Dialog。注:官方不推薦直接使用Dialog創(chuàng)建對話框。

好處與用法

  • 使用DialogFragment來管理對話框,當旋轉(zhuǎn)屏幕和按下后退鍵時可以更好的管理其聲明周期,它和Fragment有著基本一致的生命周期。且DialogFragment也允許開發(fā)者把Dialog作為內(nèi)嵌的組件進行重用,類似Fragment(可以在大屏幕和小屏幕顯示出不同的效果)
  • 使用DialogFragment至少需要實現(xiàn)onCreateView或者onCreateDIalog方法。onCreateView即使用定義的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog創(chuàng)建出Dialog。

使用方式

  1. 重寫onCreateView創(chuàng)建Dialog
  • Xml布局使用方式一的

  • 繼承DialogFragment,重寫onCreateView()方法

    public class MyCommonFragmentDialog extends DialogFragment{
    
    private View mView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      mView = inflater.inflate(R.layout.item_mydialog,container,false);
      return mView;
    }
    
    
    @Override
    public void onStart() {
      super.onStart();
      Window window = getDialog().getWindow();
      if(window != null){
          Display defaultDisplay = window.getWindowManager().getDefaultDisplay();
          window.setLayout((int) (defaultDisplay.getWidth()*0.9), WindowManager.LayoutParams.WRAP_CONTENT);
      }
    
    }
    }
    
  • 在合適的位置進行如下調(diào)用

    MyCommonFragmentDialog myCommonFragmentDialog = new MyCommonFragmentDialog();
    myCommonFragmentDialog.show(getSupportFragmentManager(),"");
    

效果圖:


效果圖
  1. 重寫onCreateDialog創(chuàng)建Dialog
  • Xml布局使用方式一的

  • 繼承DialogFragment,重寫onCreateDialog()方法

    public class MyFragmentDialog extends DialogFragment {
    
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
              .setView(R.layout.item_mydialog)
              .create();
      return alertDialog;
    }
    }
    
  • 在合適的位置進行如下調(diào)用

    MyFragmentDialog myFragmentDialog = new MyFragmentDialog();
    myFragmentDialog.show(getSupportFragmentManager(),"");
    

效果圖:


效果圖

使用心得

  • 如果Dialog界面比較簡單,通常會選擇前兩種方式來創(chuàng)建Dialog,但是要注意在合適的地方對Dialog進行銷毀
  • 如果Dialog界面比較復(fù)雜,而且涉及很多與Activity傳值等問題,建議使用繼承DialogFragment并重寫onCreateView()方法的方式來創(chuàng)建Dialog,這樣子我們就可以像使用普通Fragment的方式來使用Dialog,易于管理

如果對我的文章感興趣的話,請為我點贊,謝謝!!!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容