Android之Toast简单实现不循环提示
来源:互联网 发布:淘宝旺旺有卖家版吗 编辑:程序博客网 时间:2024/06/06 09:54
原文地址:http://blog.csdn.net/way_ping_li/article/details/8840955
不知道各位程序猿们在项目中有没有遇到这个问题:点击一个view弹出一个Toast,我们用的方法是Toast.makeText(context, "提示", Toast.LENGTH_SHORT).show(); 但是,细心的人发现了,如果频繁的点击这个view,会发现尽管我们退出了这个应用,还是会一直弹出提示,这显然是有点点小尴尬和恼人的。下面就给大家提供两种方式解决这个问题。
1.封装了一个小小的Toast:
- /**
- * 不循环提示的Toast
- * @author way
- *
- */
- public class MyToast {
- Context mContext;
- Toast mToast;
- public MyToast(Context context) {
- mContext = context;
- mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
- mToast.setGravity(17, 0, -30);//居中显示
- }
- public void show(int resId, int duration) {
- show(mContext.getText(resId), duration);
- }
- public void show(CharSequence s, int duration) {
- mToast.setDuration(duration);
- mToast.setText(s);
- mToast.show();
- }
- public void cancel() {
- mToast.cancel();
- }
- }
2.两个直接调用的函数函数:可以放在在Activity中,在需要时直接调用showToast(String or int); 在Activity的onPause()中调用hideToast(),使得应用退出时,取消掉恼人的Toast。
- /**
- * Show a toast on the screen with the given message. If a toast is already
- * being displayed, the message is replaced and timer is restarted.
- *
- * @param message
- * Text to display in the toast.
- */
- private Toast toast;
- private void showToast(CharSequence message) {
- if (null == toast) {
- toast = Toast.makeText(this, message,
- Toast.LENGTH_LONG);
- toast.setGravity(Gravity.CENTER, 0, 0);
- } else {
- toast.setText(message);
- }
- toast.show();
- }
- /** Hide the toast, if any. */
- private void hideToast() {
- if (null != toast) {
- toast.cancel();
- }
- }
- Android之Toast简单实现不循环提示
- Android之Toast简单实现不循环提示
- Android之Toast简单实现不循环提示
- Android之Toast简单实现不循环提示
- Android Toast实现提示效果
- Android消息提示之Toast
- android Toast不出现提示问题
- 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)
- Android开发之Toast工具类(实现带图片的Toast消息提示)
- 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)
- iOS 实现Android Toast消息提示 效果
- Android 开发之 Toast (弹出提示框)
- Android用户界面之提示信息(Toast)
- Android用户界面之提示信息(Toast)
- Android开发之Toast提示的优化
- android 项目优化之toast提示优化
- Android之自定义Toast提示框样式
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- 编写基于winpcap程序的环境搭建
- libginle编译 win2007
- 如何利用多核CPU来加速你的Linux命令
- linux 压缩 解压缩命令使用
- iphone调用系统电话、浏览器、地图、邮件等
- Android之Toast简单实现不循环提示
- 随记
- EditView
- IOS 键盘
- IOS 图片下载缓存模块 代码
- linux截图命令
- Matlab批量处理文件
- oracle笔记
- win8应用打包部署