###一、描述
一些常用的Dialog###二、效果图
###三、代码
/** * author:Administrator on 2016/12/16 16:01 * description:自定义dialog * version:版本 */public class CommonDialog extends Dialog { /** * 显示的图片 */ private ImageView imageIv; /** * 显示的标题 */ private TextView titleTv; /** * 显示的消息 */ private TextView messageTv; /** * 确认和取消按钮 */ private Button negtiveBn, positiveBn; /** * 按钮之间的分割线 */ private View columnLineView; public CommonDialog(Context context) { super(context, R.style.CustomDialog); } /** * 都是内容数据 */ private String message; private String title; private String positive, negtive; private int imageResId = -1; /** * 底部是否只有一个按钮 */ private boolean isSingle = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.common_dialog_layout); //按空白处不能取消动画 setCanceledOnTouchOutside(false); //初始化界面控件 initView(); //初始化界面数据 refreshView(); //初始化界面控件的事件 initEvent(); } /** * 初始化界面的确定和取消监听器 */ private void initEvent() { //设置确定按钮被点击后,向外界提供监听 positiveBn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onClickBottomListener != null) { onClickBottomListener.onPositiveClick(); } } }); //设置取消按钮被点击后,向外界提供监听 negtiveBn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onClickBottomListener != null) { onClickBottomListener.onNegtiveClick(); } } }); } /** * 初始化界面控件的显示数据 */ private void refreshView() { //如果用户自定了title和message if (!TextUtils.isEmpty(title)) { titleTv.setVisibility(View.VISIBLE); titleTv.setText(title); titleTv.setVisibility(View.VISIBLE); } else { titleTv.setVisibility(View.GONE); } if (!TextUtils.isEmpty(message)) { messageTv.setVisibility(View.VISIBLE); messageTv.setText(message); } //如果设置按钮的文字 if (!TextUtils.isEmpty(positive)) { positiveBn.setText(positive); } else { positiveBn.setText("确定"); } if (!TextUtils.isEmpty(negtive)) { negtiveBn.setText(negtive); } else { negtiveBn.setText("取消"); } if (imageResId != -1) { imageIv.setVisibility(View.VISIBLE); imageIv.setImageResource(imageResId); imageIv.setVisibility(View.VISIBLE); } else { imageIv.setVisibility(View.GONE); } /** * 只显示一个按钮的时候隐藏取消按钮,回掉只执行确定的事件 */ if (isSingle) { columnLineView.setVisibility(View.GONE); negtiveBn.setVisibility(View.GONE); } else { negtiveBn.setVisibility(View.VISIBLE); columnLineView.setVisibility(View.VISIBLE); } } @Override public void show() { super.show(); refreshView(); } /** * 初始化界面控件 */ private void initView() { negtiveBn = (Button) findViewById(R.id.negtive); positiveBn = (Button) findViewById(R.id.positive); titleTv = (TextView) findViewById(R.id.title); messageTv = (TextView) findViewById(R.id.message); imageIv = (ImageView) findViewById(R.id.image); columnLineView = findViewById(R.id.column_line); } /** * 设置确定取消按钮的回调 */ public OnClickBottomListener onClickBottomListener; public CommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) { this.onClickBottomListener = onClickBottomListener; return this; } public interface OnClickBottomListener { /** * 点击确定按钮事件 */ public void onPositiveClick(); /** * 点击取消按钮事件 */ public void onNegtiveClick(); } public String getMessage() { return message; } public CommonDialog setMessage(String message) { this.message = message; return this; } public String getTitle() { return title; } public CommonDialog setTitle(String title) { this.title = title; return this; } public String getPositive() { return positive; } public CommonDialog setPositive(String positive) { this.positive = positive; return this; } public String getNegtive() { return negtive; } public CommonDialog setNegtive(String negtive) { this.negtive = negtive; return this; } public int getImageResId() { return imageResId; } public boolean isSingle() { return isSingle; } public CommonDialog setSingle(boolean single) { isSingle = single; return this; } public CommonDialog setImageResId(int imageResId) { this.imageResId = imageResId; return this; }}复制代码
复制代码
复制代码
复制代码
###四、示例
final CommonDialog dialog = new CommonDialog(activity); dialog.setTitle("确定终止会议?") .setNegtive("取消") .setPositive("终止") .setSingle(false).setOnClickBottomListener(new CommonDialog.OnClickBottomListener() { @Override public void onPositiveClick() { dialog.dismiss(); deleteMeeting(); } @Override public void onNegtiveClick() { dialog.dismiss(); } }).show();复制代码