簡體   English   中英

僅在與現有吐司不同的情況下顯示吐司

[英]Show toast only if it's different from the existing toast

因此,我的目標是僅在沒有顯示Toast消息或顯示的消息與我要發送的消息不同的情況下,向用戶顯示Toast消息。 如果該消息與向用戶顯示的消息相同,則我不希望該消息通過(因為這毫無意義)。

為了實現這個目標,我找到了這篇文章,內容關於如何僅在未顯示吐司的情況下顯示吐司。

我已修改代碼以同時滿足這兩個要求。

private Toast toast;

public void showAToast (String st, boolean isLong){
    try{
        toast.getView().isShown();
        String text = ((TextView)((LinearLayout)toast.getView()).getChildAt(0)).getText().toString();
        if(!text.equalsIgnoreCase(st)){
            //New message, show it after
            if(isLong){
                toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
            } else {
                toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
            }
            toast.show();
        }
    } catch (Exception e) {
        //New message
        if(isLong){
            toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG);
        } else {
            toast = Toast.makeText(getApplicationContext(), st, Toast.LENGTH_SHORT);
        }
        toast.show();
    }
}

我的問題是,如果最后一個吐司消息與想要通過的消息相同,則任何消息都不會通過。

不知道為什么會這樣,但是我在方法中放入了一些調試消息以找出問題所在。

消息說,如果在應用程序的生存期內發送了任何Toast消息,則toast.getView()。isShown()不會引發異常(假定表示未顯示Toast)。

所以我的問題是,我該如何解決? 當然,必須有一種方法來實現此所需功能。

我之前在stackoverflow中看到過這一點,但是它並不像我想要的那么干凈。 我們實施了雙重吐司方法,在兩種吐司之間交替進行。 首先,我們在OnCreate之前為活動定義敬酒:

Toast toast0;
    Toast toast1;
    private static boolean lastToast0 = true;
    In the OnCreate:

    toast0 = new Toast(getApplicationContext());
    toast0.cancel();
    toast1 = new Toast(getApplicationContext());
    toast1.cancel();
    //And finally, when I need to display the toast and cancel the prior toast at the same time I use something similar to:

            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;
            }
   // If you need to just cancel an existing toast (before it times out) use:

                toast0.cancel();
                toast1.cancel();

報價單

您可以使用相同的Toast實例顯示消息。 如果消息相同,則吐司將不會顯示兩次,否則,文本將簡單地更改為最新的消息。

Toast mToast;

public void showToast(CharSequence message, int during){
    if (mToast == null) {
        mToast = Toast.makeText(getApplicationContext(), message, during);
    } else {
        mToast.setText(message);
    }
    mToast.show();
}

-↓---↓----↓---更新-↓----↓---↓-↓

對不起,我想念你的意思。

我閱讀了Toast的源代碼,因為該視圖已添加到WindownManager中,所以我們無法獲得視圖狀態。到目前為止,我還找不到一種方法來指出是否顯示了Toast。

但是您可以實現自己的Toast使用服務,它類似於應用程序上方顯示的Toast。 可能會更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM