一、 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. RtlWerpReportException failed with status code :-1073741823

    在release下程序运行总是崩溃:debugView输出了这个崩溃信息, 1. 一开始是release看崩溃,各种二分法找崩溃点,太玄没找到: 2. 终于想到可以调试,我草,调试一下瞬间发现某个cl ...

  2. Jquery Ajax模版

    $.ajax({ type: "GET", url: "test.json", data: {username:'tt', content:'tt'}, dat ...

  3. QT5 QtreeWidget 实现点击item事件以及右键菜单删除item 和 重命名item

    1.new 一个QTreeWidget 对象,并设置头标签,和根节点(个人程序需要) QTreeWidget* treeWidget = ui.treeWidget;//我已经在ui设计师中拖了一个Q ...

  4. hzau 1206 MathematicalGame

    1206: MathematicalGame Time Limit: 2 Sec  Memory Limit: 1280 MBSubmit: 124  Solved: 15[Submit][Statu ...

  5. js中判断数据类型

    一般来说,可以使用typeof来判断数据类型,但是数组,对象和null的结果都是object,那么如何区分这三类呢?可以使用如下方法: var arr = []; var obj = {} var e ...

  6. 在winform中给GridView加checkbox在按钮点击的时候获取选中的checkbox

    dataGridView绑定后 bool[] mark = new bool[this.dataGridView1.Rows.Count]; ; i < mark.Length; i++) { ...

  7. DataGridView绑定数据源的几种方式

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

  8. 7.MySQL优化---存储过程和存储函数

    转自互联网. 当一个大型系统在建立时,会发现,很多的SQL操作是有重叠的,个别计算是相同的,比如:业务系统中,计算一张工单的计算方式.当遇到这些情况时,我们运用存储过程就是一个非常棒的优化啦.那么,什 ...

  9. [转]NYOJ-511-移动小球

    大学生程序代写 http://acm.nyist.net/JudgeOnline/problem.php?pid=511 这道题很容易想到要构建一个循环链表来确定每个球的相对位置,就是操作比较繁琐,考 ...

  10. 20181229模拟 T1 palindrome

    20181229模拟 T1 palindrome 题意 : \(S\)是字符串\(s\)的子串可重集,求\(\sum\limits_{x\in S}\sum\limits_{y\in S}(|x|+| ...