1.toast.show();是展示的方法,toast.cancel();是取消的方法
代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //弹出toast提示 Toast toast = null; if (toast == null) { toast = Toast.makeText(mContext, "这里是提示", Toast.LENGTH_SHORT); } toast.show();//弹出提示 }
在activity被挂起,摧毁的时候,对弹出的toast提示进行处理.
/** * 处理onStop方法, * */ @Override protected void onStop() { super.onStop(); toast.cancel(); } /** * 处理onPause方法 * */ @Override protected void onPause() { super.onPause(); toast.cancel(); } /** * 处理onDestory方法 * */ @Override protected void onDestroy() { super.onDestroy(); toast.cancel(); }
多处使用简单的实现,自定义Toast的工具,然后再自定义的BaseActivity里面来实现
简单的Toast工具
public class ToastUtils { private static Toast mToast,mToastNormal; private static View view; /** * 普通的toast提示 * */ public static void showNOrmalToast(Context mContext,String message){ ToastUtils.cancel(); if(mToastNormal == null){ mToastNormal = Toast.makeText(mContext, message, Toast.LENGTH_SHORT); } mToastNormal.show(); } /** *toast取消 */ public static void cancel(){ if(mToast != null){ mToast.cancel(); mToast = null; } } }