自定义dialog以及使用

注:本文绝大部分参考于:https://www.jianshu.com/p/64446940eccf。

这位作者讲的十分详细,有自定义的一部分,以及封装dialog的代码,我按照她的文章实现了自己的需求但是调用的时候,对我来讲报错了,一直在解决bug,后来是在另一篇优秀的文章上找到解决办法,这个是直接自定义及现成的调用代码,对调用来讲,是非常实用的。链接:点击打开链接

强烈建议朋友们结合一起参考学习!!顺便灰常感谢他们的分享。

直接上代码了:

1.先自定义好对话框的布局  :correct.xml 



    
    
    

效果如图:(忽然发现我们截图工具,等待会补上)

2.新建一个java 文件,CustomDialog.java    (详细讲解都在第一个链接里)

package cn.demo.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by ab on 2018/4/18.
 */
public class CustomDialog extends Dialog
{
    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;
    private CustomDialog(Builder builder)
    {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }
    private CustomDialog(Builder builder, int resStyle)
    {
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view; }
    @Override
    protected void onCreate(Bundle savedInstanceState)
    { super.onCreate(savedInstanceState);
        setContentView(view);
        setCanceledOnTouchOutside(cancelTouchout);
        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.height = height; lp.width = width;
        win.setAttributes(lp);
    }
    public static final class Builder {
        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view; private int resStyle = -1;
        public Builder(Context context)
        {
            this.context = context;
        }
        public Builder view(int resView)
        {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this; }
        public Builder heightpx(int val)
        {
            height = val;
            return this;
        }
        public Builder widthpx(int val)
        {
            width = val;
            return this;
        }
        public Builder heightdp(int val)
        {
            height = DensityUtil.dip2px(context, val);
            return this;
        }
        public Builder widthdp(int val)
        {
            width = DensityUtil.dip2px(context, val);
            return this;
        }
        public Builder heightDimenRes(int dimenRes)
        {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }
        public Builder widthDimenRes(int dimenRes)
        {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }
        public Builder style(int resStyle)
        {
            this.resStyle = resStyle;
            return this;
        }
        public Builder cancelTouchout(boolean val)
        {
            cancelTouchout = val;
            return this;
        }
        public Builder addViewOnclick(int viewRes,View.OnClickListener listener)
        {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }
        public CustomDialog build()
        {
            if (resStyle != -1)
            {
                return new CustomDialog(this, resStyle);
            }
            else
            {
                return new CustomDialog(this);
            }
        }
    }
}

3.  在dream_treeActivity.java中,一个按钮调用dialog。(点击按钮,弹出自定义的对话框)

package cn.demo.ui.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import cn.demo.R;
import cn.demo.ui.CustomDialog;



public class dream_treeActivity extends AppCompatActivity  implements View.OnClickListener {
    private Button bt_confirm; //确认按钮
    private Button bt_cancel;  //取消按钮
    private RadioGroup radioGroup;
    private CustomDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dream_tree_activity);
        //单选框
        radioGroup = findViewById(R.id.radio);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton checked_gong = findViewById(R.id.checked_gong);
            }
        });
   
        bt_confirm = (Button) findViewById(R.id.bt_confirm);  //获取确认按钮的id
        bt_cancel = (Button) findViewById(R.id.bt_cancel);    //获取 取消按钮的 id

        bt_confirm.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_confirm:
                showDialog_yes();
                break;
            case R.id.bt_cancel:
                showDialog_no();
                break;
        }
    }
//选择失败
    private void showDialog_no() {
        Intent intent1=new Intent(dream_treeActivity.this,TreeActivity.class);
        startActivity(intent1);
    }
//    选择成功 ,弹出对话框
    private void showDialog_yes()
    {
    CustomDialog.Builder builder = new CustomDialog.Builder(this);
    dialog = builder.cancelTouchout(false)
            .view(R.layout.correct_dialog)
            .heightDimenRes(R.dimen.error_dialog_height)
            .widthDimenRes(R.dimen.error_dialog_width)
            .style(R.style.dialog)
            .addViewOnclick(R.id.btn_yes, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            })
            .build();
    dialog.show();
    }
}
这个颜色标出的正是我之前报错然后修改的地方!!! 


你可能感兴趣的:(Android)