第一种 创建一个MyDialogs
public class MyDialogs extends Dialog {
private Button confirmButton, cancelButton;
private EditText editText;
public MyDialogs(@NonNull Context context) {
super(context);
init();
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
// 设置 Dialog 的位置
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
// layoutParams.gravity = Gravity.BOTTOM; //TOP弹框最上方
//设置Dialog的宽度大小
layoutParams.width = 1200;
layoutParams.height = 800;
//背景圆角白色
window.getDecorView().setBackgroundResource(R.color.transparent);
window.setAttributes(layoutParams);
}
public void init() {
setContentView(R.layout.new_add_file);
// 设置点击 Dialog 外部区域不取消 Dialog
setCanceledOnTouchOutside(false);
confirmButton = findViewById(R.id.confirm_button);
cancelButton = findViewById(R.id.cancel_button);
editText = findViewById(R.id.dialog_input);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = editText.getText().toString();
if (!TextUtils.isEmpty(content)) {
String path = "/storage/emulated/0/Download/Filelocatily" + "/" + content;
File folder = new File(path);
if (!folder.exists()) {
folder.mkdirs();
}
} else {
Toast.makeText(getContext(), "请输入文件名称", Toast.LENGTH_LONG).show();
}
dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
Activity /Fragment 调用方法
private MyDialogs myDialogs;
myDialogs = new MyDialogs(getActivity());
myDialogs.show();
第二种
public class MyDialogs extends Dialog {
public MyDialogs(Context context, int layout, int style, int gravity) {
super(context, style);
setContentView(layout);
Window mWindow = getWindow();
WindowManager.LayoutParams params = mWindow.getAttributes();
// params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
//背景圆角白色
mWindow.getDecorView().setBackgroundResource(R.color.transparent);
params.width = 1100;
params.gravity = gravity;
mWindow.setAttributes(params);
}
}
调用方法
private MyDialogs myDialogs;
myDialogs = new MyDialogs(getActivity(), R.layout.new_add_file, 0, Gravity.CENTER);
myDialogs.show();
myDialogs.setCanceledOnTouchOutside(false);
// 设置点击 Dialog 外部区域不取消 Dialog
Button confirmButton = myDialogs.findViewById(R.id.confirm_button);
Button cancelButton = myDialogs.findViewById(R.id.cancel_button);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialogs.dismiss();
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myDialogs.dismiss();
}
});
在页面销毁的时候置空 要不有可能导致内存泄漏
@Override
public void onDestroy() {
super.onDestroy();
if (myDialogs != null) {
myDialogs.dismiss();
myDialogs = null;
}
}