超级简单的使用
DialogShow.showRoundProcessDialog(this, "加载数据...");
。。。
DialogShow.closeDialog();
效果图
1 代码
public class DialogShow {
//
// private static Dialog mDialog;
//
// private static View dialog;
// private static TextView mText;
//
// /**
// * 公用网络请求的dialog
// *
// * @param mContext
// */
// @SuppressLint("InflateParams")
// public static void showRoundProcessDialog(Context mContext) {//这里mContext为Activity
// closeDialog();
// if (mContext == null) {
// return;
// }
// mDialog = new Dialog(mContext, R.style.dialog);
// OnKeyListener keyListener = new OnKeyListener() {
// @Override
// public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
// mDialog.dismiss();
// }
// if (keyCode == KeyEvent.KEYCODE_BACK) {
// mDialog.dismiss();
// }
// return false;
// }
// };
// dialog = LayoutInflater.from(mContext).inflate(R.layout.loading_process_dialog_icon,
// null);
// mDialog.setCanceledOnTouchOutside(false);
// mDialog.setOnKeyListener(keyListener);
// mDialog.setCancelable(false);
// mDialog.setContentView(dialog);
// mDialog.show();
// }
//
// /**
// * 提示信息的dialog
// */
// @SuppressLint("InflateParams")
// public static void showRoundProcessDialog(Context mContext, String DialogInfo) {
// closeDialog();
// if (mContext == null) {
// return;
// }
// OnKeyListener keyListener = new OnKeyListener() {
// @Override
// public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
// mDialog.dismiss();
// }
// if (keyCode == KeyEvent.KEYCODE_BACK) {
// mDialog.dismiss();
// }
// return false;
// }
// };
// mDialog = new Dialog(mContext, R.style.dialog);
// dialog = LayoutInflater.from(mContext).inflate(R.layout.loading_process_dialog_icon,
// null);
// mText = (TextView) dialog.findViewById(R.id.loading_process_dialog_text);
// mText.setText(DialogInfo);
// mDialog.setCanceledOnTouchOutside(false);
// mDialog.setOnKeyListener(keyListener);
// mDialog.setCancelable(false);
// mDialog.setContentView(dialog);
// mDialog.show();
// }
//
// public static void closeDialog() {
// if (null != mDialog && mDialog.isShowing()) {
// mDialog.dismiss();
// }
// }
//
// public static boolean isShow() {
// if (mDialog == null)
// return false;
// return mDialog.isShowing();
// }
//
// public static void ChangeProgerss(double percent) {
// if (mDialog == null)
// return;
// if (mDialog.isShowing()) {
// mText.setText("上传中..." + (int)(percent * 100) + "%");
// }
// }
private static WeakReference<Dialog> mDialog;
// private static WeakReference<View> dialog;
// private static WeakReference<TextView> mTextView;
/**
* 公用网络请求的dialog
*
* @param mContext
*/
@SuppressLint("InflateParams")
public static void showRoundProcessDialog(Context mContext) {//这里mContext为Activity
showRoundProcessDialog(mContext,null);
}
/**
* 提示信息的dialog
*/
@SuppressLint("InflateParams")
public static void showRoundProcessDialog(Context mContext, String DialogInfo) {
closeDialog();
if (mContext == null) {
return;
}
mDialog = new WeakReference<>(new Dialog(mContext, R.style.dialog));
DialogInterface.OnKeyListener keyListener = (dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if (mDialog.get() != null) {
mDialog.get().dismiss();
}
}
if (mDialog.get() != null) {
mDialog.get().dismiss();
}
return false;
};
View view = LayoutInflater.from(mContext).inflate(R.layout.loading_process_dialog_icon,
null);
Dialog realDialog = mDialog.get();
if (realDialog != null ) {
if (!TextUtils.isEmpty(DialogInfo)) {
TextView mText = view.findViewById(R.id.loading_process_dialog_text);
mText.setText(DialogInfo);
}
realDialog.setCanceledOnTouchOutside(false);
realDialog.setOnKeyListener(keyListener);
realDialog.setCancelable(false);
realDialog.setContentView(view);
realDialog.show();
}
}
public static void closeDialog() {
if (mDialog == null){
return;
}
if (mDialog.get() == null){
return;
}
if (mDialog.get().isShowing()){
mDialog.get().dismiss();
}
}
public static boolean isShow() {
if (mDialog == null){
return false;
}
if (mDialog.get() == null){
return false;
}
return mDialog.get().isShowing();
}
// public static void ChangeProgerss(double percent) {
// Dialog dialog = mDialog.get();
// if (dialog == null)
// return;
// if (dialog.isShowing() && mTextView.get() != null) {
// String process = "上传中..." + (int) (percent * 100) + "%";
// mTextView.get().setText(process);
// }
// }
}
2 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/dialog_show_bg"
android:padding="@dimen/qb_px_10"
>
<ProgressBar
android:id="@+id/loading_process_dialog_progressBar"
android:layout_width="44dip"
android:layout_height="44dip"
android:layout_centerHorizontal="true"
android:layout_marginLeft="50dp"
android:indeterminate="false"
android:indeterminateDrawable="@drawable/progress_circle_shape"
/>
<TextView
android:id="@+id/loading_process_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/loading_process_dialog_progressBar"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="正在加载..."
android:textColor="@color/white"
/>
</RelativeLayout>
3 dialog_show_bg
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#808080" />
<corners
android:bottomLeftRadius="13dp"
android:bottomRightRadius="13dp"
android:topLeftRadius="13dp"
android:topRightRadius="13dp" />
<stroke
android:width="1dip"
android:color="#808080" />
</shape>
4 progress_circle_shape
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360"
>
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false"
>
<gradient
android:centerColor="#FFFFFF"
android:centerY="0.50"
android:endColor="#1E90FF"
android:startColor="#000000"
android:type="sweep"
android:useLevel="false"
></gradient>
</shape>
</rotate>