欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Dialog的封装

程序员文章站 2022-05-16 14:40:20
...

效果
Android Dialog的封装
常规使用:

                Dialog dialog=new Dialog(this,R.style.DialogStyle);
                View dilaogView = LayoutInflater.from(this).inflate(
                        R.layout.dialog_diylayout, null);
                dialog.setContentView(dilaogView);
                dialog.show();

如果需要设置dialog的弹出位置,则需要用到Window,通过setGravity来控制dialog位置

        Dialog  dialog = new Dialog(context, R.style.DialogStyle);
        Window win = dialog.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        // 设置弹出框的宽高
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        // 设置弹出框的位置
        win.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        win.setAttributes(lp);
        win.setContentView(R.layout.dialog_gravity);
        dialog.show();

之后就是封装Dialog了

我们把dialog的初始化,findView和设置点击事件都封装在一个类里,在给出一些set方法和接口

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.diydialog.R;

/**
 * Created by KID on 2018/1/18.
 */

public class DiyDialog {
    private Context context;
    private Dialog dialog;

    private TextView tv_dialog_content;
    private TextView tv_cancle;
    private TextView tv_ok;

    private String content;
    private String cancle;
    private String ok ;

    public DialogClickListener dialogClickListener;
    public void setDialogClickListener(DialogClickListener dialogClickListener){
        this.dialogClickListener=dialogClickListener;
    }

    public DiyDialog setContent(String content) {
        this.content = content;
        return this;
    }
    public DiyDialog setCancle(String cancle) {
        this.cancle = cancle;
        return this;
    }
    public DiyDialog setOk(String ok) {
        this.ok = ok;
        return this;
    }

    public DiyDialog(Context context){
        this.context=context;
    }
    public DiyDialog builder(){
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_diylayout, null);

        tv_dialog_content= (TextView) view.findViewById(R.id.tv_dialog_content);
        tv_cancle= (TextView) view.findViewById(R.id.tv_cancle);
        tv_ok= (TextView) view.findViewById(R.id.tv_ok);
        if(!TextUtils.isEmpty(content))tv_dialog_content.setText(content);
        if(!TextUtils.isEmpty(cancle))tv_cancle.setText(cancle);
        if(!TextUtils.isEmpty(ok))tv_ok.setText(ok);

        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(dialogClickListener!=null)dialogClickListener.cancle();
            }
        });
        tv_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(dialogClickListener!=null)dialogClickListener.ok();
            }
        });


        dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setContentView(view);

        return this;
    }
    public void show() {
        if(dialog!=null)dialog.show();
    }
    public void cancle() {
        if(dialog!=null)dialog.cancel();
    }


    public interface DialogClickListener{
        void cancle();
        void ok();
    }

}

使用这个封装dialog的代码

                final DiyDialog diyDialog =new DiyDialog(this);
                diyDialog.setCancle("No").setOk("Yes").setContent("Are you kidding me?");
                diyDialog.setDialogClickListener(new DiyDialog.DialogClickListener() {
                    @Override
                    public void cancle() {
                        diyDialog.cancle();
                    }
                    @Override
                    public void ok() {
                        diyDialog.cancle();
                        Toast.makeText(MainActivity.this, "ok哟呵呵呵呵", Toast.LENGTH_SHORT).show();
                    }
                });
                diyDialog.builder().show();

当然你也可以写成一整条链式过去,个人风格问题,比较喜欢分开写。

如果需要弹出动画

win.setWindowAnimations(R.style.mystyle);

    <style name="mystyle" parent="android:Animation">
        <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>  //进入时的动画
        <item name="@android:windowExitAnimation">@anim/dialog_exit</item>    //退出时的动画
    </style>

dialog_enter.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="100%p"
        android:duration="300" />
</set>

dialog_exit.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:toYDelta="100%p"
        android:duration="300" />
</set>

上面new Dialog的时候传入的 R.style.DialogStyle只是一个样式,非必传。不过建议还是加上,在styles.xml中插入

    <!-- Dialog的样式 -->
    <style name="DialogStyle" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

详细代码链接 http://download.csdn.net/download/qq_31390699/10211790