目录
一 简介
二 用法
2.1 onCreateView
2.2 onCreateDialog
三 DialogFragment 目前存在问题
3.1 内存泄露问题
3.2 onSaveInstanceState 之后 show,会报错。
3.3 创建dialog 时,setOnCancelListener和setOnDismissListener失效。
四 常用几个 Fragment 封装
4.1 确认 dialog
4.2 List dialog
4.3 单选 dialog
4.4 多选 dialog
4.5 等待 dialog
4.6 进度条 dialog
4.7 日期选择 dialog
4.8 时间选择 dialog
五 参考
DialogFragment其实就是dialog 和 fragment 的结合。使传统 dialog,使 dialog 赋予 fragment 特性,更好处理生命周期内的各项操作。
目前主要是两种。通过onCreateView和onCreateDialog。
package com.demo.ck.dialogfragmentdemo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
/**
* 两种布局方法,一种返回布局
*/
public class CoreDialogFragment1 extends CoreDialogFragment {
private TextView tvDemo;
public static CoreDialogFragment1 netInstance() {
CoreDialogFragment1 coreDialogFactory = new CoreDialogFragment1();
Bundle args = new Bundle();
coreDialogFactory.setArguments(args);
return coreDialogFactory;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_core_demo1, container, false);
// 设置宽度为屏宽、位置在屏幕底部
Window window = getDialog().getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.CENTER;
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvDemo = view.findViewById(R.id.tvDemo);
tvDemo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
package com.demo.ck.dialogfragmentdemo;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
/**
* 两种布局方法,一种返回Dialog
*/
public class CoreDialogFragment2 extends CoreDialogFragment {
private final String TAG = getClass().getSimpleName();
public static CoreDialogFragment2 netInstance() {
CoreDialogFragment2 coreDialogFactory = new CoreDialogFragment2();
Bundle args = new Bundle();
coreDialogFactory.setArguments(args);
return coreDialogFactory;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppDialogTheme);
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog dialog = new AlertDialog.Builder(requireActivity())
.setIcon(R.mipmap.ic_launcher)
.setTitle("title")
.setMessage("1121e3212e2e21")
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i(TAG, "onCreateDialog;show=" + getShowsDialog());
}
}
)
.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dismiss();
}
}
)
.create();
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
return dialog;
}
@Override
public void onDismiss(@NonNull DialogInterface dialog) {
super.onDismiss(dialog);
Log.i(TAG, "CoreDialogFragment2,onDismiss");
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
Log.i(TAG, "CoreDialogFragment2,onCancel");
}
}
原生 bug,会在onActivityCreated设置onCancelListener和onDismissListener,其中会把 Fragment 对象传到一个 handler 中,导致内存泄露。
解决办法:重写 onActivityCreated,使用弱引用包裹 Fragment 对象。
package com.demo.ck.dialogfragmentdemo.core;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import java.lang.ref.WeakReference;
/**
* 解决 dialogFragment 内存泄露问题
*/
public class LeakDialogFragment extends DialogFragment {
public static class DialogDismissListener implements DialogInterface.OnDismissListener {
private WeakReference leakDialogFragmentWeakReference;
DialogDismissListener(LeakDialogFragment leakDialogFragment) {
this.leakDialogFragmentWeakReference = new WeakReference<>(leakDialogFragment);
}
@Override
public void onDismiss(DialogInterface dialog) {
LeakDialogFragment leakDialogFragment = leakDialogFragmentWeakReference.get();
if (leakDialogFragment != null) {
leakDialogFragment.onDismiss(dialog);
}
}
}
public static class DialogCancelListener implements DialogInterface.OnCancelListener {
private WeakReference leakDialogFragmentWeakReference;
DialogCancelListener(LeakDialogFragment leakDialogFragment) {
this.leakDialogFragmentWeakReference = new WeakReference<>(leakDialogFragment);
}
@Override
public void onCancel(DialogInterface dialog) {
LeakDialogFragment leakDialogFragment = leakDialogFragmentWeakReference.get();
if (leakDialogFragment != null) {
leakDialogFragment.onCancel(dialog);
}
}
}
/**
* 解决原生setOnDismissListener和setOnCancelListener内存泄露问题
*
* @param savedInstanceState
*/
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
if (getShowsDialog()) {
setShowsDialog(false);
}
super.onActivityCreated(savedInstanceState);
setShowsDialog(true);
if (getDialog() == null) {
return;
}
View view = getView();
if (view != null) {
if (view.getParent() != null) {
throw new IllegalStateException(
"DialogFragment can not be attached to a container view");
}
getDialog().setContentView(view);
}
final Activity activity = getActivity();
if (activity != null) {
getDialog().setOwnerActivity(activity);
}
getDialog().setCancelable(isCancelable());
DialogDismissListener mDialogDissmissLitener = new DialogDismissListener(this);//设置一个自定义的DissmissListener
getDialog().setOnDismissListener(mDialogDissmissLitener);
DialogCancelListener mDialogCancelListener = new DialogCancelListener(this);//设置一个自定义的DissmissListener
getDialog().setOnCancelListener(mDialogCancelListener);
if (savedInstanceState != null) {
Bundle dialogState = savedInstanceState.getBundle("android:savedDialogState");
if (dialogState != null) {
getDialog().onRestoreInstanceState(dialogState);
}
}
}
}
如果使用原生 show 方法,在onSaveInstanceState之后调用,会报错
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
原因:Fragment 在 commit 的时候,会检查onSaveInstanceState 状态,如果已经onSaveInstanceState,会抛出异常。
解决方法:使用commitAllowingStateLoss,原生 DialogFragment 不支持,所以需要通过反射方法自行添加。
package com.demo.ck.dialogfragmentdemo.core;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.demo.ck.dialogfragmentdemo.R;
import java.lang.reflect.Field;
import java.util.Objects;
/**
* 常用处理
*/
public class CoreDialogFragment extends LeakDialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AppDialogTheme);
}
public void show(@NonNull FragmentActivity activity) {
show(activity.getSupportFragmentManager(), activity.getClass().getCanonicalName());
}
public void showAllowingStateLoss(@NonNull FragmentActivity activity) {
showAllowingStateLoss(activity.getSupportFragmentManager(), activity.getClass().getCanonicalName());
}
@Override
public void show(@NonNull FragmentManager manager, @Nullable String tag) {
if (!isAdded()) {
super.show(manager, tag);
}
}
@Override
public int show(@NonNull FragmentTransaction transaction, @Nullable String tag) {
if (!isAdded()) {
return super.show(transaction, tag);
}
return -1;
}
@Override
public void showNow(@NonNull FragmentManager manager, @Nullable String tag) {
if (!isAdded()) {
super.showNow(manager, tag);
}
}
public void showAllowingStateLoss(@NonNull FragmentManager manager, @Nullable String tag) {
if (isAdded()) {
return;
}
try {
Class> c = Class.forName(Objects.requireNonNull(DialogFragment.class.getCanonicalName()));
Field dismissed = c.getDeclaredField("mDismissed");
dismissed.setAccessible(true);
dismissed.set(CoreDialogFragment.this, false);
Field shownByMe = c.getDeclaredField("mShownByMe");
shownByMe.setAccessible(true);
shownByMe.set(this, true);
} catch (Exception e) {
e.printStackTrace();
}
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commitAllowingStateLoss();
}
public void showAllowingStateLoss(@NonNull FragmentTransaction transaction, @Nullable String tag) {
if (isAdded()) {
return;
}
try {
Class> c = Class.forName(Objects.requireNonNull(DialogFragment.class.getCanonicalName()));
Field dismissed = c.getDeclaredField("mDismissed");
dismissed.setAccessible(true);
dismissed.set(this, false);
Field shownByMe = c.getDeclaredField("mShownByMe");
shownByMe.setAccessible(true);
shownByMe.set(this, true);
Field viewDestroyed = c.getDeclaredField("mViewDestroyed");
viewDestroyed.setAccessible(true);
viewDestroyed.set(this, false);
transaction.add(this, tag);
int mBackStackId = transaction.commitAllowingStateLoss();
Field backStackId = c.getDeclaredField("mBackStackId");
backStackId.setAccessible(true);
backStackId.set(this, mBackStackId);
} catch (Exception e) {
e.printStackTrace();
}
}
}
ps:
isAdded() 检查用来防止重复添加报错。
原因:在 onActivityCreated时会重置。
解决办法:通过DialogFragment 中的onDismiss和onCancel回调获取。
注意:setOnCancelListener和setOnDismissListener,针对android.app.DialogFragment会报错,java.lang.IllegalStateException: You can not set Dialog's OnCancelListener or OnDismissListener,针对androidx.fragment.app.DialogFragment,并不会报错,不过 listener 是失效的。
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.io.Serializable;
/**
* 确认 dialog
*/
public class ConfirmDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
private DialogListener dialogListener;
/*Flag*/
private String title;
private String msg;
private String positiveText;
private String negativeText;
private int gravity;
private boolean widthMatch;
/*View*/
public interface DialogListener extends Serializable {
void onResult(int which, String tag);
}
public static ConfirmDialogFragment netInstance(String title, String msg) {
return netInstance(title, msg, null, null);
}
public static ConfirmDialogFragment netInstance(String title, String msg, String positiveText, String negativeText) {
ConfirmDialogFragment dialogFragment = new ConfirmDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("msg", msg);
bundle.putString("positiveText", positiveText);
bundle.putString("negativeText", negativeText);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dialogListener = (DialogListener) bundle.getSerializable("listener");
title = bundle.getString("title");
msg = bundle.getString("msg");
positiveText = bundle.getString("positiveText");
negativeText = bundle.getString("negativeText");
gravity = bundle.getInt("gravity");
widthMatch = bundle.getBoolean("widthMatch");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton((positiveText == null ? "确定" : positiveText), onClickListener);
builder.setNegativeButton((negativeText == null ? "取消" : negativeText), onClickListener);
AlertDialog dialog = builder.create();
updateGravity(dialog);
return dialog;
}
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogListener != null) {
dialogListener.onResult(which, getTag());
}
}
};
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag());
}
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
if (widthMatch) {
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
}
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
public ConfirmDialogFragment setterDialogListener(DialogListener listener) {
this.dialogListener = listener;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("listener", listener);
setArguments(bundle);
return this;
}
public ConfirmDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public ConfirmDialogFragment setterWidthMatch(boolean widthMatch) {
this.widthMatch = widthMatch;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("widthMatch", widthMatch);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.io.Serializable;
/**
* 列表 dialog
*/
public class ListDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
private DialogListener dialogListener;
/*Flag*/
private String title;
private int gravity;
private String[] items;
private boolean widthMatch;
/*View*/
public interface DialogListener extends Serializable {
void onResult(int which, String tag);
}
public static ListDialogFragment netInstance(String title, String[] items) {
ListDialogFragment dialogFragment = new ListDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putCharSequenceArray("items", items);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dialogListener = (DialogListener) bundle.getSerializable("listener");
title = bundle.getString("title");
items = (String[]) bundle.getCharSequenceArray("items");
gravity = bundle.getInt("gravity");
widthMatch = bundle.getBoolean("widthMatch");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(title);
builder.setItems(items, onClickListener);
AlertDialog dialog = builder.create();
updateGravity(dialog);
return dialog;
}
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogListener != null) {
dialogListener.onResult(which, getTag());
}
}
};
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag());
}
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
if (widthMatch) {
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
}
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
public ListDialogFragment setterDialogListener(DialogListener listener) {
this.dialogListener = listener;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("listener", listener);
setArguments(bundle);
return this;
}
public ListDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public ListDialogFragment setterWidthMatch(boolean widthMatch) {
this.widthMatch = widthMatch;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("widthMatch", widthMatch);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.io.Serializable;
/**
* 单选 dialog
*/
public class SingleSelectDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
private DialogListener dialogListener;
/*Flag*/
private String title;
private String positiveText;
private String negativeText;
private String[] items;
private int curPostition;
private int gravity;
private boolean widthMatch;
/*View*/
public interface DialogListener extends Serializable {
void onResult(int which, String tag);
}
public static SingleSelectDialogFragment netInstance(String title, String[] items, int curPostition) {
return netInstance(title, items, curPostition, null, null);
}
public static SingleSelectDialogFragment netInstance(String title, String[] items, int curPostition, String positiveText, String negativeText) {
SingleSelectDialogFragment dialogFragment = new SingleSelectDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("positiveText", positiveText);
bundle.putString("negativeText", negativeText);
bundle.putInt("curPostition", curPostition);
bundle.putCharSequenceArray("items", items);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dialogListener = (DialogListener) bundle.getSerializable("listener");
title = bundle.getString("title");
positiveText = bundle.getString("positiveText");
negativeText = bundle.getString("negativeText");
items = (String[]) bundle.getCharSequenceArray("items");
curPostition = bundle.getInt("curPostition");
gravity = bundle.getInt("gravity");
widthMatch = bundle.getBoolean("widthMatch");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(title);
builder.setSingleChoiceItems(items, curPostition, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
curPostition = which;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("curPostition", curPostition);
setArguments(bundle);
}
});
builder.setPositiveButton((positiveText == null ? "确定" : positiveText), onClickListener);
builder.setNegativeButton((negativeText == null ? "取消" : negativeText), onClickListener);
AlertDialog dialog = builder.create();
updateGravity(dialog);
return dialog;
}
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogListener != null) {
if (which == AlertDialog.BUTTON_POSITIVE) {
dialogListener.onResult(curPostition, getTag());
} else {
dialogListener.onResult(which, getTag());
}
}
}
};
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag());
}
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
if (widthMatch) {
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
}
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
public SingleSelectDialogFragment setterDialogListener(DialogListener listener) {
this.dialogListener = listener;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("listener", listener);
setArguments(bundle);
return this;
}
public SingleSelectDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public SingleSelectDialogFragment setterWidthMatch(boolean widthMatch) {
this.widthMatch = widthMatch;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("widthMatch", widthMatch);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.io.Serializable;
import java.util.ArrayList;
/**
* 多选 dialog
*/
public class MultiSelectDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
private DialogListener dialogListener;
/*Flag*/
private String title;
private String positiveText;
private String negativeText;
private String[] items;
private boolean[] select;
private int gravity;
private boolean widthMatch;
private ArrayList choice = new ArrayList<>();
/*View*/
public interface DialogListener extends Serializable {
void onResult(int which, boolean[] select, String tag);
}
public static MultiSelectDialogFragment netInstance(String title, String[] items, boolean[] select) {
return netInstance(title, items, select, null, null);
}
public static MultiSelectDialogFragment netInstance(String title, String[] items, boolean[] select, String positiveText, String negativeText) {
MultiSelectDialogFragment dialogFragment = new MultiSelectDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("positiveText", positiveText);
bundle.putString("negativeText", negativeText);
bundle.putCharSequenceArray("items", items);
bundle.putBooleanArray("select", select);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dialogListener = (DialogListener) bundle.getSerializable("listener");
title = bundle.getString("title");
positiveText = bundle.getString("positiveText");
negativeText = bundle.getString("negativeText");
items = (String[]) bundle.getCharSequenceArray("items");
select = bundle.getBooleanArray("select");
initSelect();
gravity = bundle.getInt("gravity");
widthMatch = bundle.getBoolean("widthMatch");
}
convertToArrayList();
}
private void initSelect() {
if (select == null && items != null) {
select = new boolean[items.length];
}
}
private void convertToArrayList() {
if (select != null) {
for (int i = 0; i < select.length; i++) {
if (select[i]) {
choice.add(i);
}
}
}
}
private void convertToBooleans() {
if (choice != null && select != null) {
for (int i = 0; i < select.length; i++) {
select[i] = choice.contains(i);
}
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle(title);
builder.setMultiChoiceItems(items, select, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
if (!choice.contains(which)) {
choice.add(which);
}
} else {
if (choice.contains(which)) {
choice.remove(choice.indexOf(which));
}
}
convertToBooleans();
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBooleanArray("select", select);
setArguments(bundle);
}
});
builder.setPositiveButton((positiveText == null ? "确定" : positiveText), onClickListener);
builder.setNegativeButton((negativeText == null ? "取消" : negativeText), onClickListener);
AlertDialog dialog = builder.create();
updateGravity(dialog);
return dialog;
}
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogListener != null) {
dialogListener.onResult(which, select, getTag());
}
}
};
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, select, getTag());
}
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
if (widthMatch) {
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
}
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
public MultiSelectDialogFragment setterDialogListener(DialogListener listener) {
this.dialogListener = listener;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("listener", listener);
setArguments(bundle);
return this;
}
public MultiSelectDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public MultiSelectDialogFragment setterWidthMatch(boolean widthMatch) {
this.widthMatch = widthMatch;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("widthMatch", widthMatch);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
/**
* 等待 dialog
*/
public class WaitingDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
/*Flag*/
private String title;
private String msg;
private int gravity;
private boolean widthMatch;
/*View*/
public static WaitingDialogFragment netInstance(String title, String msg) {
WaitingDialogFragment dialogFragment = new WaitingDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("msg", msg);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
title = bundle.getString("title");
msg = bundle.getString("msg");
gravity = bundle.getInt("gravity");
widthMatch = bundle.getBoolean("widthMatch");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
ProgressDialog dialog = new ProgressDialog(requireActivity());
dialog.setTitle(title);
dialog.setMessage(msg);
setCancelable(false);
updateGravity(dialog);
return dialog;
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
if (widthMatch) {
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
}
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
public WaitingDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public WaitingDialogFragment setterWidthMatch(boolean widthMatch) {
this.widthMatch = widthMatch;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("widthMatch", widthMatch);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.text.NumberFormat;
/**
* 进度条 dialog
*/
public class ProgressDialogFragment extends CoreDialogFragment {
/*Default*/
public static final String NUMBER_FORMAT = "%1d/%2d";
/*Util*/
/*Flag*/
private String title;
private int maxValue;
private int progress;
private int gravity;
private boolean widthMatch;
private String numberFormat;
private boolean showPercent;
private NumberFormat percentFormat;
/*View*/
private ProgressDialog progressDialog;
public static ProgressDialogFragment netInstance(String title, int maxValue) {
ProgressDialogFragment dialogFragment = new ProgressDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putInt("maxValue", maxValue);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
title = bundle.getString("title");
maxValue = bundle.getInt("maxValue");
gravity = bundle.getInt("gravity");
widthMatch = bundle.getBoolean("widthMatch");
numberFormat = bundle.getString("numberFormat");
showPercent = bundle.getBoolean("showPercent");
percentFormat = (NumberFormat) bundle.getSerializable("percentFormat");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
progressDialog = new ProgressDialog(requireActivity());
progressDialog.setProgress(progress);
if (numberFormat != null) {
progressDialog.setProgressNumberFormat(numberFormat);
} else {
progressDialog.setProgressNumberFormat(NUMBER_FORMAT);
}
if (showPercent) {
if (percentFormat == null) {
progressDialog.setProgressPercentFormat(NumberFormat.getPercentInstance());
} else {
progressDialog.setProgressPercentFormat(percentFormat);
}
} else {
progressDialog.setProgressPercentFormat(null);
}
progressDialog.setTitle(title);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(maxValue);
setCancelable(false);
updateGravity(progressDialog);
return progressDialog;
}
public void setProgress(int progress) {
this.progress = progress;
if (progressDialog != null) {
progressDialog.setProgress(progress);
}
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
if (widthMatch) {
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
}
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
public ProgressDialogFragment initNumberFormat(String numberFormat) {
this.numberFormat = numberFormat;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putString("numberFormat", numberFormat);
setArguments(bundle);
return this;
}
public ProgressDialogFragment initPercentFormat(boolean showPercent, NumberFormat numberFormat) {
this.showPercent = showPercent;
this.percentFormat = numberFormat;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("percentFormat", numberFormat);
bundle.putBoolean("showPercent", showPercent);
setArguments(bundle);
return this;
}
public ProgressDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public ProgressDialogFragment setterWidthMatch(boolean widthMatch) {
this.widthMatch = widthMatch;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("widthMatch", widthMatch);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.io.Serializable;
import java.util.Calendar;
/**
* 日期选择 dialog
*/
public class DatePickerDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
private DialogListener dialogListener;
private DatePickerDialog datePickerDialog;
/*Flag*/
private String title;
private boolean useCustomSet;
private int year;
private int month;
private int day;
private String positiveText;
private String negativeText;
private int gravity;
/*View*/
public interface DialogListener extends Serializable {
void onResult(int which, String tag, int year, int month, int dayOfMonth);
}
public static DatePickerDialogFragment netInstance() {
return netInstance(null, null, null);
}
public static DatePickerDialogFragment netInstance(String title, String positiveText, String negativeText) {
DatePickerDialogFragment dialogFragment = new DatePickerDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("positiveText", positiveText);
bundle.putString("negativeText", negativeText);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dialogListener = (DialogListener) bundle.getSerializable("listener");
title = bundle.getString("title");
positiveText = bundle.getString("positiveText");
negativeText = bundle.getString("negativeText");
useCustomSet = bundle.getBoolean("useCustomSet");
year = bundle.getInt("year");
month = bundle.getInt("month");
day = bundle.getInt("day");
gravity = bundle.getInt("gravity");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
int mYear = 0;
int mMonth = 0;
int mDay = 0;
if (useCustomSet) {
mYear = year;
mMonth = month;
mDay = day;
} else {
Calendar ca = Calendar.getInstance();
mYear = ca.get(Calendar.YEAR);
mMonth = ca.get(Calendar.MONTH);
mDay = ca.get(Calendar.DAY_OF_MONTH);
}
datePickerDialog = new DatePickerDialog(requireActivity(), null, mYear, mMonth, mDay);
datePickerDialog.setButton(AlertDialog.BUTTON_POSITIVE, (positiveText == null ? "确定" : positiveText), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int selectYear = datePickerDialog.getDatePicker().getYear();
int selectMonth = datePickerDialog.getDatePicker().getMonth();
int selectDay = datePickerDialog.getDatePicker().getDayOfMonth();
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_POSITIVE, getTag(), selectYear, selectMonth, selectDay);
}
}
});
datePickerDialog.setButton(AlertDialog.BUTTON_NEGATIVE, (negativeText == null ? "取消" : negativeText), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag(), 0, 0, 0);
}
}
});
datePickerDialog.setTitle(title);
updateGravity(datePickerDialog);
return datePickerDialog;
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag(), 0, 0, 0);
}
}
public DatePickerDialogFragment setterDialogListener(DialogListener listener) {
this.dialogListener = listener;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("listener", listener);
setArguments(bundle);
return this;
}
public DatePickerDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public DatePickerDialogFragment setterDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
this.useCustomSet = true;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("year", year);
bundle.putInt("month", month);
bundle.putInt("day", day);
bundle.putBoolean("useCustomSet", useCustomSet);
setArguments(bundle);
if (datePickerDialog != null) {
datePickerDialog.updateDate(year, month, day);
}
return this;
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
}
package com.demo.ck.dialogfragmentdemo.base;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TimePicker;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.demo.ck.dialogfragmentdemo.core.CoreDialogFragment;
import java.io.Serializable;
import java.util.Calendar;
/**
* 时间选择 dialog
*/
public class TimePickerDialogFragment extends CoreDialogFragment {
/*Default*/
/*Util*/
private DialogListener dialogListener;
private TimePickerDialog timePickerDialog;
/*Flag*/
private String title;
private boolean useCustomSet;
private int hourOfDay;
private int minOfHour;
private boolean is24Hour;
private String positiveText;
private String negativeText;
private int gravity;
/*View*/
public interface DialogListener extends Serializable {
void onResult(int which, String tag, int hourOfDay, int minOfHour);
}
public static TimePickerDialogFragment netInstance() {
return netInstance(null, null, null);
}
public static TimePickerDialogFragment netInstance(String title, String positiveText, String negativeText) {
TimePickerDialogFragment dialogFragment = new TimePickerDialogFragment();
Bundle bundle = new Bundle();
bundle.putString("title", title);
bundle.putString("positiveText", positiveText);
bundle.putString("negativeText", negativeText);
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
dialogListener = (DialogListener) bundle.getSerializable("listener");
title = bundle.getString("title");
positiveText = bundle.getString("positiveText");
negativeText = bundle.getString("negativeText");
useCustomSet = bundle.getBoolean("useCustomSet");
hourOfDay = bundle.getInt("hourOfDay");
minOfHour = bundle.getInt("minOfHour");
is24Hour = bundle.getBoolean("is24Hour");
gravity = bundle.getInt("gravity");
}
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
int mHourOfDay = 0;
int mMinOfHour = 0;
if (useCustomSet) {
mHourOfDay = hourOfDay;
mMinOfHour = minOfHour;
} else {
Calendar ca = Calendar.getInstance();
mHourOfDay = ca.get(Calendar.HOUR_OF_DAY);
mMinOfHour = ca.get(Calendar.MINUTE);
}
timePickerDialog = new TimePickerDialog(requireActivity(), new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_POSITIVE, getTag(), hourOfDay, minute);
}
}
}, mHourOfDay, mMinOfHour, is24Hour);
timePickerDialog.setButton(AlertDialog.BUTTON_POSITIVE, (positiveText == null ? "确定" : positiveText), (DialogInterface.OnClickListener) null);
timePickerDialog.setButton(AlertDialog.BUTTON_NEGATIVE, (negativeText == null ? "取消" : negativeText), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag(), 0, 0);
}
}
});
timePickerDialog.setTitle(title);
updateGravity(timePickerDialog);
return timePickerDialog;
}
@Override
public void onCancel(@NonNull DialogInterface dialog) {
super.onCancel(dialog);
if (dialogListener != null) {
dialogListener.onResult(AlertDialog.BUTTON_NEGATIVE, getTag(), 0, 0);
}
}
public TimePickerDialogFragment setterDialogListener(DialogListener listener) {
this.dialogListener = listener;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putSerializable("listener", listener);
setArguments(bundle);
return this;
}
public TimePickerDialogFragment setterGravity(int gravity) {
this.gravity = gravity;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("gravity", gravity);
setArguments(bundle);
updateGravity(getDialog());
return this;
}
public TimePickerDialogFragment setterTime(int hourOfDay, int minOfHour) {
this.hourOfDay = hourOfDay;
this.minOfHour = minOfHour;
this.useCustomSet = true;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putInt("hourOfDay", hourOfDay);
bundle.putInt("minOfHour", minOfHour);
bundle.putBoolean("useCustomSet", useCustomSet);
setArguments(bundle);
if (timePickerDialog != null) {
timePickerDialog.updateTime(hourOfDay, minOfHour);
}
return this;
}
public TimePickerDialogFragment initIs24Hour(boolean is24Hour) {
this.is24Hour = is24Hour;
Bundle bundle = getArguments();
if (bundle == null) {
bundle = new Bundle();
}
bundle.putBoolean("is24Hour", is24Hour);
setArguments(bundle);
return this;
}
private void updateGravity(Dialog dialog) {
if (dialog == null) {
return;
}
// 设置宽度为屏宽、位置在屏幕底部
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawableResource(android.R.color.white);
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams wlp = window.getAttributes();
if (gravity != 0) {
wlp.gravity = gravity;
} else {
wlp.gravity = Gravity.CENTER;
}
wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(wlp);
}
}
}
https://developer.android.google.cn/reference/android/app/DialogFragment
https://www.jianshu.com/p/ad1d77207ea2
https://github.com/codepath/android_guides/wiki/Using-DialogFragment
示例:https://github.com/TonyChen-9102/DialogFragmentDemo