Blame view

src/com/ectrip/cyt/utils/ToastUtils.java 4.53 KB
90601e4f   黄灿宏   标准版本 1.扩展设备10 增...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  package com.ectrip.cyt.utils;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.ContextWrapper;
  import android.os.Handler;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.PopupWindow;
  import android.widget.TextView;
  
  import com.ectrip.trips.check.R;
  
  
  /**
   * Created by dc on 2017/3/9.
   */
  
  public class ToastUtils {
      public static final int LENGTH_LONG = 1;
      public static final int LENGTH_SHORT = 0;
      private static ToastUtils toastUtils;
      private static View layout2;
      private static TextView tv_info;
      private static Activity mActivity;
      private static Handler handler;
      private static Runnable runnable;
      private static PopupWindow popupWindow2;
  
  
      public static ToastUtils with(Activity activity) {
          mActivity = activity;
          layout2 = LayoutInflater.from(activity).inflate(
                  R.layout.toastutils_view, null);
          tv_info = (TextView) layout2.findViewById(R.id.poptext);
          if (handler == null) {
              handler = new Handler();
          }
          if (runnable == null) {
              runnable = new Runnable() {
                  @Override
                  public void run() {
                      if (popupWindow2 != null) {
                          popupWindow2.dismiss();
                          popupWindow2 = null;
                      }
                  }
              };
          }
          if (popupWindow2 != null) {
              if (handler != null && runnable != null) {
                  handler.removeCallbacks(runnable);
              }
              popupWindow2.dismiss();
              popupWindow2 = null;
          }
          popupWindow2 = new PopupWindow(layout2);
  
          if (toastUtils == null) {
              toastUtils = new ToastUtils();
          }
          return toastUtils;
      }
  
      /**
       * @param text     提示信息
       * @param resource 显示的布局界面
       * @param duration 显示时长设置
       */
  
      public void makeText(String text, int resource, int duration) {
  
  
          // 设置弹框的宽度为屏幕宽度
          popupWindow2.setWidth(DenstityUtils.getScreenWidth(mActivity));
          popupWindow2.setHeight(DenstityUtils.dp2px(mActivity, 80));
          // 设置显示消失的动画
          popupWindow2.setAnimationStyle(R.style.TopSelectAnimationShow);
          // 设置点击弹框外部,弹框不消失
          popupWindow2.setOutsideTouchable(false);
          popupWindow2.setFocusable(false);
          popupWindow2.setContentView(layout2);
          // 设置弹框出现的位置,在v的正下方横轴偏移textview的宽度,为了对齐~纵轴不偏移
          if (popupWindow2.isShowing()) {
  //            popupWindow2.dismiss();
          }
          try {
              popupWindow2.showAtLocation(LayoutInflater.from(mActivity).inflate(resource, null),
                      Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
  
          tv_info.setText(text);
          // 显示时长
          long time;
          if (duration == LENGTH_LONG) {
              time = 3500;
          } else if (duration == LENGTH_SHORT) {
              time = 1000;
          } else {
              time = 1500;
          }
  
          handler.postDelayed(runnable, time);
      }
  
  
      public static void toast(final Activity activity, final String msg, final int resourt) {
          activity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                  try {
                      with(activity).makeText(msg, resourt, LENGTH_SHORT);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
      }
  
      public static void toast(final Context mContext, final String msg) {
          final Activity mActivity = getActivity(mContext);
          if (mActivity != null) {
              mActivity.runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          with(mActivity).makeText(msg, R.layout.app_welcome, 2);
                      } catch (Exception e) {
  
                      }
                  }
              });
          }
      }
  
      private static Activity getActivity(Context mContext) {
  
          if (mContext instanceof Activity) {
              return (Activity) mContext;
          } else if (mContext instanceof ContextWrapper) {
              mContext = ((ContextWrapper) mContext).getBaseContext();
              return getActivity(mContext);
          } else {
              return null;
          }
      }
  }