/**
* //帶圖片的吐司窗體
*
* @param context 上下文
* @param message 吐司內(nèi)容
* @param imageId 圖片ID
*/
public static void ToastWithImage(final Context context, final Object message, int imageId) {
Toast toast = Toast.makeText(context.getApplicationContext(), "" + message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
if (imageId != 0) {
ImageView imageCodeProject = new ImageView(context.getApplicationContext());
imageCodeProject.setImageResource(imageId);
toastView.addView(imageCodeProject, 0);
toast.show();
}
if (imageId == 0) {
toast.show();
}
}
/**
* 快速快速toast 讓其在主線程進行
*
* @param mContext
* @param msg
*/
public static void showToast(final Context mContext, final String msg) {
try {
if (Looper.getMainLooper() == Looper.myLooper()) {
//說明是在主線程
safeToast(mContext, msg);
} else {
//說明不是在主線程
if (handler == null) {
handler = new Handler(Looper.getMainLooper());
}
handler.post(new Runnable() {
@Override
public void run() {
safeToast(mContext, msg);
// toast(mContext, msg);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 安全的toast 避免內(nèi)存溢出
*
* @param mContext
* @param msg
*/
private static void safeToast(Context mContext, String msg) {
try {
if (mToast == null || mToast.get() == null) {
mToast = new SoftReference<Toast>(Toast.makeText(mContext, msg, Toast.LENGTH_SHORT));
}
mToast.get().setText("" + msg);
mToast.get().show();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param mContext
* @param msg
*/
private static void toast(Context mContext, String msg) {
if (toast == null) {
toast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
}
toast.setText(msg);
toast.show();
}
Android工具類之 toast
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 短信分享 郵件分享 短信分享 短信分享2 上邊的短信分享在華為手機上分享會沒有內(nèi)容,因此采用下邊的方法,親測華為手...
- 前言 相信大部分仁兄在使用系統(tǒng)Toast的時候,都感覺不太盡如人意,因為系統(tǒng)Toast顯示的位置比較固定,并且字體...