首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Toast在Toast的顶部-当顶部的Toast去掉时,底部的Toast仍然存在

Android Toast在Toast的顶部-当顶部的Toast去掉时,底部的Toast仍然存在
EN

Stack Overflow用户
提问于 2019-09-24 01:13:35
回答 1查看 117关注 0票数 0

我的应用程序通过调用

代码语言:javascript
复制
Toast.makeText(getBaseContext(), "Blah", Toast.LENGTH_SHORT).show();

(好的,我使用资源id而不是字符串)。在我用过的从4到7的每个Android版本上,一切都运行得很好,但对于Android 8.1.0,如果在第一个敬酒消失之前弹出第二个吐司,当第二个吐司消失时,第一个吐司就会恢复。我的猜测是,显示管理器会记住吐司下面的像素,并在吐司消失时恢复它们,但它不够聪明,无法处理一堆吐司,因此第二个管理器“记住”第一个的图像,并恢复它而不是底层窗口区域。

我可以使用一个"makeToast“包装器来解决这个问题,这个包装器每次都会将Toast对象隐藏在一个静态变量中,并在对新的对象执行.show()之前对旧的对象调用.cancel()。这是终极解决方案,还是有更好的方法?

代码语言:javascript
复制
private static Toast lastToast;
private static Context toastContext; // set in onCreate
public static void makeToast(int resId, int duration) {
    if (lastToast != null) lastToast.cancel();
    lastToast = Toast.makeText(toastContext, resId, duration);
    lastToast.show();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-24 19:51:42

这为我解决了这个问题--我用我自己的类包装了Toast,cancel()是在show()调用新对象之前调用show()的最后一个对象。看起来像是一个巨大的黑客攻击,但它确实完成了工作,只需要在我的应用程序的其余部分中更改import android.widget.Toast行。

代码语言:javascript
复制
package my.app.hacks;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;    

public class Toast extends android.widget.Toast {
    public static final int LENGTH_SHORT = android.widget.Toast.LENGTH_SHORT;
    public static final int LENGTH_LONG = android.widget.Toast.LENGTH_LONG;

    private static Toast lastShown;

    public Toast(Context context) {
        super(context);
    }

    public void show() {
        if (lastShown != null) lastShown.cancel();
        super.show();
        lastShown = this;
    }

    public static Toast makeText(Context context, CharSequence text, int duration) {
        //
        // this is almost copy-n-pasted from android.widget.Toast.makeText()
        //
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // we can't directly access com.android.internal.R identifiers so use the public interface
        int layout = Resources.getSystem().getIdentifier("transient_notification", "layout", "android");
        View v = inflate.inflate(layout, null);
        int id = Resources.getSystem().getIdentifier("message", "id", "android");
        TextView tv = (TextView)v.findViewById(id);
        tv.setText(text);

        // the original code accesses private members here - again, we have to use the public interface
        result.setView(v);
        result.setDuration(duration);

        return result;
    }

    public static Toast makeText(Context context, int resId, int duration)
            throws Resources.NotFoundException {
        return makeText(context, context.getResources().getText(resId), duration);
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58067220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档