Android自定义Dialog对话框的三种方法

 

自定义对话框是经常使用的功能,我们常用的弹窗操作,除了使用popwindow(popupwindow的简单实用)就是使用dialog来实现,这两种组件都支持之定义布局和功能来满足我们个性化的需求,也可以不采用自定义而直接使用系统封装好的api来实现功能。今天简单总结下在使用dialog做弹窗功能的方法和会遇到的问题与解决方案

方法一:直接使用系统的,不自定义布局和功能方式,效果如下图:

Android自定义Dialog对话框的三种方法_第1张图片

点击事件中代码如下:

AlertDialog.Builder dialog = new AlertDialog.Builder(GroupInfoActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("Something important");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //自己的逻辑
    }
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //自己的逻辑
    }
});
dialog.show();

方法二:效果如下图

Android自定义Dialog对话框的三种方法_第2张图片

代码如下:

    1,:自定义我们的dialog类如下所示  

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.im.R;

/**
 * 公用的Dialog,提示用户的一些操作
 * Created by AndyYuan on 2018/12/26.
 */

public class CommonDialog extends Dialog implements View.OnClickListener {
    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private String content;
    private OncloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;


    public CommonDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public CommonDialog(@NonNull Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;

    }

    public CommonDialog(@NonNull Context context, int themeResId, String content, OncloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    protected CommonDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public CommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CommonDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public CommonDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        contentTxt = (TextView) findViewById(R.id.content);
        titleTxt = (TextView) findViewById(R.id.title);
        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        contentTxt.setText(content);
        if (!TextUtils.isEmpty(positiveName)) {
            submitTxt.setText(positiveName);
        }

        if (!TextUtils.isEmpty(negativeName)) {
            cancelTxt.setText(negativeName);
        }

        if (!TextUtils.isEmpty(title)) {
            titleTxt.setText(title);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                if (listener != null) {
                    listener.onClick(false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onClick(true);
                }
                this.dismiss();
                break;
        }
    }

    public interface OncloseListener {
        void onClick(boolean confirm);
    }
}

2:定义公共的Dialog的布局:




    

    

    

    
    
    
    

    
    
    
    
    
    
    
    
    

    
    
    
    

    
    
    
    
    
    
    
    
    

    
    

        

        

        
    

3:在style里面定义我们自己的样式:

  


4:其中bg_round_white.xml文件如下:



    
    

5:调用方式很简单如下,我的是在一个点击事件里调用的如下代码:

new CommonDialog(GroupInfoActivity.this, R.style.commonDialog, "确定删除群聊天记录?",
        confirm -> {
            if (confirm) {
               这里都是自己的需要的逻辑操作......................
                Toast.makeText(GroupInfoActivity.this, "清空聊天记录成功", Toast.LENGTH_SHORT).show();
            }
        }).setTitle("提示").show();
方法三:采用自定义和功能方式效果也不错:

//初始化并弹出对话框方法
private void showDialog () {
    View view = LayoutInflater.from(this).inflate(R.layout.high_opinion_dialog_layout, null, false);
    final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();

    Button btn_cancel_high_opion = view.findViewById(R.id.btn_cancel_high_opion);
    Button btn_agree_high_opion = view.findViewById(R.id.btn_agree_high_opion);

    btn_cancel_high_opion.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferencesUnitls.setParam(getApplicationContext(), "HighOpinion", "false");
            //... To-do
            dialog.dismiss();
        }
    });

    btn_agree_high_opion.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //... To-do
            dialog.dismiss();
        }
    });

    dialog.show();
    //此处设置位置窗体大小,我这里设置为了手机屏幕宽度的3/4
    dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this) / 4 * 3), LinearLayout.LayoutParams.WRAP_CONTENT);
}

其中ScreenUtils工具类如下所示:

public class ScreenUtils {

    /**
     * 获取屏幕高度(px)
     */
    public static int getScreenHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 获取屏幕宽度(px)
     */
    public static int getScreenWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }

}
 

你可能感兴趣的:(布局的中等知识点,Dialog)