【文件属性】:
文件名称:自定义Dialog弹窗
文件大小:16.05MB
文件格式:RAR
更新时间:2020-11-01 09:52:05
自定义Dialog
从底部弹出的dialog。位置你可以在base里自己改。使用方法都有。public class BaseDialog extends Dialog {
private View mContentView;
public Context mContext;
public LayoutInflater mInflater;
public BaseDialog(Context context) {
this(context, R.style.BaseDialogStyle);
mContext = context;
}
public BaseDialog(Context context, int themeResId) {
super(context, themeResId);
mContext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.dialog_base);
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp1 = dialogWindow.getAttributes();
WindowManager wm = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
int width = outMetrics.widthPixels;
lp1.width = width;
dialogWindow.setGravity(Gravity.BOTTOM);
dialogWindow.setAttributes(lp1);
mInflater = LayoutInflater.from(mContext);
}
public void setContentView(int layoutResID) {
mContentView = View.inflate(mContext, layoutResID, null);
LinearLayout view = (LinearLayout) findViewById(R.id.base_container);
view.addView(mContentView);
}
public View getContextView() {
return mContentView;
}
}
public class CustomDialog extends BaseDialog implements View.OnClickListener {
/**
* 标题
*/
private TextView mTitleTv;
/**
* 确定按钮
*/
private Button mPositiveBt;
/**
* 取消按钮
*/
private Button mNegativeBt;
/**
* 确定取消中间分割线
*/
private View mDiverView;
/**
* 内容容器
*/
private LinearLayout mContentLin;
private View.OnClickListener mPositiveClickListener;
private View.OnClickListener mNegativeClickListener;
private List mContents;
private String mContent;
private String mTilte;
private String mPositiveText;
private String mNegativeText;
private CustomDialog(Context context) {
super(context);
}
private CustomDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_one_button);
mTitleTv = (TextView) findViewById(R.id.title);
mContentLin = (LinearLayout) findViewById(R.id.container);
mPositiveBt = (Button) findViewById(R.id.positiveButton);
mNegativeBt = (Button) findViewById(R.id.negativeButton);
mDiverView = findViewById(R.id.diver);
mPositiveBt.setOnClickListener(this);
mNegativeBt.setOnClickListener(this);
setContent(mContent);
setContents();
setTitle();
setButtonClickListener();
setNegativeBtText();
setPositiveBtText();
}
private void setContents() {
if (mContents != null && mContents.size() != 0) {
if (mContents.size() == 1) {
setContent(mContents.get(0));
} else {
for (int i = 0; i < mContents.size(); i++) {
View view = mInflater.inflate(R.layout.diaolog_content_item_left, null);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(mContents.get(i));
mContentLin.addView(view);
}
}
}
}
private void setContent(String content) {
if (!TextUtils.isEmpty(content)) {
View view = mInflater.inflate(R.layout.diaolog_content_item_center, null);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText(content);
mContentLin.addView(view);
}
}
/**
* 设置标题
*/
private void setTitle() {
if (!TextUtils.isEmpty(mTilte)) {
mTitleTv.setVisibility(View.VISIBLE);
mTitleTv.setText(mTilte);
}
}
/**
* 设置右侧按钮的显示文字,默认为确定
*/
private void setPositiveBtText() {
if (!TextUtils.isEmpty(mPositiveText)) {
mPositiveBt.setText(mPositiveText);
}
}
/**
* 设置左侧按钮的显示文字,默认为取消
*/
private void setNegativeBtText() {
if (!TextUtils.isEmpty(mNegativeText)) {
mNegativeBt.setText(mNegativeText);
}
}
/**
* 设置监听 可不设置
*/
private void setButtonClickListener() {
if (mPositiveClickListener != null) {
mPositiveBt.setVisibility(View.VISIBLE);
}
if (mNegativeClickListener != null) {
mNegativeBt.setVisibility(View.VISIBLE);
}
if (mNegativeClickListener != null && mPositiveClickListener != null) {
mDiverView.setVisibility(View.VISIBLE);
}
}
public static class Builder {
private CustomDialog dialog;
public Builder(Context context) {
dialog = new CustomDialog(context);
}
public Builder(Context context, int themeResId) {
dialog = new CustomDialog(context, themeResId);
}
/**
* 通过Builder类创建dialog
*/
public CustomDialog create() {
return dialog;
}
/**
* 多条提示
*/
public Builder setContent(List contents) {
dialog.mContents = contents;
return this;
}
/**
* 单条提示
*/
public Builder setContent(String content) {
dialog.mContent = content;
return this;
}
/**
* 设置标题
*/
public Builder setTitle(String tilte) {
dialog.mTilte = tilte;
return this;
}
/**
* 设置右侧按钮的显示文字,默认为确定
*
* @param positiveText 按钮上的显示内容
*/
public Builder setPositiveBtText(String positiveText) {
dialog.mPositiveText = positiveText;
return this;
}
/**
* 设置左侧按钮的显示文字,默认为取消
*
* @param negativeText 按钮上的显示内容
*/
public Builder setNegativeBtText(String negativeText) {
dialog.mNegativeText = negativeText;
return this;
}
;
/**
* 设置监听 可不设置
*
* @param positiveClickListener 确定按钮
* @param negativeClickListener 确定取消按钮
*/
public Builder setButtonClickListener(View.OnClickListener positiveClickListener,
View.OnClickListener negativeClickListener) {
dialog.mPositiveClickListener = positiveClickListener;
dialog.mNegativeClickListener = negativeClickListener;
return this;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.positiveButton:
if (mPositiveClickListener != null) {
mPositiveClickListener.onClick(v);
dismiss();
}
break;
case R.id.negativeButton:
if (mNegativeClickListener != null) {
mNegativeClickListener.onClick(v);
dismiss();
}
break;
}
}
}