2012-10-16 17 views
19

मैं एक "टोस्ट" का उपयोग कर रहा हूं जिसका उपयोग मैं "myToast.cancel(); नया टोस्ट जारी करने से पहले करता हूं। एंड्रॉइड v2.3 और पुराने के लिए, यह बहुत अच्छा काम करता है। नए टोस्ट को भेजने की जरूरत है, पुराना एक, यदि अभी भी स्क्रीन पर है, तो नए टोस्ट के साथ प्रतिस्थापित करने के लिए रद्द कर दिया गया है (और तुरंत गायब हो जाता है)। यदि उपयोगकर्ता कई बार एक कुंजी दबाता है तो यह टोस्टों का एक गुच्छा ढेर से बचाता है चेतावनी (और अन्य स्थितियां)। मेरा वास्तविक मामला एक टोस्ट तब होता है जब गलत कुंजी दबाई जाती है, और दूसरा दिखाई देता है यदि साफ़ कुंजी दबाया नहीं जाता है।एकाधिक टोस्ट ओवरलैप को कैसे रोकें

एंड्रॉइड 4.0 और 4.1 के लिए, myToast.cancel() जारी करना अगले टोस्ट से पहले वर्तमान और अगले टोस्ट दोनों को मारता है। वर्तमान रद्द() एपीआई इंगित करता है कि यह वर्तमान और अगले टोस्ट को रद्द करता है (जो बेवकूफ लगता है)। एक टोस्ट को क्यों रद्द करना चाहते हैं?

एंड्रॉइड संस्करणों (और जिस तरह से यह v2.3 और पुराने में काम करता है) में निरंतर काम रद्द करने पर कोई विचार?

मैं कुछ सुरुचिपूर्ण दोहरी टोस्ट सिस्टम का प्रयास करूँगा जिसमें टॉस्ट का उपयोग किया जा रहा है, लेकिन ऐसा लगता है कि पुराने एंड्रॉइड संस्करणों में पूरी तरह से और तार्किक रूप से काम करने के लिए 4.x में इस बुरे व्यवहार के आसपास ऐसा दर्द होता है।


ठीक है, मैंने इसे हल किया है, लेकिन यह लगभग उतना ही साफ नहीं है जितना मुझे पसंद आएगा। मैंने दोहरी टोस्ट दृष्टिकोण लागू किया, जहां यह दो टोस्टों के बीच बदलता है। OnCreate में

Toast toast0; 
Toast toast1; 
private static boolean lastToast0 = true; 

: अंत में

toast0 = new Toast(getApplicationContext()); 
toast0.cancel(); 
toast1 = new Toast(getApplicationContext()); 
toast1.cancel(); 

और, जब मैं टोस्ट प्रदर्शित करने और एक ही समय में पहले टोस्ट रद्द करने की आवश्यकता पहले हम टोस्ट गतिविधि के लिए पहले OnCreate को परिभाषित

if (lastToast0) { 
    toast0.cancel(); 
    toast1.setDuration(Toast.LENGTH_LONG); 
    toast1.setText("new message"); 
    toast1.show(); 
    lastToast0 = false; 
} else { 
    toast1.cancel(); 
    toast0.setDuration(Toast.LENGTH_LONG); 
    toast0.setText("new message"); 
    toast0.show(); 
    lastToast0 = true; 
} 

तुम सिर्फ (यह समय समाप्त होने से पहले) एक मौजूदा टोस्ट को रद्द करने की जरूरत है का उपयोग करें::

मैं करने के लिए कुछ इसी तरह का उपयोग 0
toast0.cancel(); 
toast1.cancel(); 

नेक्सस 7 (4.1), एम्यूलेटर 4.0, और एंड्रॉइड 2.2, 2.3 के साथ कई उपकरणों पर परीक्षण किया गया।

+0

आप पुराने टोस्ट को कैसे रद्द कर रहे हैं? नया बनाने से पहले या बाद में? – TheZ

