哈哈,蠻開心的,今天收到了簡(jiǎn)友的第一個(gè)喜歡费坊,謝謝~
相信大家都用過(guò)Toast提示:Toast.makeText(context,message, duration).show();
一行很簡(jiǎn)單的代碼架专。但是有一個(gè)問(wèn)題就是同窘,當(dāng)我頻繁操作一個(gè)按鈕的時(shí)候,就會(huì)看到Toast一直顯示部脚,甚至有時(shí)候跳出這個(gè)Activity或者是App它還是按照順序逐一顯示想邦。今天優(yōu)化代碼,又想到了這個(gè)問(wèn)題委刘,所以就上網(wǎng)查了一下丧没,搜索到這么一篇文章,其代碼如下:
//就是定義一個(gè)全局的成員變量Toast, 這個(gè)Toast不為null的時(shí)候才去make,否則直接setText.
//為了按返回鍵后立即使Toast不再顯示锡移,重寫父類Activity的onBackPressed()方法里面去cancel你的Toast即可
private Toast mToast;
public void showToast(String text) {
if(mToast == null) {
mToast = Toast.makeText(MobileSendTopicActivity.this, text, Toast.LENGTH_SHORT);
} else {
mToast.setText(text);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
public void cancelToast() {
if (mToast != null) {
mToast.cancel();
}
}
public void onBackPressed() {
cancelToast();
super.onBackPressed();
}
按照他的思路呕童,我就在自己的項(xiàng)目里去實(shí)現(xiàn),但是由于我的項(xiàng)目里有自定義的Toast(通過(guò)new Toast(context)
的方法實(shí)現(xiàn))和Toast.makeText(context,message, duration).show();
不同淆珊,兩種混合使用拉庵,就會(huì)在程序中報(bào)類似以下錯(cuò)誤:
為了避免這種錯(cuò)誤,要么不用自定義的Toast(不可能)套蒂,要么全部用自定義的(工作量有點(diǎn)大钞支,不想改),所以我就在類中定義了兩個(gè)全局成員變量
private static Toast mToast;
private static Toast mCustomToast;
下面貼上我的Tst.java代碼:
public class Tst {
private Tst() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}
private static Toast mToast;
private static Toast mCustomToast;
private static Toast initToast(Context context,CharSequence message, int duration) {
if (mToast == null) {
mToast = Toast.makeText(context, message,duration);
}
mToast.setText(message);
mToast.setDuration(duration);
return mToast;
}
private static Toast initToast(Context context,int message, int duration) {
if (mToast == null) {
mToast = Toast.makeText(context, message,duration);
}
mToast.setText(message);
mToast.setDuration(duration);
return mToast;
}
private static Toast initCustomToast(Context context, CharSequence message, ToastType toastType, int duration) {
LinearLayout toastView = initToastView(context, message, toastType);
if (mCustomToast == null) {
mCustomToast = new Toast(context);
}
mCustomToast.setGravity(Gravity.CENTER, 0, 0);
mCustomToast.setDuration(duration);
mCustomToast.setView(toastView);
return mCustomToast;
}
/**
* 短時(shí)間顯示Toast
* @param context
* @param message
*/
public static void showShort(Context context, CharSequence message) {
initToast(context,message,Toast.LENGTH_SHORT).show();
}
/**
* 短時(shí)間顯示Toast
* @param context
* @param message
*/
public static void showShort(Context context, int message) {
initToast(context,message,Toast.LENGTH_SHORT).show();
}
/**
* 自定義view Toast
*/
public static void showShort(Context context, CharSequence message, ToastType toastType) {
initCustomToast(context, message, toastType, Toast.LENGTH_SHORT).show();
}
/**
* 長(zhǎng)時(shí)間顯示Toast
* @param context
* @param message
*/
public static void showLong(Context context, CharSequence message) {
initToast(context,message,Toast.LENGTH_LONG).show();
}
/**
* 長(zhǎng)時(shí)間顯示Toast
* @param context
* @param message
*/
public static void showLong(Context context, int message) {
initToast(context,message,Toast.LENGTH_LONG).show();
}
public static void showLong(Context context, CharSequence message, ToastType toastType) {
initCustomToast(context, message, toastType, Toast.LENGTH_LONG).show();
}
/**
* 自定義顯示Toast時(shí)間
* @param context
* @param message
* @param duration
*/
public static void show(Context context, CharSequence message, int duration) {
initToast(context,message,duration).show();
}
/**
* 自定義顯示Toast時(shí)間
* @param context
* @param message
* @param duration
*/
public static void show(Context context, int message, int duration) {
initToast(context,message,duration).show();
}
public static void show(Context context, int msgId, int duration, ToastType toastType) {
CharSequence message = context.getResources().getText(msgId);
initCustomToast(context,message,toastType ,duration).show();
}
private static LinearLayout initToastView(Context context, CharSequence message, ToastType toastType) {
int resId;
int dp2px10 = DensityUtils.dp2px(context, 10);
LinearLayout layout = new LinearLayout(context);
layout.setBackgroundResource(R.drawable.bg_toast);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setMinimumWidth(DensityUtils.dp2px(context, 122));
ImageView imageCodeProject = new ImageView(context);
switch (toastType) {
default:
case DONE:
resId = R.drawable.icon_toast_done;
break;
case WARNING:
resId = R.drawable.icon_toast_warning;
break;
}
imageCodeProject.setImageResource(resId);
LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams1.setMargins(0, dp2px10 * 2, 0, 0);
layout.addView(imageCodeProject, layoutParams1);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams2.setMargins(dp2px10, DensityUtils.dp2px(context, 14) , dp2px10, DensityUtils.dp2px(context, 22));
TextView textView = new TextView(context);
textView.setText(message);
textView.setTextColor(Color.WHITE);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(textView, layoutParams2);
return layout;
}
}
———————————————分隔線———————————————————
事實(shí)證明操刀,我的這種方法不可取啊烁挟,由于toast被定義為static
,并且在產(chǎn)生Toast的時(shí)候傳入了Context
,使得context一直得不到釋放骨坑,最終導(dǎo)致了內(nèi)存泄漏問(wèn)題撼嗓。在此,推薦大家讀如何在你的應(yīng)用中正確使用Context或者Context欢唾,什么是Context且警?這篇文章。