2012-04-09 79 views
22

在我的应用程序中,我为自己的活动构建了一个日历小部件,当我将其滚动到上个月或下个月时,我会让它烤面包并显示它。例如,当我滚动到“2012/05”和“2012/06”并且滚动到“2012/07”而没有停顿时,敬酒需要时间来显示,当我滚动它足够快时,我必须等待“2012/05”,“2012/06”,“2012/07”的敬酒慢慢展示。当我想要显示其他吐司时,我可以取消先前的吐司吗?

好像Android有一个看不见的队列管理祝酒

我该怎么清理,只显示最后一个土司?我可以立即显示一个specificate吐司,而无需等待?

我搜索了“android.widget.Toast.java”并找到了一个“cancel()”方法,但不幸的是它不适用如下。

if (t != null) { 
    t.cancel(); 
} 
t = Toast.makeText(this.mContext, mHelper.getYear() + "年" 
       + (mHelper.getMonth() + 1) + "月", Toast.LENGTH_SHORT); 
t.show(); 
+1

我一直在试图弄明白但老实说,我不知道为什么它不工作。你的代码应该工作,如果cancel()做它所说的。也许你将不得不使用别的东西而不是吐司。 – Joe 2012-04-09 08:31:37

+0

@topxebec您的代码应该可以正常工作。它构成了我的'Boast'类参考的基础,在下面的答案中http://stackoverflow.com/a/16103514/383414 – 2013-04-19 11:27:03

回答

7

您需要在正确的对象上调用方法。

toastObject.cancel() 
5

这是代码。

final Toast toastobject = Toast.makeText(context, "This message will disappear when toast.close(); is called", Toast.LENGTH_SHORT); 

现在您可以使用toastobject对象。它的Reference

toastobject.cancel(); 

你可以在线程中使用它或当你想关闭烤面包。

+0

做吐司有一个叫close()的方法吗? – JonA 2012-04-09 07:29:47

+0

是的,我给了参考。 – Bhavin 2012-04-09 07:38:21

+0

谢谢,但Toast中没有可用的“close()”方法,只有一个“cancel()”,我试过的女巫(在上面的问题中)并且在我的程序中不起作用。 – topxebec 2012-04-09 07:38:34

1

吐司具有隐藏当前吐司消息

public void cancel() { 
    mTN.hide(); 
} 

尝试调用t.cancel(),当它是需要的方法。

+1

我试了一下,但似乎它不工作... – topxebec 2012-04-09 07:35:55

+1

@JonA - 是的,确切的。 @topxebec - 诀窍是保留原来的吐司 - 'mTN'的实例。这是吸引了很多人。你不能只是创建一个新的Toast并尝试取消那个 - 你需要取消原来的。 – 2013-07-03 13:24:04

20

,这里是我的答案从这里另一个类似的问题复制:

Boast类实现正是你需要的。最近的代码可以在GitHub在这里找到:


关键是要保持跟踪被显示的最后Toast,并以取消一个。

我所做的是创建一个Toast包装,它包含对最后Toast显示的静态引用。

当我需要显示一个新的,我首先取消静态引用,然后显示新的(并保存在静态)。

以下是我制作的Boast包装的完整代码 - 它足以模仿Toast方法以供我使用。默认情况下,Boast将取消前一个,所以你不会建立一个等待显示的Toasts队列。

如果您只是想知道如何在退出应用时取消通知,那么您会在该处找到很多帮助。


package mobi.glowworm.lib.ui.widget; 

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.content.res.Resources; 
import android.support.annotation.Nullable; 
import android.widget.Toast; 

import java.lang.ref.WeakReference; 

/** 
* {@link Toast} decorator allowing for easy cancellation of notifications. Use this class if you 
* want subsequent Toast notifications to overwrite current ones. </p> 
* <p/> 
* By default, a current {@link Boast} notification will be cancelled by a subsequent notification. 
* This default behaviour can be changed by calling certain methods like {@link #show(boolean)}. 
*/ 
public class Boast { 
    /** 
    * Keeps track of certain Boast notifications that may need to be cancelled. This functionality 
    * is only offered by some of the methods in this class. 
    * <p> 
    * Uses a {@link WeakReference} to avoid leaking the activity context used to show the original {@link Toast}. 
    */ 
    @Nullable 
    private volatile static WeakReference<Boast> weakBoast = null; 

