仅在调试变体上显示的 Toast
Toast that only shows on debug variants
我想创建一个简单的助手 class,它只显示调试变体的 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)
您不能将 Toast
(基础 class)的实例强制转换为派生的 class 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();
}
}
}
思考'composing over inheritance',我相信我们可以找到更简单的解决方案。
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);
}
}
我想创建一个简单的助手 class,它只显示调试变体的 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)
您不能将 Toast
(基础 class)的实例强制转换为派生的 class 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();
}
}
}
思考'composing over inheritance',我相信我们可以找到更简单的解决方案。
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);
}
}