当前位置:   article > 正文

Toast简单封装_android开发 tosat 自定义 封装

android开发 tosat 自定义 封装

1.在AndroidManifest.XML中声明这个MyApplication

android:name=".MyApplication"

2.自定义Application【系统上下文】

  1. import android.app.Application;
  2. import android.content.Context;
  3. public class MyApplication extends Application {
  4. /**系统上下文*/
  5. private static Context mAppContext;
  6. @Override
  7. public void onCreate() {
  8. super.onCreate();
  9. mAppContext = getApplicationContext();
  10. }
  11. /**获取系统上下文:用于ToastUtil类*/
  12. public static Context getAppContext(){
  13. return mAppContext;
  14. }
  15. }

3. Toast的封装

  1. import android.content.Context;
  2. import android.view.Gravity;
  3. import android.widget.Toast;
  4. /**
  5. * Used 简单的Toast封装类
  6. */
  7. public class ToastUtil {
  8. //实现不管我们触发多少次Toast调用,都只会持续一次Toast显示的时长
  9. private static Toast toast;
  10. /**
  11. * 短时间显示Toast【居下】
  12. * @param msg 显示的内容-字符串
  13. */
  14. public static void ToastBelowshow(String msg) {
  15. if (MyApplication.getAppContext() != null) {
  16. if (toast == null) {
  17. toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
  18. } else {
  19. toast.setText(msg);
  20. }
  21. //1、setGravity方法必须放到这里,否则会出现toast始终按照第一次显示的位置进行显示(比如第一次是在底部显示,那么即使设置setGravity在中间,也不管用)
  22. //2、虽然默认是在底部显示,但是,因为这个工具类实现了中间显示,所以需要还原,还原方式如下:
  23. toast.setGravity(Gravity.BOTTOM, 0, dip2px(MyApplication.getAppContext(), 64));
  24. toast.show();
  25. }
  26. }
  27. /**
  28. * 短时间显示Toast【居中】
  29. * @param msg 显示的内容-字符串
  30. */
  31. public static void ToastCentershow(String msg) {
  32. if (MyApplication.getAppContext() != null) {
  33. if (toast == null) {
  34. toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
  35. } else {
  36. toast.setText(msg);
  37. }
  38. toast.setGravity(Gravity.CENTER, 0, 0);
  39. toast.show();
  40. }
  41. }
  42. /**
  43. * 短时间显示Toast【居上】
  44. * @param msg 显示的内容-字符串
  45. */
  46. public static void ToastTopshow(String msg) {
  47. if (MyApplication.getAppContext() != null) {
  48. if (toast == null) {
  49. toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
  50. } else {
  51. toast.setText(msg);
  52. }
  53. toast.setGravity(Gravity.TOP, 0, 0);
  54. toast.show();
  55. }
  56. }
  57. /**
  58. * 长时间显示Toast【居下】
  59. * @param msg 显示的内容-字符串
  60. */
  61. public static void ToastBelowshowLong(String msg) {
  62. if (MyApplication.getAppContext() != null) {
  63. if (toast == null) {
  64. toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
  65. } else {
  66. toast.setText(msg);
  67. }
  68. toast.setGravity(Gravity.BOTTOM, 0, dip2px(MyApplication.getAppContext(), 64));
  69. toast.show();
  70. }
  71. }
  72. /**
  73. * 长时间显示Toast【居中】
  74. * @param msg 显示的内容-字符串
  75. */
  76. public static void ToastCentershowLong(String msg) {
  77. if (MyApplication.getAppContext() != null) {
  78. if (toast == null) {
  79. toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
  80. } else {
  81. toast.setText(msg);
  82. }
  83. toast.setGravity(Gravity.CENTER, 0, 0);
  84. toast.show();
  85. }
  86. }
  87. /**
  88. * 长时间显示Toast【居上】
  89. * @param msg 显示的内容-字符串
  90. */
  91. public static void ToastTopshowLong(String msg) {
  92. if (MyApplication.getAppContext() != null) {
  93. if (toast == null) {
  94. toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
  95. } else {
  96. toast.setText(msg);
  97. }
  98. toast.setGravity(Gravity.TOP, 0, 0);
  99. toast.show();
  100. }
  101. }
  102. /*=================================常用公共方法============================*/
  103. public static int dip2px(Context context, float dpValue) {
  104. final float scale = context.getResources().getDisplayMetrics().density;
  105. return (int) (dpValue * scale + 0.5f);
  106. }
  107. //ToastUtil.ToastTopshow(MyApplication.getAppContext().getResources().getString(R.string.app_name));
  108. // 如果想要显示Strings.xml文件中的字符串,建议使用MyApplication.getAppContext()
  109. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/305066
推荐阅读
相关标签
  

闽ICP备14008679号