    @Nullable 
    private static Boast getGlobalBoast() { 
     if (weakBoast == null) { 
      return null; 
     } 

     return weakBoast.get(); 
    } 

    private static void setGlobalBoast(@Nullable Boast globalBoast) { 
     Boast.weakBoast = new WeakReference<>(globalBoast); 
    } 


    // //////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /** 
    * Internal reference to the {@link Toast} object that will be displayed. 
    */ 
    private Toast internalToast; 

    // //////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /** 
    * Private constructor creates a new {@link Boast} from a given {@link Toast}. 
    * 
    * @throws NullPointerException if the parameter is <code>null</code>. 
    */ 
    private Boast(Toast toast) { 
     // null check 
     if (toast == null) { 
      throw new NullPointerException("Boast.Boast(Toast) requires a non-null parameter."); 
     } 

     internalToast = toast; 
    } 

    // //////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /** 
    * Make a standard {@link Boast} that just contains a text view. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *     {@link android.app.Activity} object. 
    * @param text  The text to show. Can be formatted text. 
    * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or 
    *     {@link Toast#LENGTH_LONG} 
    */ 
    @SuppressLint("ShowToast") 
    public static Boast makeText(Context context, CharSequence text, int duration) { 
     return new Boast(Toast.makeText(context, text, duration)); 
    } 

    /** 
    * Make a standard {@link Boast} that just contains a text view with the text from a resource. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *     {@link android.app.Activity} object. 
    * @param resId The resource id of the string resource to use. Can be formatted text. 
    * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or 
    *     {@link Toast#LENGTH_LONG} 
    * @throws Resources.NotFoundException if the resource can't be found. 
    */ 
    @SuppressLint("ShowToast") 
    public static Boast makeText(Context context, int resId, int duration) 
      throws Resources.NotFoundException { 
     return new Boast(Toast.makeText(context, resId, duration)); 
    } 

    /** 
    * Make a standard {@link Boast} that just contains a text view. Duration defaults to 
    * {@link Toast#LENGTH_SHORT}. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *    {@link android.app.Activity} object. 
    * @param text The text to show. Can be formatted text. 
    */ 
    @SuppressLint("ShowToast") 
    public static Boast makeText(Context context, CharSequence text) { 
     return new Boast(Toast.makeText(context, text, Toast.LENGTH_SHORT)); 
    } 

    /** 
    * Make a standard {@link Boast} that just contains a text view with the text from a resource. 
    * Duration defaults to {@link Toast#LENGTH_SHORT}. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *    {@link android.app.Activity} object. 
    * @param resId The resource id of the string resource to use. Can be formatted text. 
    * @throws Resources.NotFoundException if the resource can't be found. 
    */ 
    @SuppressLint("ShowToast") 
    public static Boast makeText(Context context, int resId) throws Resources.NotFoundException { 
     return new Boast(Toast.makeText(context, resId, Toast.LENGTH_SHORT)); 
    } 

    // //////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /** 
    * Show a standard {@link Boast} that just contains a text view. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *     {@link android.app.Activity} object. 
    * @param text  The text to show. Can be formatted text. 
    * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or 
    *     {@link Toast#LENGTH_LONG} 
    */ 
    public static void showText(Context context, CharSequence text, int duration) { 
     Boast.makeText(context, text, duration).show(); 
    } 

    /** 
    * Show a standard {@link Boast} that just contains a text view with the text from a resource. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *     {@link android.app.Activity} object. 
    * @param resId The resource id of the string resource to use. Can be formatted text. 
    * @param duration How long to display the message. Either {@link Toast#LENGTH_SHORT} or 
    *     {@link Toast#LENGTH_LONG} 
    * @throws Resources.NotFoundException if the resource can't be found. 
    */ 
    public static void showText(Context context, int resId, int duration) 
      throws Resources.NotFoundException { 
     Boast.makeText(context, resId, duration).show(); 
    } 

    /** 
    * Show a standard {@link Boast} that just contains a text view. Duration defaults to 
    * {@link Toast#LENGTH_SHORT}. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *    {@link android.app.Activity} object. 
    * @param text The text to show. Can be formatted text. 
    */ 
    public static void showText(Context context, CharSequence text) { 
     Boast.makeText(context, text, Toast.LENGTH_SHORT).show(); 
    } 

    /** 
    * Show a standard {@link Boast} that just contains a text view with the text from a resource. 
    * Duration defaults to {@link Toast#LENGTH_SHORT}. 
    * 
    * @param context The context to use. Usually your {@link android.app.Application} or 
    *    {@link android.app.Activity} object. 
    * @param resId The resource id of the string resource to use. Can be formatted text. 
    * @throws Resources.NotFoundException if the resource can't be found. 
    */ 
    public static void showText(Context context, int resId) throws Resources.NotFoundException { 
     Boast.makeText(context, resId, Toast.LENGTH_SHORT).show(); 
    } 

    // //////////////////////////////////////////////////////////////////////////////////////////////////////// 

    /** 
    * Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally 
    * have to call this. Normally view will disappear on its own after the appropriate duration. 
    */ 
    public void cancel() { 
     internalToast.cancel(); 
    } 

    /** 
    * Show the view for the specified duration. By default, this method cancels any current 
    * notification to immediately display the new one. For conventional {@link Toast#show()} 
    * queueing behaviour, use method {@link #show(boolean)}. 
    * 
    * @see #show(boolean) 
    */ 
    public void show() { 
     show(true); 
    } 

    /** 
    * Show the view for the specified duration. This method can be used to cancel the current 
    * notification, or to queue up notifications. 
    * 
    * @param cancelCurrent <code>true</code> to cancel any current notification and replace it with this new 
    *      one 
    * @see #show() 
    */ 
    public void show(boolean cancelCurrent) { 
     // cancel current 
     if (cancelCurrent) { 
      final Boast cachedGlobalBoast = getGlobalBoast(); 
      if ((cachedGlobalBoast != null)) { 
       cachedGlobalBoast.cancel(); 
      } 
     } 

     // save an instance of this current notification 
     setGlobalBoast(this); 

     internalToast.show(); 
    } 

} 
+1

谢谢,漂亮的清洁解决方案:-) – 2013-06-12 20:47:18

+1

似乎并没有在姜饼顺便说一句,那里的老吐司保持可见和新的吐司后它 – 2013-06-12 20:56:52

+0

@thenail Thx反馈。如果你愿意,哪种设备?正在使用我们的三星2.3设备,但不记得是什么型号。并在HTC Desire 2.2,Galaxy Nexus 4.1上工作。反馈赞赏。 – 2013-06-12 21:57:12

2

您可以重复举杯,这将使其立即显示。

myToast.setText(toastMsg); 
myToast.show(); 
1

您可以创建静态方法,并用它来显示举杯:

public static Toast toast = null; 
public static showToast(Context context,String msg){ 
if(toast!=null)    //this will cancel the toast on the screen if one exists 
    toast.cancel(); 
toast = Toast.makeText(context,msg); 
toast.show(); 
} 
9

你只需要声明一个“吐司” VAR是这样的:在

Toast toastMessage; 

然后你功能,请这样做:

if (toastMessage!= null) { 
    toastMessage.cancel(); 
} 
toastMessage= Toast.makeText(context, "The message you want to display", duration); 
toastMessage.show(); 
0
public static Toast sToast=null; 

// create Toast object; 

public void showToast(String msg) 

