本文实例为大家分享了android自定义Dialog弹框和背景阴影显示的具体代码,供大家参考,具体内容如下
首先需要自定义一个类,继承Dialog
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.zhiziyun.dmptest.bot.R;
public class CustomDialog extends Dialog {
private Button yes, no;//确定按钮
private TextView titleTv;//消息标题文本
private TextView messageTv;//消息提示文本
private String titleStr;//从外界设置的title文本
private String messageStr;//从外界设置的消息文本
//确定文本和取消文本的显示内容
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
public void setNoonclickListener(String str, onNoOnclickListener onNoOnclickListener) {
if (str != null) {
noStr = str;
}
this.noonclickListener = onNoOnclickListener;
}
public void setYesonclickListener(String str, onYesOnclickListener onYesOnclickListener) {
if (str != null) {
yesStr = str;
}
this.yesonclickListener = onYesOnclickListener;
}
public CustomDialog(Context context) {
super(context, R.style.Dialog_Msg);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_custom);
//按空白处不能取消动画
setCanceledonTouchOutside(false);
//初始化界面控件
initView();
//初始化界面数据
initData();
//初始化界面控件的事件
initEvent();
}
private void initEvent() {
//设置确定按钮被点击后,向外界提供监听
yes.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
if (yesonclickListener != null) {
yesOnclickListener.onYesClick();
}
}
});
no.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View v) {
if (noonclickListener != null) {
noOnclickListener.onNoClick();
}
}
});
}
private void initData() {
//如果用户自定了title和message
if (titleStr != null) {
titleTv.setText(titleStr);
}
if (messageStr != null) {
messageTv.setText(messageStr);
}
//如果设置按钮的文字
if (yesStr != null) {
yes.setText(yesStr);
}
}
private void initView() {
yes = (Button) findViewById(R.id.yes);
no = (Button) findViewById(R.id.no);
titleTv = (TextView) findViewById(R.id.title);
messageTv = (TextView) findViewById(R.id.message);
}
public void setTitle(String title) {
titleStr = title;
}
public void setMessage(String message) {
messageStr = message;
}
public interface onYesOnclickListener {
public void onYesClick();
}
public interface onNoOnclickListener {
public void onNoClick();
}
@Override
public void show() {
super.show();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
}
}
这是实体类中的style:
- @null
- true
- false
- true
- true
- @color/transparent
- 10dp
其中@color/transparent是一个透明色
#00000000
然后是布局
下面是shape_dialog_msg的代码
准备工作都做好了,下面就是如何使用了
//点击弹出对话框
final CustomDialog customDialog = new CustomDialog(getActivity());
customDialog.setTitle("消息提示");
customDialog.setMessage("是否暂停广告投放?");
customDialog.setYesonclickListener("确定", new CustomDialog.onYesOnclickListener() {
@Override
public void onYesClick() {
//这里是确定的逻辑代码,别忘了点击确定后关闭对话框
customDialog.dismiss();
}
});
customDialog.setNoonclickListener("取消", new CustomDialog.onNoOnclickListener() {
@Override
public void onNoClick() {
customDialog.dismiss();
}
});
customDialog.show();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



