奥利奥(8.1)上的吐司重叠问题 [英] Toasts overlap issue on Oreo (8.1)

查看:122
本文介绍了奥利奥(8.1)上的吐司重叠问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对吐司有疑问。对于API 26及以下版本,祝酒词可以正确显示(下一个祝酒词正在等待上一个的消失),但是在Android 8.1(API 27)上,祝酒词相互覆盖。我有这样设置的通知渠道:

I have a problem with toasts. For API 26 and below toasts are displayed properly (next toast is waiting for previous to disappear) but on Android 8.1 (API 27) they are covering each other. I have notification channel set up like this:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, 
                        NOTIFICATION_CHANNEL_NAME, 
                        NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(notificationChannel);
    builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}

此功能为我修复了8.0,但在8.1上它们仍然重叠

This fixes toasts on 8.0 for me, but on 8.1 they are still overlapping

有什么方法可以解决此问题,而不是记住上次使用的烤面包并手动将其取消?

Is there any way to fix this instead of remembering last used toast and canceling it manually?

编辑:

此线程不会t工作

/**
 * <strong>public void showAToast (String st)</strong></br>
 * this little method displays a toast on the screen.</br>
 * it checks if a toast is currently visible</br>
 * if so </br>
 * ... it "sets" the new text</br>
 * else</br>
 * ... it "makes" the new text</br>
 * and "shows" either or  
 * @param st the string to be toasted
 */

public void showAToast (String st){ //"Toast toast" is declared in the class
    try{ toast.getView().isShown();     // true if visible
        toast.setText(st);
    } catch (Exception e) {         // invisible if exception
        toast = Toast.makeText(theContext, st, toastDuration);
        }
    toast.show();  //finally display it
}

吐司面包仍然很浓

编辑2:
我已经在Android Issue Tracker上为该错误创建了故事:
链接

推荐答案

private static final int TIME_DELAY = 4000;
private static long lastToastShowTime = 0;

showToast(final String msg, final Context ctx){
    final long pastTime = System.currentTimeMillis() - lastToastShowTime;
    if(pastTime > TIME_DELAY ){

        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
        lastToastShowTime = System.currentTimeMillis();

     }else{
        final long delay = TIME_DELAY - pastTime;
        lastToastShowTime = System.currentTimeMillis() + delay;
        postDelayed(new Runnable(

            @Override
            public void run() {
               try{
                  Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
               catch(Exception e){
                  Log.e("TOAST_NOT_SHOWED", "Toast not showed: " + msg, e);
               }

            }

        ), delay);

    }
}

这篇关于奥利奥(8.1)上的吐司重叠问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