package com.basic.security.utils;
|
|
import android.content.Context;
|
import android.view.Gravity;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.basic.security.base.BaseApplication;
|
|
class MyToast {
|
private final TextView textView;
|
private Toast mToast;
|
private boolean isShow = false;
|
|
private MyToast(Context context, CharSequence text, int duration) {
|
View v = LayoutInflater.from(context).inflate(com.basic.security.utils.ResolutionAdaptation.eplay_toast(), null);
|
textView = v.findViewById(com.basic.security.utils.RUtils.R_id_textView1);
|
textView.setText(text);
|
mToast = new Toast(context);
|
mToast.setDuration(duration);
|
mToast.setView(v);
|
}
|
|
public static MyToast makeText(Context context, CharSequence text, int duration) {
|
return new MyToast(context, text, duration);
|
}
|
|
public void setText(CharSequence text) {
|
if (textView != null) {
|
textView.setText(text);
|
}
|
}
|
|
public void show() {
|
if (mToast != null) {
|
mToast.show();
|
}
|
}
|
|
public void setGravity(int gravity, int xOffset, int yOffset) {
|
if (mToast != null) {
|
mToast.setGravity(gravity, xOffset, yOffset);
|
}
|
}
|
|
public void setDuration(int duration) {
|
if (mToast != null) {
|
mToast.setDuration(duration);
|
}
|
}
|
}
|
|
public class ToastUtil {
|
private static Toast toast;
|
private static Toast toast_short;
|
private static MyToast toast_down;
|
private static MyToast toast_long;
|
|
public static void showNormalToast(final String message) {
|
View v = LayoutInflater.from(BaseApplication.getApplication()).inflate(com.basic.security.utils.ResolutionAdaptation.eplay_toast(), null);
|
TextView textView = v.findViewById(com.basic.security.utils.RUtils.R_id_textView1);
|
textView.setText(message);
|
if (Thread.currentThread().getId() == 1) {
|
if (toast == null) {
|
toast = new Toast(BaseApplication.getApplication());
|
toast.setView(v);
|
toast.setDuration(Toast.LENGTH_LONG);
|
} else {
|
toast.setView(v);
|
}
|
toast.setGravity(Gravity.CENTER, 0, 0);
|
toast.show();
|
} else {
|
BaseApplication.getApplication().activity.runOnUiThread(() -> {
|
if (toast == null) {
|
toast = new Toast(BaseApplication.getApplication());
|
toast.setView(v);
|
toast.setDuration(Toast.LENGTH_LONG);
|
} else {
|
toast.setView(v);
|
}
|
toast.setGravity(Gravity.CENTER, 0, 0);
|
toast.show();
|
});
|
}
|
}
|
|
public static void show(final String message) { // 有多次连续点击不显示问题 参照showNormalToast,
|
System1.out.println("toast:" + message);
|
View v = LayoutInflater.from(BaseApplication.getApplication()).inflate(com.basic.security.utils.ResolutionAdaptation.eplay_toast(), null);
|
TextView textView = v.findViewById(com.basic.security.utils.RUtils.R_id_textView1);
|
textView.setText(message);
|
if (Thread.currentThread().getId() == 1) {
|
if (toast == null) {
|
toast = new Toast(BaseApplication.getApplication());
|
toast.setView(v);
|
toast.setDuration(Toast.LENGTH_LONG);
|
} else {
|
toast.setView(v);
|
}
|
toast.setGravity(Gravity.CENTER, 0, 0);
|
toast.show();
|
} else {
|
BaseApplication.getApplication().activity.runOnUiThread(() -> {
|
if (toast == null) {
|
toast = new Toast(BaseApplication.getApplication());
|
toast.setView(v);
|
toast.setDuration(Toast.LENGTH_LONG);
|
} else {
|
toast.setView(v);
|
}
|
toast.setGravity(Gravity.CENTER, 0, 0);
|
toast.show();
|
});
|
}
|
// if (Thread.currentThread().getId() == 1) {
|
// if (toast_short == null) {
|
// toast_short = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
|
// } else {
|
// toast_short.setText(message);
|
// toast_short.setDuration(Toast.LENGTH_SHORT);
|
// }
|
// toast_short.setGravity(Gravity.CENTER, 0, 0);
|
// toast_short.show();
|
// } else {
|
// BaseApplication.getApplication().activity.runOnUiThread(new Runnable() {
|
//
|
// public void run() {
|
// if (toast_short == null) {
|
// toast_short = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
|
// toast_short.setGravity(Gravity.CENTER, 0, 0);
|
// } else {
|
// toast_short.setText(message);
|
// }
|
// toast_short.show();
|
// }
|
// });
|
// }
|
}
|
|
public static void showInDown(final String message) {
|
if (Thread.currentThread().getId() == 1) {
|
if (toast_down == null) {
|
toast_down = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
|
toast_down.setGravity(Gravity.CENTER, 0, 120);
|
} else {
|
toast_down.setText(message);
|
}
|
toast_down.show();
|
} else {
|
BaseApplication.getApplication().activity.runOnUiThread(() -> {
|
if (toast_down == null) {
|
toast_down = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_SHORT);
|
toast_down.setGravity(Gravity.CENTER, 0, 120);
|
} else {
|
toast_down.setText(message);
|
}
|
toast_down.show();
|
});
|
}
|
}
|
|
public static void showLong(final String message) {
|
if (Thread.currentThread().getId() == 1) {
|
if (toast_long == null) {
|
toast_long = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_LONG);
|
toast_long.setGravity(Gravity.CENTER, 0, 0);
|
} else {
|
toast_long.setText(message);
|
}
|
toast_long.show();
|
} else {
|
BaseApplication.getApplication().activity.runOnUiThread(() -> {
|
if (toast_long == null) {
|
toast_long = MyToast.makeText(BaseApplication.getApplication().activity, message, Toast.LENGTH_LONG);
|
toast_long.setGravity(Gravity.CENTER, 0, 0);
|
} else {
|
toast_long.setText(message);
|
}
|
toast_long.show();
|
});
|
}
|
}
|
}
|