1
0

ToastUtils.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. public class ToastUtils {
  11. private static Toast mToast;
  12. private static Handler mHandler = new Handler();
  13. private static Runnable runnable = new Runnable() {
  14. public void run() {
  15. mToast.cancel();
  16. //toast隐藏后,将其置为null
  17. mToast=null;
  18. }
  19. };
  20. public static void make(Context context, String msg) {
  21. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  22. //自定义布局
  23. View view = inflater.inflate(R.layout.custom_toast, null);
  24. TextView text = (TextView) view.findViewById(R.id.toast_message);
  25. //显示的提示文字
  26. text.setText(msg);
  27. mHandler.removeCallbacks(runnable);
  28. mToast = new Toast(context);
  29. mToast.setDuration(Toast.LENGTH_SHORT);
  30. mToast.setGravity(Gravity.CENTER, 0, -22);
  31. mToast.setView(view);
  32. mToast.show();
  33. }
  34. }