ToastUtil【简单的Toast封装类】【未自定义Toast的显示风格】

版权声明:本文为HaiyuKing原创文章,转载请注明出处!

前言

一个简单的Toast封装类。

效果图

API = 6.0 API = 4.4.2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

代码分析

  • 实现了不管我们触发多少次Toast调用,都只会持续一次Toast显示的时长;
  • 使用了系统上下文,防止当前Activity未加载完成就退出时,程序崩溃的情况;
  • 缺陷:只是调用系统的Toast显示,未自定义Toast的显示风格(机型不同,显示的样式也就不同)。

使用步骤

一、项目组织结构图

注意事项:

1、复制ToastUtil类文件后需要重新import MyApplication路径

二、导入步骤

创建一个包含以下代码的MyApplication.java(自定义的Application子类)

/**
 * Used 自定义Application【系统上下文】
 */
public class MyApplication extends Application{
    
    /**系统上下文*/
    private static Context mAppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = getApplicationContext();

    }

    /**获取系统上下文:用于ToastUtil类*/
    public static Context getAppContext()
    {
        return mAppContext;
    }
}

在AndroidManifest.xml中声明这个MyApplication

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.why.project.toastutildemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".MyApplication">
        <activity android:name=".activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

在{package}.util(比如,com.why.project.toastutildemo.util)包下新建一个ToastUtil.java

package com.why.project.toastutildemo.util;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import com.why.project.toastutildemo.MyApplication;

/**
 * Create By HaiyuKing
 * Used 简单的Toast封装类
 */
public class ToastUtil {
    
    private static Toast toast;//实现不管我们触发多少次Toast调用,都只会持续一次Toast显示的时长
    
    /**
     * 短时间显示Toast【居下】
     * @param msg 显示的内容-字符串*/
    public static void showShortToast(String msg) {
        if(MyApplication.getAppContext() != null){
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            //1、setGravity方法必须放到这里,否则会出现toast始终按照第一次显示的位置进行显示(比如第一次是在底部显示,那么即使设置setGravity在中间,也不管用)
            //2、虽然默认是在底部显示,但是,因为这个工具类实现了中间显示,所以需要还原,还原方式如下:
            toast.setGravity(Gravity.BOTTOM, 0, dip2px(MyApplication.getAppContext(),64));
            toast.show();
        }
    }
    /**
     * 短时间显示Toast【居中】
     * @param msg 显示的内容-字符串*/
    public static void showShortToastCenter(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    /**
     * 短时间显示Toast【居上】
     * @param msg 显示的内容-字符串*/
    public static void showShortToastTop(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
    }
    
    /**
     * 长时间显示Toast【居下】
     * @param msg 显示的内容-字符串*/
    public static void showLongToast(String msg) {
        if(MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.BOTTOM, 0, dip2px(MyApplication.getAppContext(),64));
            toast.show();
        }
    }
    /**
     * 长时间显示Toast【居中】
     * @param msg 显示的内容-字符串*/
    public static void showLongToastCenter(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }
    /**
     * 长时间显示Toast【居上】
     * @param msg 显示的内容-字符串*/
    public static void showLongToastTop(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toast == null) {
                toast = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.setGravity(Gravity.TOP, 0, 0);
            toast.show();
        }
    }

    /*=================================常用公共方法============================*/
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

 

三、使用方法

ToastUtil.showShortToast("ToastUtilDemo");//如果显示字符串,直接写即可
ToastUtil.showShortToast(MyApplication.getAppContext().getResources().getString(R.string.app_name));//如果想要显示Strings.xml文件中的字符串,建议使用MyApplication.getAppContext()

如果是在Fragment、DialogFragment中调用ToastUtil类,则建议使用下面的方式

if(isAdded()) {//如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回false。
  ToastUtil.showShortToast(MyApplication.getAppContext().getResources().getString(R.string.app_name)); 
}

混淆配置

参考资料

暂时空缺

项目demo下载地址

https://github.com/haiyuKing/ToastUtilDemo

 

posted @ 2017-05-07 21:23  HaiyuKing  阅读(9635)  评论(0编辑  收藏  举报