Android之Toast简单实现不循环提示
2014-06-09 11:48
483 查看
来自大牛:http://blog.csdn.net/way_ping_li/article/details/8840955
不知道各位程序猿们在项目中有没有遇到这个问题:点击一个view弹出一个Toast,我们用的方法是Toast.makeText(context, "提示", Toast.LENGTH_SHORT).show(); 但是,细心的人发现了,如果频繁的点击这个view,会发现尽管我们退出了这个应用,还是会一直弹出提示,这显然是有点点小尴尬和恼人的。下面就给大家提供两种方式解决这个问题。
1.封装了一个小小的Toast:
[java] view
plaincopy
/**
* 不循环提示的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。
[java] view
plaincopy
/**
* 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();
}
}
不知道各位程序猿们在项目中有没有遇到这个问题:点击一个view弹出一个Toast,我们用的方法是Toast.makeText(context, "提示", Toast.LENGTH_SHORT).show(); 但是,细心的人发现了,如果频繁的点击这个view,会发现尽管我们退出了这个应用,还是会一直弹出提示,这显然是有点点小尴尬和恼人的。下面就给大家提供两种方式解决这个问题。
1.封装了一个小小的Toast:
[java] view
plaincopy
/**
* 不循环提示的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。
[java] view
plaincopy
/**
* 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 (自定义扩展Toast)
- 15、android 用toast实现简单的进度显示
- AndroidUI之三分钟教你实现效果简单大气的Dialog提示框
- Android中Spinner下拉列表(简单layout.xml,ArrayAdapter和自定义Adapter实现)及下拉框提示prompt问题
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- js实现动态消息提示框(模仿android系统中toast的效果)
- 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)
- Android实现Toast提示框图文并存的方法
- Android中实现带声音提示的Toast (自定义扩展Toast)
- Android开发之Toast工具类(实现带图片的Toast消息提示)
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- 【Android代码片段之六】Toast工具类(实现带图片的Toast消息提示)
- 如何在iOS上实现Android风格的提示信息Toast
- Android中实现带声音提示的Toast
- Android基础教程(三)之-----简单的Button事件响应综合提示控件Toast的应用
- android简单实现 自动匹配提示