+0

क्या आप अपना कोड पोस्ट कर सकते हैं जहां आप टोस्ट कर रहे हैं? – Ralgha

+0

मैं इसे नए से पहले रद्द कर रहा था - यही कारण है कि यह इतना अजीब बात है कि एंड्रॉइड 4.x नया छुपाता है (लेकिन टोस्ट के लिए एपीआई बताएगा कि समझा जाएगा)। मैंने एक समाधान में संपादित किया जो मैंने बनाया है। बस बहुत अच्छी लग रही नहीं है। – Frank

उत्तर

47

रद्द करने के बजाय। पाठ और कॉल शो() को रीसेट करने का प्रयास करें। यह अपने आप

myToast.setText("wrong key") 
myToast.show(); 

द्वारा पिछले टोस्ट रद्द कर देना चाहिए मैं लगता है कि वे टिके रहते नहीं होते तो आप एक ही myToast का उपयोग कर के बजाय एक हर बनाते रहें हैं।

+0

बिल्कुल सही!साझा करने के लिए धन्यवाद :) – Jona

2

cancel() कुछ भी नहीं करता जो मुझे डरता है।

मैं crouton https://github.com/keyboardsurfer/Crouton का उपयोग कर

+0

पुस्तकालय अच्छा है, लेकिन यह इस समस्या को हल नहीं करता है। – Ahmad

+0

एक नए टोस्ट से पहले toast.cancel() का उपयोग करके आंशिक रूप से काम करता है - वर्तमान टोस्ट गायब होने पर गायब हो जाता है। एंड्रॉइड 4.x (शायद 3.x) में क्या बदल गया है, यह अगले टोस्ट को भी छुपाता है जिसे मैं पहले बदलना चाहता हूं! – Frank

2

nandeesh के समाधान के लिए काम नहीं किया था सुझाव है? उनका समाधान दो अलग-अलग टोस्टों का उपयोग करने से साफ होगा।

उदाहरण के लिए, पहले (उसकी/उसके जवाब पर विस्तार) onCreate को हम टोस्ट घोषित चाहते हैं:

private Toast myToast; 

और onCreate में हम इसे makeToast का उपयोग कर प्रारंभ करना होगा (अन्यथा हम प्राप्त होता एक त्रुटि):

myToast = Toast.makeText(getApplicationContext(), null, Toast.LENGTH_SHORT); 

और जब हमें कोई टोस्ट प्रदर्शित करना चाहते हैं हम बस फोन चाहते हैं:

myToast.setText("some text"); 
myToast.show(); 

और यह पिछले टोस्ट को ठीक से बदल देगा।

+0

मुझे यकीन नहीं है कि आप कहां पढ़ते हैं कि नंदेश दो टोस्टों का उपयोग करने का सुझाव देते हैं, उन्होंने स्पष्ट रूप से कहा, 'यदि आप हर बार एक बनाने के बजाए एक ही मायोस्ट का उपयोग करते रहते हैं तो मुझे लगता है कि वे ढेर नहीं होंगे।' उस बिंदु पर, मुझे आपके बीच कोई अंतर नहीं दिखता जवाब और उसका। साथ ही, मैं सुझाव दूंगा कि आप अपने उत्तरों को अन्य लोगों के उत्तर की तुलना किए बिना लिखते हैं। यह मेरे लिए कठोर लगता है, खासकर जब आप उनके जवाब के बारे में बातें कह रहे हैं जो गलत हैं। – ForceMagic

+0

मैंने कहां कहा कि नंदेश दो टोस्टों का उपयोग कर रहा था? इसके अतिरिक्त, मेरा जवाब बस उस पर विस्तार करना था और एक लंबा उदाहरण देना था। – TSL

+0

