A

Android Toast快速切换内容

source 默认分类JAVAAndroid 2023-10-12

在安卓开发中,在使用Toast的过程中发现了一个问题,当需要快速切换Toast内容时,例如音乐播放器频繁快速切换播放模式弹出的Toast提示,出现切换的内容比较缓慢甚至后面直接不弹出Toast情况,这种体验效果不是很好,应该快速切换内容才对。下面方式对这个问题做了处理,能够连续快速切换Toast内容。

private static Toast toast = null;
public static void showToast(Context ctx, String text, int duration) {
    if (toast != null) {
        toast.cancel();
        toast = null;
    }
    toast = Toast.makeText(ctx, text, duration);
    toast.setText(text);
    toast.show();
}

使用方法很简单,和Toast.makeText方法一致

showToast(ctx,"我是Toast",Toast.LENGTH_SHORT);

评论(0)

发布评论