版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bbtianshi/article/details/81239909
对应的一个工具类
public class ToastUtils {
private static Toast sToast;
private static Handler handler = new Handler(Looper.getMainLooper());
public static void showToast(final Context context, final String msg) {
if (sToast == null) {
if (Looper.getMainLooper()==Looper.myLooper()){
initToast(context, msg);
}else {
handler.post(new Runnable() {
@Override
public void run() {
initToast(context, msg);
}
});
}
}
//判断当前代码是否是主线程
if (Looper.myLooper() == Looper.getMainLooper()) {
sToast.setText(msg);
sToast.show();
} else {
handler.post(new Runnable() {
@Override
public void run() {
sToast.setText(msg);
sToast.show();
}
});
}
}
private static void initToast(Context context, String msg) {
sToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
sToast.setText(msg);
}
}
接下来 就是自己在封装的基类 或者activity里面调用 道理都一样
//对应一个点击事件toast一下
tv_apply_selled_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showMsg("格式错误");
}
});
下面的方法是自己定义的方法 里面对应的msg 就是toast的内容
public void showMsg(String msg) {
ToastUtils.showToast(ApplyjyActivity.this,msg);
}
showMSG