0
0

ToastUtils.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package com.emato.ich.utils;
  2. import android.content.Context;
  3. import android.os.Handler;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import com.emato.ich.R;
  10. /**
  11. * 弹出框工具类
  12. */
  13. public class ToastUtils {
  14. private static Toast mToast;
  15. private static Handler mHandler = new Handler();
  16. // private static Runnable runnable = new Runnable() {
  17. // public void run() {
  18. // mToast.cancel();
  19. // //toast隐藏后,将其置为null
  20. // mToast=null;
  21. // }
  22. // };
  23. /**
  24. * 弹出框
  25. * @param context 程序上下文
  26. * @param msg 弹出信息
  27. */
  28. public static void make(Context context, String msg) {
  29. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  30. //自定义布局
  31. View view = inflater.inflate(R.layout.custom_toast, null);
  32. TextView text = (TextView) view.findViewById(R.id.toast_message);
  33. //显示的提示文字
  34. text.setText(msg);
  35. // mHandler.removeCallbacks(runnable);
  36. if (mToast != null) {
  37. mToast.cancel();
  38. }
  39. mToast = new Toast(context);
  40. mToast.setDuration(Toast.LENGTH_SHORT);
  41. mToast.setGravity(Gravity.CENTER, 0, -22);
  42. mToast.setView(view);
  43. mToast.show();
  44. // mHandler.removeCallbacks(runnable);
  45. }
  46. }