2012-07-15 70 views
1

當我搜索吐司在stackoverflow我發現很多事情都與我的問題有關。但他們都沒有解決我的問題。在我的Android應用程序中,我應該在海誓山盟之後展示許多祝酒詞。但是,當我嘗試這樣做時,接下來的祝酒會等待當前祝酒結束。即使我用取消敬酒,在我看來,它從來沒有works.Please我提供你一個確切的solution.Thank ..吐司取消不起作用

inflater = getLayoutInflater(); 
    backgForToast=(ViewGroup) findViewById(R.id.toast_layout_root); 
    layout = inflater.inflate(R.layout.toastbackground,backgForToast); 
    textForToast = (TextView) layout.findViewById(R.id.text); 

    textForToast.setText(toastMessage); 
    toast.setGravity(Gravity.TOP|Gravity.LEFT, toastX2-90,toastY2-90); 
    toast.setView(layout); 
    toast.show(); 

    Handler handler = new Handler(); 
    long delay = 500; 

    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      toast.cancel(); 
     } 
    }, delay); 

回答

1

你需要創建一個Toast對象與Toast.makeText才能cancel()它。

+0

我做到了在這樣的吐司=新吐司(本)onCreate方法; – Emilla 2012-07-15 16:20:03

+0

好的,所以你需要在調用'cancel()'之前調用'toast.makeText(this,toastMessage,Toast.LENGTH_LONG);'(或類似的東西)。 – 2012-07-15 16:21:56

+0

但我正在使用自定義烤麪包......我誇大了烤麪包的觀點 – Emilla 2012-07-15 16:42:26

1

這是一個基本的例子取消Toast

Toast mytoast; 
mytoast = Toast.makeText(getApplicationContext(), "Jorgesys was here!", Toast.LENGTH_LONG); 
mytoast.show(); 
.... 
.... 
.... 
if(CancelToast){ 
    mytoast.cancel(); //Cancelling the toast!. 
}