一、 ToastView.java

 1 import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class ToastView extends LinearLayout{
private static ToastView mToastView; private TextView toastText;
private ImageView toastIcon;
private Context mContext; private View layout;
private Toast mToast; private ToastView(Context context) {
super(context,null);
mContext = context;
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.toast, null); toastIcon = (ImageView) layout.findViewById(R.id.toastIcon);
toastText = (TextView) layout.findViewById(R.id.toastText);
} public static ToastView getInstance(Context context) {
if (mToastView != null) {
return mToastView;
} else {
if (context != null) {
mToastView = new ToastView(context);
return mToastView;
} else {
return null;
}
}
} public void setIconVisiblity(int visiblity){
toastIcon.setVisibility(visiblity);
} public void showToast(String str,int gravity,int xoffset,int yoffest) {
toastText.setText(str); if(mToast!=null){
mToast.setGravity(gravity,xoffset,yoffest);
mToast.show();
}else{
mToast= new Toast(mContext);
mToast.setView(layout);
mToast.setGravity(gravity,xoffset,yoffest);
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.show();
}
} public void showToast(String str,int gravity,int xoffset,int yoffest,int duration) {
toastText.setText(str);
duration = duration<=0 ? Toast.LENGTH_SHORT : duration; if(mToast!=null){
mToast.setGravity(gravity,xoffset,yoffest);
mToast.setDuration(duration);
mToast.show();
}else{
mToast= new Toast(mContext);
mToast.setView(layout);
mToast.setGravity(gravity,xoffset,yoffest);
mToast.setDuration(duration);
mToast.show();
}
} }

二、toast.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"> <TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="@dimen/toastHeight"
android:textColor="@color/title_text_color"
android:background="@drawable/toast_bg"
android:gravity="center"
android:paddingLeft="@dimen/toastTextPaddingLeft"
android:paddingRight="@dimen/toastTextPaddingRight"
android:textSize="@dimen/toastTextSize"/> <ImageView
android:id="@+id/toastIcon"
android:layout_width="@dimen/toastIconWidth"
android:layout_height="@dimen/toastIconWidth"
android:layout_marginLeft="@dimen/toastIconWidth"
android:scaleType="fitXY"
android:layout_centerVertical="true"
android:src="@drawable/toast_icon" />
</RelativeLayout>

三、Acitivity中调用

    //自定义的
private void showToast(String str, int offset) {
ToastView toast = ToastView.getInstance(RecommendActivity.this);
if(offset < 0){
toast.showToast(str, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,    //默认时长的Toast
(int)getResources().getDimension(R.dimen.defaultToastMarginTop));
}else{
toast.showToast(str, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0,    //自定义时长的Toast
(int)getResources().getDimension(R.dimen.defaultToastMarginTop), offset);
}
}

封装的这个Toast没什么技术含量。

但是,之所以把Toast定义为单例模式,是为了防止疯狂点击按钮,出现连续不断的Toast

因为系统的Toast是维护一个队列,每次cancle只对当前的Toast 有用。所以之后的Toast还是会不停出现

这个Toast改为单例模式之后。如果当前Toast不为空,只对其进行setText。然后show(不调用show不会出现)

如果当前为空,才会新建一个ToastView。

封装 Toast的更多相关文章

  1. React Native封装Toast与加载Loading组件

    React Native开发封装Toast与加载Loading组件 在App开发中,我们避免不了使用的两个组件,一个Toast,一个网络加载Loading,在RN开发中,也是一样,React Nati ...

  2. 原生JS封装 toast 弹层,自动关闭

    由于公司业务需求,要一个公共toast ,下面是自己封装的一个. css: .toast { text-align: center; min-height: 70px; width: 220px; c ...

  3. appium自动化测试中获取toast消息的解决方法【转】

    http://blog.csdn.net/hqzxsc2006/article/details/50036911 待实践.. 解决方法:appium下切换selendroid模式去获取Android的 ...

  4. appium桌面版本以及一些自动化测试方方封装

    appium_desktop 标签(空格分隔): appium_desktop 一 appium_desktop_v1.2.6 1.appium_desktop在github上最新下载地址:appiu ...

  5. appium之toast处理

    注意 toast要appium1.6.3以上版本才支持,Android 5.0以上(需使用夜神多开模拟器),jdk1.8且配置了环境变量. toast定位 1.先看下toast长什么样,如下图,像这种 ...

  6. 『与善仁』Appium基础 — 29、获取toast信息

    目录 1.toast介绍 2.toast定位 3.示例 4.封装toast判断 1.toast介绍 Android中的toast是一种简易的消息提示框,toast提示框不能被用户点击,会根据所设置的显 ...

  7. android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理

    本示例说明: 1.自定义listview条目样式,自定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,并且操作后listview自动刷新. 3.响应用户操作点击事 ...

  8. android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理

    本演示样例说明: 1.自己定义listview条目样式,自己定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,而且操作后listview自己主动刷新. 3.响应用 ...

  9. 分析BGARefreshLayout-master

    一.知识点,创建BaseActivity 建立方法的逻辑顺序 并将一些常用的方法填充到其中 ①.将initView().setListener().onClick().processLogic()方法 ...

随机推荐

  1. iOS中,Framework和.a的打包及使用

    最近在做一个小项目,需要给客户一个demo测试,有一部分核心代码暂时不想让客户知道,就想到了打包成framework或.a库.库有两种: 静态库:.a和.framework 动态库:.tbd和.fra ...

  2. dp1--乘积最大

    dp1--乘积最大 一.心得 1.用excel填数组很方便 2. dp就是填表 找状态就是缩小规模 找状态转移方程就是 找状态的最后一次关系 二.题目 8782:乘积最大 查看 提交 统计 提问 总时 ...

  3. poj3694 边-双连通分量+lca

    题意:先给了一张无向图,然后依次加边,每次求桥的数量 题解:先用一次tarjan,我们可以标记桥的位置和记录桥的数量同时记录fa数组,然后更新边的时候我们可以用lca,因为在tarjan缩点之后得到了 ...

  4. 用系统工具sxstrace检查缺少的VC运行时组件

    在管理员运行的命令提示符中输入sxstrace获得如下帮助: C:\>sxstrace WinSxs Tracing Utility. Usage: SxsTrace [Options] Opt ...

  5. 简单CSS3动画

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 验证实现element-ui树形控件的自定义图标及右键菜单

    许久不用,element-ui已经更新至2.4.1版本.直接进入今天的正题,前提是node.js的环境还有vue及elment-ui都已经安装.由于element-ui的官方文档中介绍比较粗略,试了许 ...

  7. 16 Python 递归函数

    递归 1.什么是递归 recursion 递归 递归的定义——在一个函数里再调用这个函数本身 在一个函数里再调用这个函数本身,这种魔性的使用函数的方式就叫做递归. 递归的最大深度——997 一个函数在 ...

  8. 配合Jenkins自动化构建,bat脚本(一)

    C:\Windows\System32\inetsrv\appcmd.exe stop site ServiceIIS C:\Windows\System32\inetsrv\appcmd.exe s ...

  9. hdoj-2647-Reward(拓扑排序)

    题目链接: /* Name:hdoj-2647-Reward Copyright: Author: Date: 2018/4/11 15:59:18 Description: */ #include ...

  10. Linux运维工程师中级面试题

    1.解释top命令和vmstat命令 2.请写出iptables语句 3.mysql高可用方案有哪些?mysql备份方案有哪些?有什么缺点? 4.写出Apache 2.x的两种工作模式,以及各自的工作 ...