一、 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. Eclipse里面新建servlet 是否需要配置web.xml

    在新建的时候可选时候映射,如果选择了映射,那么就会在servle开头的地方有一行@servlet(""),这就完成了映射.注释掉这行就需要在web.xml中设置了

  2. spring:使用会话和请求作用域

    在Web应用中,如果能够实例化在会话和请求范围内共享的bean,那将是非常有价值的事情.例如,在典型的电子商务应用中,可能会有一个bean代表用户的购物车.如果购物车是单例的话,那么将会导致所有的用户 ...

  3. Mssql 跨域查询

    有数据库test1和数据库test2.其中test1中有表 table1.table2:test2 中有表 table1.三个表的字段都为为:id.xingming.shijian.shuliang. ...

  4. 51nod 1428 贪心

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活动安排问题 基准时间限制:1 秒 空间限制:13107 ...

  5. python--*args和**kwargs可变参数

    先来看个例子: #! /usr/bin/env python #coding=utf-8 def foo(*args, **kwargs): print('args=',args) print('kw ...

  6. nyoj-67-三角形面积(S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2))

    题目链接 /* Name:nyoj-67-三角形面积 Copyright: Author: Date: 2018/4/26 16:44:47 Description: 三角形的三个顶点坐标求其面积的公 ...

  7. 自动部署基于Maven的war文件到远程Tomcat

    转载自:https://www.cnblogs.com/jtlgb/p/7018705.html Tomcat 7发布URL = http://localhost:8080/manager/text命 ...

  8. CodeForces - 156D:Clues(矩阵树定理&并查集)

    题意:给定N点,M边,求添加最少的边使之变为连通图的方案数. 思路:注意题目给出的M边可能带环,即最后生成的不一定是一棵树.但是影响不大.根据矩阵树定理,我们知道生成树的数量=N^(N-2),即点数^ ...

  9. 1625 codevs数字金字塔

    1625 数字金字塔 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 考虑在下面被显示的数字金字塔. 写 ...

  10. mysql开启和使用事件、与服务器重启mysql错误

    一.mysql开启事件 首先用SHOW VARIABLES LIKE 'event_scheduler',查看计划事件有没有开启,他的返回值是off和on. 如果没有开启,可以在my.cnf配置文件中 ...