android - Oreo (8.1) 上的 toast 重叠问题

标签 android android-8.1-oreo

我对 toast 有疑问。对于 API 26 及以下的 toasts 可以正确显示(下一个 toast 正在等待上一个 toast 消失)但在 Android 8.1 (API 27) 上它们相互覆盖。我有这样设置的通知 channel :

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 上的 toasts,但在 8.1 上它们仍然重叠 like on this picture

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

编辑:

解决方法来自 this thread不工作

/**
 * <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
}

toast 还在重叠

编辑 2: 我在 Android Issue Tracker 上为这个错误创建了故事: link

最佳答案

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

    }
}

关于android - Oreo (8.1) 上的 toast 重叠问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50062913/

相关文章:

android - MPAndroid 图表动画开始

java - Android:确定java文件中的点击按钮

android - 不同 fragment 的多个工具栏布局

android - 数据未在 Android Pie 9.0 上显示

android - 找不到 Android 8.0-8.1 (Oreo) API 26-27 ARM 系统镜像

Android模拟器顶部无法点击

java - Android 服务后台 Android Oreo

android - 设计 Android 应用程序 - 最佳实践?

java - 由 : java. lang.IllegalStateException 引起:替换 Activity 中的任何 fragment 时,无法在 onSaveInstanceState 之后执行此操作

android - 我需要删除 Android 通知徽章