android自定义Dialog

android中为了用户体验常常会有很漂亮的dialog提示框,但是系统自带的dialog又解决不了产品的需求,这个时候就需要我们去自定义一个dialog去解决了,这里给大家准备了两个方法大家可以参考参考;

1.通过申明一个dialog,给dialog设置view,然后trun this;

public class DetailDialog {
    private Context context;
    private Dialog dialog;
    private Display display;
    private TextView tv_lx,tv_close;
    private ListView lv_list;
    private List list;



    public DetailDialog(Context context, List list) {
        this.context = context;
        this.list=list;
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public DetailDialog build(){
        View view = LayoutInflater.from(context).inflate(R.layout.detaildialog, null);

        tv_close=view.findViewById(R.id.tv_close);
        lv_list=view.findViewById(R.id.lv_list);

        lv_list.setAdapter(new MyDetailAdapter());

        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                dialog=null;
            }
        });

        // 定义Dialog布局和参数
        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        WindowManager manager=dialogWindow.getWindowManager();
        Display d=manager.getDefaultDisplay();
        lp.width=d.getWidth();
//        lp.x = 0;
//        lp.y = 0;
        dialogWindow.setAttributes(lp);
        return this;
    }


    public DetailDialog setLv(String  text) {
        tv_lx.setText(text);
        return this;
    }

    public void show() {
        dialog.show();
    }

    private class MyDetailAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View item= View.inflate(context,R.layout.item_detaildialog,null);
            LinearLayout ll_one=item.findViewById(R.id.ll_one);
            LinearLayout ll_two=item.findViewById(R.id.ll_two);
            TextView tv_jdlx=item.findViewById(R.id.tv_jdlx);
            TextView tv_je=item.findViewById(R.id.tv_je);
            TextView tv_ll=item.findViewById(R.id.tv_ll);
            TextView tv_time=item.findViewById(R.id.tv_time);
            TextView tv_jdname=item.findViewById(R.id.tv_jdname);
            TextView tv_lx=item.findViewById(R.id.tv_lx);
            ll_one.setVisibility(View.GONE);
            ll_two.setVisibility(View.GONE);

           if(list.get(position).getType().equals("1")){
               ll_one.setVisibility(View.VISIBLE);
               tv_lx.setText(isNull(list.get(position).getRealinterest())+"元");
           }else {
               ll_two.setVisibility(View.VISIBLE);
               tv_jdname.setText(isNull(list.get(position).getPeriodName()));
               tv_jdlx.setText(isNull(list.get(position).getRealinterest())+"元");
               tv_je.setText(isNull(list.get(position).getRealamountView()+"万元"));
               tv_ll.setText(isNull(list.get(position).getInterest())+"%");
               tv_time.setText(isNull(list.get(position).getInterestBegindate())+"至"+isNull(list.get(position).getInterestEnddate()));

           }

            return item;
        }
    }

    private String isNull(String tex) {
        if(TextUtils.isEmpty(tex)){
            return "--";
        }else {
            return tex;
        }
    }
}

2.通过直接继承dialog然后在在dialog重写的方法里面init(),设置布局

public class CertifiedPromptDialog extends Dialog {
    Context context;
    public CertifiedPromptDialog(@NonNull Context context) {
        super(context);
        this.context=context;
        init();
    }

    public CertifiedPromptDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context=context;
        init();
    }

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

    private void init()
    {
        setContentView(R.layout.certified_prompt_dialog);
    }

//    public void setDesc(String desc)
//    {
//        ((TextView) findViewById(R.id.certified_prompt_desc)).setText(desc);
//    }
//
//    TextView tvBtn;

    ImageView iv_shareorfinsh;

    public void setBgImage(int image)
    {
        findViewById(R.id.rl_rzbg).setBackgroundResource(image);
    }


    public void setBtnImage(int image, final OnClickListener listener)
    {
        iv_shareorfinsh= (ImageView) findViewById(R.id.iv_shareorfinsh);
        iv_shareorfinsh.setImageResource(image);
        iv_shareorfinsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener!=null){
                    listener.onClick();
                }
            }
        });
    }



//    public void setButtonOnClickListener(String btnText,final OnClickListener listener)
//    {
//        tvBtn= (TextView) findViewById(R.id.certified_prompt_btn);
//        tvBtn.setText(btnText);
//        tvBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if(listener!=null)
//                    listener.onClick();
//            }
//        });
//    }

    public interface OnClickListener{
        public void onClick();
    }
}

你可能感兴趣的:(android自定义Dialog)