2種自定義dialog實現自定義佈局的思考

1.第一種,使用靜態Builder,返回一個dialog,有一點不足我發現不能實現點擊之後的dialog的dismiss,很神奇,先貼上我的代碼

                final TimesDialog.Builder builder = new TimesDialog.Builder(Fragment_MyHomeNew.this.getContext());
                builder.setDialogContent(query_count + "");
                builder.setTimeClick(new TimesClik() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(Fragment_MyHomeNew.this.getActivity(), Activity_Pay.class));
                    }
                });
                Dialog dialog = builder.create();
                dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_TOAST));
                dialog.setCanceledOnTouchOutside(false);
                dialog.show();

public class TimesDialog extends Dialog {
    public TimesDialog(Context context) {
        super(context);
    }

    public TimesDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected TimesDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String str_times = null;
        private TimesClik timesClik;

        public Builder(Context context) {
            this.context = context;
        }


        public Builder setDialogContent(String content) {
            this.str_times = content;
            return this;
        }

        public Builder setTimeClick(TimesClik timeClick0) {
            this.timesClik = timeClick0;
            return this;
        }

        public TimesDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final TimesDialog dialog = new TimesDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_times, null);
            TextView tv_times = (TextView) layout.findViewById(R.id.tv2);
            ImageView iv_chognzhi = (ImageView) layout.findViewById(R.id.iv);
            if (timesClik != null) {
                iv_chognzhi.setOnClickListener(timesClik);
            }
            if (str_times != null) {
                tv_times.setText(str_times);
            }
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return dialog;
        }
    }

}
有一個問題就是點擊按鈕之後dialog並不消失。回調接口啥的都行,就是對dialog本身操作不方便。

2.第二種,我發現自己定義一個,把構造方法改了,加幾個參數很好的實現功能。下面貼代碼

 TimesDialogNew timesDialogNew = new TimesDialogNew(Fragment_MyHomeNew.this.getContext(), R.style.Dialog, query_count + "", new TimesDialogNew.yoyo() {
                    @Override
                    public void onClick() {
                        startActivity(new Intent(Fragment_MyHomeNew.this.getActivity(), Activity_Pay.class));
                    }
                },false);
                timesDialogNew.show();

public class TimesDialogNew extends Dialog implements View.OnClickListener {
    private Context mContext;
    private String str_times = null;
    private boolean flag;//false顯示剩餘服務次數,true顯示剩餘服務天數

    private ImageView iv_chognzhi;
    private TextView tv;
    private TextView tv_times;

    private yoyo yoyo;

    public TimesDialogNew(Context context) {
        super(context);
        this.mContext = context;
    }

    public TimesDialogNew(Context context, int themeResId, String str, yoyo clicklistener, boolean flag) {
        super(context, themeResId);
        this.mContext = context;
        this.str_times = str;
        this.yoyo = clicklistener;
        this.flag = flag;
    }


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

    private void initView() {
        iv_chognzhi = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv1);
        tv_times = (TextView) findViewById(R.id.tv2);
        if (str_times != null) {
            tv_times.setText(str_times);
        }
        tv_times.setText(str_times);
        if (flag) {
            tv.setText("剩餘服務天數");
        } else {
            tv.setText("剩餘服務次數");
        }
        iv_chognzhi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv:
                if (yoyo != null) {
                    yoyo.onClick();
                }
                this.dismiss();
                break;
        }
    }


    public interface yoyo {
        void onClick();
    }
}
總結一下,2種都可以,視情況而定使用哪種。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章