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();
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种都可以,视情况而定使用哪种。
相关文章推荐
- 完全自定义dialog布局的实现
- Fragment+ListView实现碎片化管理订单(另附自定义布局AlertDialog弹窗的设置)
- Dialog自定义布局的实现
- Dialog自定义布局上面,控件实现动画
- Android自定义Dialog布局listview 实现显示安装的应用
- 用自定义布局实现仿IOS的dialog布局
- Android自定义Dialog--DanmakuFlame弹幕发送框界面的实现
- android之自定义ViewGroup和自动换行的布局的实现
- 自定义ViewGroup实现流式布局
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android 自定义dialog的实现代码
- Android 自定义Dialog实现步骤及封装
- 在自定义Dialog上,点击Button实现WebView的跳转
- android 通过回调实现 完全自定义Dialog 简单易用
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- RecycleView实现横向带指示器翻页滑动,一行两列自定义布局
- android自定义dialog实现app换肤功能
- 来实现android自定义的menu菜单之二:为自定义menu中的线性布局添加事件
- 自定义dialog,实现右上角显示一个控件按钮
- adapter模式实现流式布局(自定义标签效果)