话不多说,参考github地址:https://github.com/mamumu/mmDialog
添加Add the dependency貌似不好使,直接把库加载进来了
dependencies {
implementation 'com.github.mamumu:mmDialog:1.2.0'
}
github上说的方法不好用,
直接依赖库后,:
package com.kexinlib.base.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.widget.Toast;
import com.mumu.dialog.MMAlertDialog;
import com.mumu.dialog.MMLoading;
import com.mumu.dialog.MMToast;
public class IosDialog {
private MMLoading mmLoading;
private static MMToast mmToast;
private AlertDialog alertDialog;
private Context context;
private String msg;
private boolean autoCancle;
private boolean showMsg;
private String cancleText;
private String confirmText;
private IosDialog mySelf;
public IosDialog(Context context) {
this.context = context;
msg = "loading...";
autoCancle = true;
showMsg = true;
cancleText = "取消";
confirmText = "确定";
mySelf = this;
}
public IosDialog setMsg(String msgs) {
this.msg = msgs;
if (TextUtils.isEmpty(msg)){
showMsg = false;
}
return this;
}
public IosDialog setAutoCancle(boolean autoCancle) {
this.autoCancle = autoCancle;
return this;
}
public IosDialog setShowMsg(boolean showMsg) {
this.showMsg = showMsg;
return this;
}
public void loading(){
if (mmLoading == null) {
MMLoading.Builder builder = new MMLoading.Builder(context)
.setMessage(msg)
.setShowMessage(showMsg)
.setCancelable(autoCancle)
.setCancelOutside(autoCancle);
mmLoading = builder.create();
}else {
mmLoading.dismiss();
MMLoading.Builder builder = new MMLoading.Builder(context)
.setMessage(msg)
.setShowMessage(showMsg)
.setCancelable(autoCancle)
.setCancelOutside(autoCancle);
mmLoading = builder.create();
}
mmLoading.show();
}
public void hideLoading() {
if (mmLoading != null && mmLoading.isShowing()) {
mmLoading.dismiss();
}
}
public static void showToastSuccess(Context context,String msg) {
showToast(context,msg,true);
}
public static void showToastFailure(Context context,String msg) {
showToast(context,msg,false);
}
private static void showToast(Context context,String msg,boolean success){
if (mmToast == null) {
MMToast.Builder builder=new MMToast.Builder(context)
.setMessage(msg)
.setSuccess(success);
mmToast=builder.create();
}else {
mmToast.cancel();
MMToast.Builder builder=new MMToast.Builder(context)
.setMessage(msg)
.setSuccess(success);
mmToast=builder.create();
}
mmToast.show();
}
public IosDialog setCancleText(String cancleText) {
this.cancleText = cancleText;
return this;
}
public IosDialog setConfirmText(String confirmText) {
this.confirmText = confirmText;
return this;
}
public void showDialog() {
showDialog2();
}
private void showDialog2() {
dismiss();
DialogInterface.OnClickListener clickListener = null;
if (!TextUtils.isEmpty(cancleText)){
clickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
if (onClickListener!=null){
onClickListener.onCancle();
}
}
};
}
alertDialog = MMAlertDialog.showDialog(context,
"",
msg,
cancleText,
confirmText,
autoCancle,
clickListener,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onClickListener!=null){
onClickListener.onConfirm(mySelf);
}
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (onClickListener!=null){
onClickListener.onDismiss();
}
}
});
}
private OnClickListener onClickListener;
public IosDialog setOnClickListener(OnClickListener onClickListener) {
this.onClickListener = onClickListener;
return this;
}
public void dismiss(){
if (alertDialog!=null){
alertDialog.dismiss();
}
}
public static class OnClickListener{
public void onCancle(){}
public void onConfirm(IosDialog iosDialog){}
public void onDismiss(){}
}
}
package im.kexin.app.messenger.utils;
import android.content.Context;
import android.view.View;
import android.widget.Toast;
import com.kexinlib.base.utils.IosDialog;
/**
* Toast统一管理类
*
*/
public class Toasts
{
private static Toast toast;
public static void show(Context context,
String content, int dur) {
if (toast == null) {
toast = Toast.makeText(context,
content,
dur);
} else {
View view = toast.getView();
toast.cancel();
toast= new Toast(context);
toast.setView(view);
toast.setDuration(dur);
toast.setText(content);
}
toast.show();
}
public static void showTips(Context context,
String content) {
if (toast == null) {
toast = Toast.makeText(context,
content,
Toast.LENGTH_SHORT);
} else {
View view = toast.getView();
toast.cancel();
toast= new Toast(context);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setText(content);
}
toast.show();
}
public static void show(Context context,
String content) {
show(context,content,false);
}
public static void show(Context context,
String content,boolean isSuccess) {
if (isSuccess){
IosDialog.showToastSuccess(context,content);
}else {
IosDialog.showToastFailure(context,content);
}
}
/*public static void show(Context context,
String content) {
if (toast == null) {
toast = Toast.makeText(context,
content,
Toast.LENGTH_SHORT);
} else {
View view = toast.getView();
toast.cancel();
toast= new Toast(context);
toast.setView(view);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setText(content);
}
toast.show();
}*/
}
所以最后在调用的时候,直接通过调用Toast调用
在访问接口的时候,成功弹出的吐司,是调用三个参数的方法,如果是失败弹出的吐司,那么就是调用两个参数的方法,参考如下:
失败的时候传入两个参数,
Toasts.show(mContext,LocaleController.formatString("search_user_none",R.string.search_user_none));
成功的时候,传入三个参数,最后一个传入true
Toasts.show(mContext,str,true);
访问网络加载数据效果转圈:
package im.kexin.app.messenger.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import com.android.tu.loadingdialog.LoadingDailog;
import com.kexinlib.base.utils.IosDialog;
import org.telegram.messenger.LocaleController;
import im.kexin.app.messenger.R;
public class LoadingDialogUtils {
public static IosDialog dialog_ios;
public static void dismissDialog_ios(){
if (dialog_ios!=null){
try {
dialog_ios.hideLoading();
} catch (Exception e) {
e.printStackTrace();
}
dialog_ios=null;
}
}
/**
* 仿iso加载效果
*/
public static void show(Context context){
show(context,true);
}
public static void show(Context context,boolean autoDismiss){
show(context,"Loading...",autoDismiss);
}
public static void showNoText(Context context,boolean autoDismiss){
if (dialog_ios!=null){
dialog_ios.hideLoading();
}
dialog_ios = new IosDialog(context).setAutoCancle(autoDismiss).setShowMsg(false);
dialog_ios.loading();
}
public static void showNoText(Context context){
showNoText(context,true);
}
/**
* 仿iso加载效果
*/
public static void show(Context context, String info){
show(context,info,true);
}
public static void show(Context context, String info,boolean autoDismiss){
if (dialog_ios!=null){
dialog_ios.hideLoading();
}
dialog_ios = new IosDialog(context).setAutoCancle(autoDismiss).setMsg(info);
dialog_ios.loading();
}
private String title;
private String message;
private boolean showCancle;
private OnClickListener onClickListener;
private Context context;
public LoadingDialogUtils(Context context) {
this.context = context;
this.showCancle = true;
}
public LoadingDialogUtils title(String t) {
this.title = t;
return this;
}
public LoadingDialogUtils message(String t) {
this.message = t;
return this;
}
public LoadingDialogUtils showCancle(boolean b) {
this.showCancle = b;
return this;
}
public LoadingDialogUtils listener(OnClickListener t) {
this.onClickListener = t;
return this;
}
OnDisMissListener onDisMissListener;
public LoadingDialogUtils disMissListener(OnDisMissListener listener){
onDisMissListener = listener;
return this;
}
private IosDialog alertDialog;
public void disMiss(){
if (alertDialog!=null){
alertDialog.dismiss();
alertDialog = null;
}
}
public void show(){
alertDialog = new IosDialog(context)
.setMsg(message)
.setCancleText(showCancle ? LocaleController.formatString("",R.string.cancle) : null)
.setConfirmText(LocaleController.formatString("",R.string.confirm))
.setOnClickListener(new IosDialog.OnClickListener(){
@Override
public void onCancle() {
if (onClickListener!=null){
onClickListener.onCancle();
}
}
@Override
public void onConfirm(IosDialog iosDialog) {
iosDialog.dismiss();
if (onClickListener!=null){
onClickListener.onConfirm();
}
}
@Override
public void onDismiss() {
if (onDisMissListener!=null){
onDisMissListener.ondismiss();
}
}
});
alertDialog.showDialog();
}
public interface OnDisMissListener{
void ondismiss();
}
public static class OnClickListener{
public void onConfirm(){}
public void onCancle(){}
}
}
调用方法:
调用接口方法之前第一行:
private void getAll() {
LoadingDialogUtils.showNoText(mContext);
获取数据成功之后第一行:
LoadingDialogUtils.dismissDialog_ios();
获取数据失败之后第一行:
LoadingDialogUtils.dismissDialog_ios();