很簡單, 直接上代碼: ?
package com.wh.view.toast;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;
public class ToastUtil {
public static final String TAG = "byWh";
public static final String TAG2 = "ToastUtil - ";
static Toast mToast;
/**
* context 必須是Activity 也就是UI線程
*/
public static void show(Context context, String error) {
if (error.contains("HTTP 429")) {
error = "操作過快,請稍后再試";
}
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(context, error, Toast.LENGTH_LONG);
mToast.show();
}
/**
* context 可以是任何上下文
*/
public static void show2(final Context context, final String error) {
handlerToast.post(new Runnable() {
public void run() {
ToastUtil.show(context, error);
}
});
}
static Handler handlerToast = new Handler(Looper.getMainLooper());
}
覺得有用,請?點(diǎn)贊?+?關(guān)注,您的支持是我最大的動(dòng)力!
覺得有用,請?點(diǎn)贊?+?關(guān)注,您的支持是我最大的動(dòng)力!
覺得有用,請?點(diǎn)贊?+?關(guān)注,您的支持是我最大的動(dòng)力!
文章來源地址http://www.zghlxwxcb.cn/news/detail-595008.html
文章來源:http://www.zghlxwxcb.cn/news/detail-595008.html
到了這里,關(guān)于Android Toast工具類 解決Toast不消失 解決非UI線程不能Toast的問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!