    { 

    //here is checking whether toast object is null or not;if not null gonna cancel the toast that is showing in phone window and make it null; 

    if(sToast!=null) 
    { 

     sToast.cancel; 

    sToast=null; 
    } 

    //if toast object is null,gonna create new instance and make it shown on phone window. 

    if(sToast==null) 
    { 

     sToast=Toast.makeText(currentActivity.this,msg,Duration); 

     sToast.setGravity(); 

     sToast.show(); 

    } 

} 
+0

无需检查sToast是否为空。它永远是。 – 2016-03-15 19:10:04

0

简单。一旦你想创建另一个烤面包,只需在烤面包上调用方法.cancel()即可。

开始通过在你的班上名列前茅这样

private Toast mToast; 

定义一个变量吐司以后,当你想创建一个新的吐司(并有旧的消失), 做到这一点。

if(mToast != null) { 
    mToast.cancel(); //if a toast exists it deletes it, allowing you to create a new one 
} 


mToast = Toast.makeText(this, "This will show up now!", Toast.LENGTH_LONG); 
mToast.show(); //creates the new toast. 
0

您可以使用一种拍摄技术。奥凯,让我们开始定义:

private Toast mToast; 
private showOnce=false; 

后来,当你想告诉敬酒一次:

if(showOnce==false){ 
    mToast=Toast.makeText(this, "Once!", Toast.LENGTH_LONG); 
    mToast.show(); 
    showOnce=true; 
}