क्षमा करें, यह अभी भी मुझे भ्रमित करता है 'क्या नंदेशेश का समाधान आपके लिए काम नहीं करता था? उनका समाधान दो अलग-अलग टोस्टों का उपयोग करने से साफ होगा। 'मुझे यकीन नहीं है कि आप उसका जवाब क्यों इंगित कर रहे हैं और फिर एक ही पोस्ट करते हैं, मेरा मतलब है कि वह हिस्सा जो उसके उत्तर से गायब था, उसे "काम नहीं कर रहा" IMO। – ForceMagic

2

यहाँ मेरा उत्तर एक और इसी तरह प्रश्न यहाँ से नकल है:

Boast वर्ग पूरा करता आप वास्तव में क्या जरूरत है।


चाल पिछले Toast कि दिखाया गया था का ट्रैक रखने के लिए, और रद्द करने के लिए है कि एक है।

मैंने जो किया है वह Toast रैपर बनाने के लिए है, जिसमें अंतिम टोस्ट प्रदर्शित करने के लिए एक स्थिर संदर्भ शामिल है।

जब मुझे एक नया दिखाना है, तो मैं पहले नया दिखाने से पहले स्थिर संदर्भ को रद्द कर देता हूं (और इसे स्थिर में सहेजने से पहले)।

यहां दिए गए Boast रैपर का पूरा कोड है - यह मेरे उपयोग के लिए टोस्ट विधियों की पर्याप्त नकल करता है। डिफ़ॉल्ट रूप से Boast पिछले एक को रद्द कर देगा, इसलिए आप प्रदर्शित होने के लिए प्रतीक्षा करने वाले टोस्ट्स की कतार नहीं बनाते हैं।

यदि आप सिर्फ अपने ऐप से बाहर निकलने पर अधिसूचनाओं को रद्द करना चाहते हैं, तो आपको वहां बहुत सारी मदद मिलेगी।


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(); 
    } 

} 
+0

मुझे आपके समाधान से प्यार है, और यह 4.x पर ठीक काम कर रहा है, लेकिन क्या आपको कोई विचार है कि यह 2.x पर क्यों काम नहीं कर रहा है? (मैंने 3.x की कोशिश नहीं की है) –

+0

क्षमा करें कोई विचार नहीं। यह उत्पादन कोड है और मैंने सोचा कि मैं इसे पीछे की संगतता के लिए परीक्षण करूँगा। भविष्य में कुछ भी पता लगाने पर दोबारा मरम्मत होगी। –

1

यह मेरा समाधान दोनों 4. * और 2.3 Android संस्करण

static Toast toast; 
..... 

if (toast != null) 
    toast.cancel(); 

boolean condition = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB; 
if ((toast == null && condition) || !condition) 
    toast = Toast.makeText(context, text, Toast.LENGTH_LONG); 
if ((toast != null && condition)) 
    toast.setText(text); 
toast.show(); 
0

नया कार्य बना सकते हैं और इस फोन के लिए एकदम सही काम करता है।

Toast toastobject=null; 

अब टोस्ट प्रदर्शित करने के लिए नीचे दिए गए कोड का उपयोग करें:

ImageButton ABtn = (ImageButton) findViewById(R.id.Btn); 
ABtn.setOnClickListener(new View.OnClickListener() { 
public void onClick(View v) 
{  
SETToast("mytext"); 
} 
}); 

    private Toast toast = null; 

public void SETToast(String text) 
{ 
    if(toast==null) 
    { 
    toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT); 
    toast.show(); 
    final Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      toast=null; 
     } 
    }, 2000); 
    } 
    else 
    { 
     toast.setText(text); 
    } 
} 
1

एक टोस्ट वस्तु बनाएँ। यह मेरे लिए काम करेगा

int index = clickCounter-1; 


    if(toastobject!= null) 
      { 
       toastobject.cancel(); 
      } 
      toastobject = Toast.makeText(this,"Toast Text" , Toast.LENGTH_SHORT); 
      listItems.remove(index); 
      toastobject.show();