Android(安卓)自定义弹窗 Dialog
16lz
2021-01-26
自定义Dialog,弹出提示。总的来说就是 extends Dialog类。
下面列出一些Dialog的基本属性:
1 AlertDialog.Builder属性* setTitle: 为对话框设置标题 ;* setIcon : 为对话框设置图标;* setMessage: 为对话框设置内容;* setView : 给对话框设置自定义样式 ;* setItems: 设置对话框要显示的一个list,一般用于显示几个命令时;* setMultiChoiceItems:用来设置对话框显示一系列的复选框;* setNeutralButton : 响应中立行为的点击;* setPositiveButton : 响应Yes/Ok的点击 ;* setNegativeButton :响应No/Cancel的点击 ;* create : 创建对话框 ;* show : 显示对话框;2 ProgressDialog属性*setProgressStyle: 设置进度条风格,风格为圆形,旋转的;*setTitlt: 设置ProgressDialog 标题;*setMessage: 设置ProgressDialog提示信息;*setIcon: 设置ProgressDialog标题图标;*setIndeterminate: 设置ProgressDialog 的进度条是否不明确;*setCancelable: 设置ProgressDialog 是否可以按返回键取消;*setButton: 设置ProgressDialog 的一个Button(需要监听Button事件);*show: 显示ProgressDialog。
如果了解了Dialog的基本属性。我们在自定义一个Dialog。呈现出我们自己的弹窗 public class HintToast extends Dialog { public HintToast(Context context) { super(context); } public HintToast(Context context, int theme) { super(context, theme); } protected HintToast(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } public static class Builder { private Context mContext; private View mView; private String mMessage; private String positiveButtonText; // 确定按钮 private String negativeButtonText; // 取消 private DialogInterface.OnClickListener positiveButtonClickListener; // 确定监听 private DialogInterface.OnClickListener negativeButtonClickListener; public Builder(Context context) { this.mContext = context; } public Builder setMessage(String message) { this.mMessage = message; return this; } public Builder setContentView(View view) { this.mView = view; return this; } // 设置监听(确定) public Builder setPositiveButton(String string, DialogInterface.OnClickListener listener) { this.positiveButtonText = string; this.positiveButtonClickListener = listener; return this; } public Builder setNegativeButton(String string, DialogInterface.OnClickListener listener) { this.negativeButtonText = string; this.negativeButtonClickListener = listener; return this; } public HintToast create() { LayoutInflater infalter = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = infalter.inflate(R.layout.mimicam_toast, null); final HintToast dialog = new HintToast(mContext, R.style.Dialog); // 是ViewGroup 下的LayoutParans 包 LayoutParams layoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); dialog.addContentView(layout, layoutparams); // z设置确定 按钮 Button ok = (Button) layout.findViewById(R.id.id_toast_ok); if (null != positiveButtonText) { ok.setText(positiveButtonText); if (null != positiveButtonClickListener) { ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } } else { ok.setVisibility(View.GONE); } //设置取消 按钮 Button no = (Button) layout.findViewById(R.id.id_toast_no); if (null != negativeButtonText) { no.setText(negativeButtonText); if (null != negativeButtonClickListener) { no.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } } else { no.setVisibility(View.GONE); } // 设置消息 if (null != mMessage) { TextView message = (TextView) layout.findViewById(R.id.id_toast_message); message.setText(mMessage); } else if (null != mView) { LinearLayout linear = (LinearLayout) layout.findViewById(R.id.layout_content); linear.removeAllViews(); linear.addView(mView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } dialog.setContentView(layout); return dialog; } }}
下面是我们自定义的XML 布局。 <?xml version="1.0" encoding="utf-8"?>
引用的背景Drawable xml 文件是: <?xml version="1.0" encoding="utf-8"?> -
简单的两张图片而已。 更多相关文章
- 一个Activity的显示过程总结(二)
- Android中关于退出和Toast的引用
- 模态对话框 和 非模态对话框
- Android(安卓)ActionBar详解
- Android中Toolbar的使用
- Android(安卓)SimpleDateFormat 日期时间格式 系统设置12_24小时
- android service中显示一个dialog
- Android(判断wifi是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏)
- Android开发之蓝牙(Bluetooth)---源码目录