天天看點

模态對話框 和 非模态對話框

* 簡介:在項目開發時,經常會用到非模态對話框;雖然,可以用Popupwindow實作非模态對話框,但Popupwindow覆寫的部分不容易擷取事件,且混合使用比較麻煩。 下面就用Android自身的Dialog來實作模态和非模态對話框,以及與界面綁定的對話框。*

  1. 素材:
    模态對話框 和 非模态對話框
    模态對話框 和 非模态對話框
  2. 布局檔案wait_layer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center">


    <RelativeLayout android:layout_width="140dp"
                    android:layout_height="140dp"
                    android:background="@drawable/wait_layer_back"
            >
        <RelativeLayout android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center">
            <FrameLayout android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_centerHorizontal="true"
                         android:id="@+id/frame"
                    >
                <ImageView android:layout_width="100"
                           android:layout_height="100"
                           android:background="@drawable/ym_progress"
                           android:id="@+id/progress"
                        />
                <ImageView android:layout_width="100"
                           android:layout_height="100"
                           android:background="@drawable/ym_progress_icon"
                        />
            </FrameLayout>
            <TextView  android:id="@+id/tipText"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text="正在加載..."
                      android:textSize="16sp"
                      android:gravity="center"
                      android:textColor="#007E95"
                      android:layout_below="@+id/frame"
                      android:layout_marginTop="5dp"/>
        </RelativeLayout>
    </RelativeLayout>

</LinearLayout>
           
  1. anim檔案loading_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set
    android:shareInterpolator="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:duration="1000"
        android:startOffset="-1"
        android:repeatMode="restart"
        android:repeatCount="-1" />
</set> 
           

4.樣式

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--等待層-->
    <style name="loading_dialog" parent="android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
    <style name="loading_dialog_dim" parent="loading_dialog">

        <item name="android:backgroundDimEnabled">false</item>
    </style>
</resources>
           

5.主要代碼

package com.single.oto.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.single.oto.R;
import com.single.oto.activity.BaseActivity;

public class WaitLayer {
    private TextView tipTextView;
    private Dialog loadingDialog;
    private Animation hyperspaceJumpAnimation;
    private ImageView spaceshipImage;
    private DialogType dialogType = DialogType.MODALESS;
    private ViewGroup rootView;
    private View  view;
    public WaitLayer(Context context,DialogType dialogType) {
        this.dialogType = dialogType;
        creatDialog(context);
    }


    private void creatDialog(Context context) {
        if(view == null){
            view = LayoutInflater.from(context).inflate(R.layout.wait_layer,null);
        }
        hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context,
                R.anim.loading_animation);
        // 使用ImageView顯示動畫
        spaceshipImage = (ImageView)view.findViewById(R.id.progress);
        tipTextView = (TextView)view.findViewById(R.id.tipText);
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);

        if(dialogType == DialogType.NOT_NOMAL){//與界面綁定的對話框
            if(context instanceof  Activity){
                rootView = (ViewGroup)((Activity)context).findViewById(android.R.id.content);
            }else {
                Log.e("waitLayer:","params context is not Activity");
            }
        }else {
            if (dialogType == DialogType.MODAL){//模态
                loadingDialog = new Dialog(context, R.style.loading_dialog_dim);// 建立自定義樣式dialog
                Window window = loadingDialog.getWindow();
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.dimAmount = f;
       //       wl.alpha  = 0f;    //這句設定了對話框的透明度
                wl.flags = wl.flags | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
                wl.flags = wl.flags | WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                window.setAttributes(wl);
            }else {//非模态
                loadingDialog = new Dialog(context, R.style.loading_dialog);// 建立自定義樣式dialog
                loadingDialog.setCancelable(false);
            }
            loadingDialog.addContentView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        }
    }

    /**
     * 顯示等待層
     *
     */
    public void show() {
        show(null);
    }

    /**
     * 顯示等待層
     *
     * @param msg
     */
    public void show(String msg) {
        if(dialogType == DialogType.NOT_NOMAL){
            if(rootView.getChildAt(rootView.getChildCount() - ) != view){
                spaceshipImage.startAnimation(hyperspaceJumpAnimation);
                rootView.addView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
            }
        }else {
            if (loadingDialog.getWindow() != null
                    && !loadingDialog.getWindow().isActive()) {
                if (msg != null) {
                    tipTextView.setText(msg);// 設定加載資訊,否則加載預設值
                }else {
                    tipTextView.setText("");
                }
                if(!loadingDialog.isShowing()){
                    spaceshipImage.startAnimation(hyperspaceJumpAnimation);
                    loadingDialog.show();
                }
            }
        }
    }

    /**
     * 關閉等待層
     */
    public void dismiss() {
        if(dialogType != DialogType.NOT_NOMAL){
            if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        }else {
            if(rootView != null && view != null){
                rootView.removeView(view);
            }
        }
        hyperspaceJumpAnimation.cancel();
    }



    public  enum DialogType{
           MODAL,//模态
        MODALESS,//非模态
        NOT_NOMAL;//與界面綁定的對話框
    }

}



           

小結:在建立對話框是根據enum DialogType的值來建立您所需要的對話框。