android通用的Dialog

android开发中一般需要设计到dialog的显示,而dialog的显示需求可能多种多样,我们可以把这种变化封装到一个基类中提供给其他人使用,先看代码:

package com.example.tools;

import com.example.demo.R;
import com.example.demo.application.BaseApplication;
import android.app.Dialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


/**
 * Created by baiting on 2016/2/22.
 */
public class CommonDialog  {

    protected int STYLE_NONE=0;
    protected int GRAVITY_NONE=0;
    protected int WIDTH=500;
    protected int HEIGHT=300;

    private int width=0;
    private int height=0;

    private boolean isCanceled=true;

    private Dialog dialog;

    private String message;

    private View view;

    private int gravity=GRAVITY_NONE;

    private int style=STYLE_NONE;

    private onBackListener mOnBackListener;
    private onSearchListener mOnSearchListener;
    private onDialogDismissListener mOnDialogDismissListener;

    public CommonDialog() {

    }

    public CommonDialog setOnBackListener(onBackListener mOnBackListener) {
        this.mOnBackListener = mOnBackListener;
        return this;
    }

    public CommonDialog setOnSearchListener(onSearchListener mOnSearchListener) {
        this.mOnSearchListener = mOnSearchListener;
        return this;
    }

    public CommonDialog setOnDialogDismissListener(
            onDialogDismissListener mOnDialogDismissListener) {
        this.mOnDialogDismissListener = mOnDialogDismissListener;
        return this;
    }

    public CommonDialog setWidth(int width) {
        this.width = width;
        return this;
    }

    public CommonDialog setHeight(int height) {
        this.height = height;
        return this;
    }

    public CommonDialog setView(View view) {
        this.view = view;
        return this;
    }

    public CommonDialog setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public CommonDialog setStyle(int style) {
        this.style = style;
        return this;
    }

    public CommonDialog setMessage(String message) {
        this.message=message;
        return this;
    }

    public CommonDialog setCanceled(boolean flag) {
        isCanceled=flag;
        return this;
    }

    public void onCreateDialog(Context context) {
        dialog = new Dialog(BaseApplication.getInstance(),
                    style<=STYLE_NONE?R.style.ParentStyle_NormalDialog:style);

        dialog.setContentView(view==null?createView(context):view);

        WindowManager.LayoutParams lp=dialog.getWindow().getAttributes();
        lp.width=width>0?width:WIDTH;
        lp.height=height>0?height:HEIGHT;
        dialog.getWindow().setAttributes(lp); 

        dialog.getWindow().setGravity(gravity<=GRAVITY_NONE?
                Gravity.CENTER:gravity);

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if(mOnDialogDismissListener!=null)
                    mOnDialogDismissListener.onDismiss(dialogInterface);
            }
        });
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if(mOnBackListener!=null)
                        mOnBackListener.onBack(dialogInterface);
                    return !isCanceled;
                }
                else if(keyCode==KeyEvent.KEYCODE_SEARCH) {
                    if(mOnSearchListener!=null)
                        mOnSearchListener.onSearch(dialogInterface);
                    return !isCanceled;
                }
                else
                    return false;
            }
        });

        dialog.setCanceledOnTouchOutside(isCanceled);
        dialog.setCancelable(isCanceled);
    }


    public interface onBackListener{
        void onBack(DialogInterface dialogInterface);
    }

    public interface onSearchListener{
        void onSearch(DialogInterface dialogInterface);
    }

    public interface onDialogDismissListener{
        void onDismiss(DialogInterface dialogInterface);
    }

    protected View createView(Context context) {
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_confirm,null);
        TextView tv_content= (TextView) view.findViewById(R.id.confirm_content);
        if(!TextUtils.isEmpty(message))
            tv_content.setText(message);
        Button cancel= (Button) view.findViewById(R.id.confirm_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissDialog();
            }
        });
        Button ok=(Button) view.findViewById(R.id.confirm_sure);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissDialog();
            }
        });
        return view;
    }


    public void show(Context context) {
        if(dialog==null)
            onCreateDialog();
        dismissDialog();
        dialog.show();
    }

    public void dismissDialog() {
        if(dialog!=null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void closeDialog() {
        dismissDialog();
        dialog=null;
    }

}

代码中涉及到的布局文件:

<?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:gravity="center_horizontal"
    android:orientation="vertical">


    <TextView
        android:id="@+id/confirm_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:gravity="center"
        android:text=""
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/confirm_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"
            />

        <Button
            android:id="@+id/confirm_sure"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:text="确定"
            />

    </LinearLayout>


</LinearLayout>

style属性:

<style name="ParentStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

    <style name="ParentStyle.NetworkProgressbar">
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowIsTranslucent">false</item>
    </style>

    <style name="ParentStyle.NormalDialog">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

通过这种链式方法的封装的dialog,别人使用起来会非常方便。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值