您的位置:首页 > 其它

2种自定义dialog实现自定义布局的思考

2018-01-22 15:48 253 查看
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种都可以,视情况而定使用哪种。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: