|
@@ -8,6 +8,7 @@ import android.view.Gravity;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.widget.ImageView;
|
|
|
+import android.widget.TableLayout;
|
|
|
import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
@@ -15,132 +16,55 @@ import com.cooleshow.base.R;
|
|
|
|
|
|
public class ToastUtil {
|
|
|
|
|
|
- public static ToastUtil toast;
|
|
|
- private String toastStr;
|
|
|
- Toast toast1 = null;
|
|
|
- private TextView tv;
|
|
|
- private Handler handler;
|
|
|
+ public static ToastUtil instance;
|
|
|
+ private Toast toast;
|
|
|
|
|
|
public static ToastUtil getInstance() {
|
|
|
- if (null == toast) {
|
|
|
- toast = new ToastUtil();
|
|
|
+ if (null == instance) {
|
|
|
+ instance = new ToastUtil();
|
|
|
}
|
|
|
- return toast;
|
|
|
+ return instance;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 创建自定义Toasts :
|
|
|
*/
|
|
|
public void show(Context context, String toastStr) {
|
|
|
-
|
|
|
- if (null == context || TextUtils.isEmpty(toastStr)) {
|
|
|
- return;
|
|
|
+ if (init(toastStr, Toast.LENGTH_SHORT) == null) {
|
|
|
+ toast = new Toast(Utils.getApp());
|
|
|
+ toast.setText(toastStr);
|
|
|
+ toast.setDuration(Toast.LENGTH_SHORT);
|
|
|
+ toast.show();
|
|
|
}
|
|
|
- if (!TextUtils.isEmpty(this.toastStr) && toastStr.equals(this.toastStr)) {
|
|
|
- if (handler == null) {
|
|
|
- handler = new Handler();
|
|
|
- handler.postDelayed(() -> {
|
|
|
- ToastUtil.this.toastStr = null;
|
|
|
- handler = null;
|
|
|
- }, 2000);
|
|
|
- }
|
|
|
- return;
|
|
|
- }
|
|
|
- this.toastStr = toastStr;
|
|
|
- toast1 = null;
|
|
|
- toast1 = new Toast(context);
|
|
|
- View toastRoot = LayoutInflater.from(context).inflate(R.layout.my_toast, null);
|
|
|
- toast1.setView(toastRoot);
|
|
|
- toast1.setGravity(Gravity.CENTER, 0, 0);
|
|
|
- tv = toastRoot.findViewById(R.id.TextViewInfo);
|
|
|
-
|
|
|
- if (toastStr.length() > 15) {
|
|
|
- toast1.setDuration(Toast.LENGTH_LONG);
|
|
|
- } else {
|
|
|
- toast1.setDuration(Toast.LENGTH_SHORT);
|
|
|
- }
|
|
|
- if (tv != null) {
|
|
|
- tv.setText(toastStr);
|
|
|
- }
|
|
|
- toast1.show();
|
|
|
}
|
|
|
|
|
|
- public void cancel(){
|
|
|
- if(toast1 != null){
|
|
|
- toast1.cancel();
|
|
|
+ private Toast init(String message, int duration) {
|
|
|
+ try {
|
|
|
+ if (toast != null) {
|
|
|
+ TextView text = (TextView) toast.getView();
|
|
|
+ if (text != null) {
|
|
|
+ text.setText(message);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ int dp26 = SizeUtils.dp2px(26);
|
|
|
+ int dp10 = SizeUtils.dp2px(10);
|
|
|
+ TextView contentView = new TextView(Utils.getApp());
|
|
|
+ contentView.setGravity(Gravity.CENTER);
|
|
|
+ contentView.setBackgroundResource(R.drawable.shape_toast);
|
|
|
+ contentView.setTextSize(15);
|
|
|
+ contentView.setTextColor(Utils.getApp().getResources().getColor(R.color.white));
|
|
|
+ contentView.setPadding(dp26, dp10, dp26, dp10);
|
|
|
+ contentView.setText(message);
|
|
|
+ toast = new Toast(Utils.getApp());
|
|
|
+ toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
|
|
|
+ toast.setView(contentView);
|
|
|
+ }
|
|
|
+ toast.setDuration(duration);
|
|
|
+ toast.show();
|
|
|
+ } catch (Exception e) {
|
|
|
+ Toast.makeText(Utils.getApp(), message, duration).show();
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 创建自定义Toasts :
|
|
|
- */
|
|
|
- public void showSpecialScore(Context context, int score) {
|
|
|
-
|
|
|
-// if(toast1 != null){
|
|
|
-// toast1.cancel();
|
|
|
-// }
|
|
|
-// toast1 = null;
|
|
|
-// toast1 = new Toast(context);
|
|
|
-// View toastRoot = LayoutInflater.from(context).inflate(R.layout.layout_toast_score, null);
|
|
|
-// toast1.setView(toastRoot);
|
|
|
-// toast1.setGravity(Gravity.CENTER, 0, 0);
|
|
|
-// TextView tv_layout_toast_score = toastRoot.findViewById(R.id.tv_layout_toast_score);
|
|
|
-// toast1.setDuration(Toast.LENGTH_SHORT);
|
|
|
-// ImageView iv_layout_toast_score = toastRoot.findViewById(R.id.iv_layout_toast_score);
|
|
|
-//
|
|
|
-// if (score >= 80) {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_perfect);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#516AFF"));
|
|
|
-// } else if (score >= 60) {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_great);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#FF8E5A"));
|
|
|
-// } else if (score >= 40) {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_good);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#FF958B"));
|
|
|
-// } else {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_bad);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#EE4C6A"));
|
|
|
-// }
|
|
|
-// if (tv_layout_toast_score != null) {
|
|
|
-// tv_layout_toast_score.setText(score + "");
|
|
|
-// }
|
|
|
-// toast1.show();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 创建自定义Toasts :
|
|
|
- */
|
|
|
- public void showSpecialScore(Context context, int score, int x, int y) {
|
|
|
-//
|
|
|
-// if(toast1 != null){
|
|
|
-// toast1.cancel();
|
|
|
-// }
|
|
|
-// toast1 = null;
|
|
|
-// toast1 = new Toast(context);
|
|
|
-// View toastRoot = LayoutInflater.from(context).inflate(R.layout.layout_toast_score, null);
|
|
|
-// toast1.setView(toastRoot);
|
|
|
-// toast1.setGravity(Gravity.TOP | Gravity.LEFT, x, y);
|
|
|
-// TextView tv_layout_toast_score = toastRoot.findViewById(R.id.tv_layout_toast_score);
|
|
|
-// toast1.setDuration(Toast.LENGTH_SHORT);
|
|
|
-// ImageView iv_layout_toast_score = toastRoot.findViewById(R.id.iv_layout_toast_score);
|
|
|
-//
|
|
|
-// if (score >= 80) {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_perfect);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#516AFF"));
|
|
|
-// } else if (score >= 60) {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_great);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#FF8E5A"));
|
|
|
-// } else if (score >= 40) {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_good);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#FF958B"));
|
|
|
-// } else {
|
|
|
-// iv_layout_toast_score.setImageResource(R.mipmap.img_score_bad);
|
|
|
-// tv_layout_toast_score.setTextColor(Color.parseColor("#EE4C6A"));
|
|
|
-// }
|
|
|
-// if (tv_layout_toast_score != null) {
|
|
|
-// tv_layout_toast_score.setText(score + "");
|
|
|
-// }
|
|
|
-// toast1.show();
|
|
|
+ return toast;
|
|
|
}
|
|
|
}
|