【问题标题】:Toast that only shows on debug variants仅在调试变体上显示的 Toast
【发布时间】:2015-08-08 02:29:26
【问题描述】:

我想创建一个简单的帮助类,仅显示调试变体的 toast 消息。

这样使用:

TOAST.makeText(context, "Debug Toast message", Toast.LENGTH_SHORT).show();

TOAST.java

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.Toast;

import com.mypp.BuildConfig;

/**
 * Toast that only shows for debug build variants.
 */
@SuppressLint("ShowToast")
public class TOAST extends Toast {
    public TOAST(Context context) {
        super(context);
    }

    @NonNull
    public static TOAST makeText(@NonNull Context context, CharSequence text, int duration) {
        return (TOAST) Toast.makeText(context, text, duration);
    }

    @NonNull
    public static TOAST makeText(@NonNull Context context, int resId, int duration) {
        return (TOAST) Toast.makeText(context, resId, duration);
    }

    @Override public void show() {
        if (BuildConfig.DEBUG) {
            super.show();
        }
    }
}

虽然演员在我的实施中失败了:

Caused by: java.lang.ClassCastException: android.widget.Toast cannot be cast to com.mypp.helpers.TOAST
            at com.mypp.helpers.TOAST.makeText(TOAST.java:23)

【问题讨论】:

    标签: android toast


    【解决方案1】:

    您不能将Toast(基类)的实例类型转换为派生类TOAST,但也可以采用其他方式。

    我建议您将实现更改为如下所示:

    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.support.annotation.NonNull;
    import android.widget.Toast;
    
    import com.mypp.BuildConfig;
    
    /**
     * Toast that only shows for debug build variants.
     */
    @SuppressLint("ShowToast")
    public class TOAST {
    
        private Toast toast;
    
        public TOAST(Toast toast) {
            this.toast = toast;
        }
    
        @NonNull
        public static TOAST makeText(@NonNull Context context, CharSequence text, int duration) {
            return new TOAST(Toast.makeText(context, text, duration));
        }
    
        @NonNull
        public static TOAST makeText(@NonNull Context context, int resId, int duration) {
            return new TOAST(Toast.makeText(context, resId, duration));
        }
    
        public void show() {
            if (BuildConfig.DEBUG) {
                toast.show();
            }
        }
    }
    

    【讨论】:

    • 如果我们使 TOAST extends Toast 可以简化吗?
    • @RyanR 没有理由延长 toast。 Toast.class 中也没有您需要访问的内容。
    • @RyanR 无论您是否扩展Toast,您想要的TOAST 的示例用法都将保持不变。
    • 是的,我只是想我们是否可以在show() 中使用super.show(),但这并没有简化任何事情。
    • @RyanR 谢谢,这是有道理的 - 不需要上下文
    【解决方案2】:

    考虑“组合而不是继承”,我相信我们可以找到一个更简单的解决方案。

    public class DebugToast {
    
        public static class Builder {
    
            private final Toast toast;
    
            public Builder(Context context, String message, int length) {
                toast = Toast.makeText(context, message, length);
            }
    
            public Builder(Context context, @StringRes int message, int length) {
                toast = Toast.makeText(context, message, length);
            }
    
            public void show() {
                if (BuildConfig.DEBUG) toast.show();
            }
        }
    
        public static Builder makeText(Context context, String message, int length) {
            return new Builder(context, message, length);
        }
    
        public static Builder makeText(Context context, @StringRes int message, int length) {
            return new Builder(context, message, length);
        }
    }
    

    【讨论】:

    • 我完全赞成更简单的实现,但保持使用接近Toast.makeText(...).show() 是非常可取的。
    • 用法为DebugToast.makeText(...).show()。或您想要的任何其他班级名称。
    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2021-05-25
    • 2019-09-06
    • 2018-04-20
    • 2021-03-15
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多