//在toas初始化t前后加上:(前)Looper.prepare();(后)Looper.loop();即可,非常好用
Looper.prepare();
if (url!=null){
Toast toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
//ToastUtils.newToast(context,"保存成功");
}else {
Toast toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
//ToastUtils.newToast(context,"保存失败");
}
Looper.loop();
自定义:
public void showToast(String msg) {
if (toast == null) {
toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.show();
}
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.zz.ui.R;
import com.zz.utils.DPUtils;
public class CustomToast {
public static void showToast(Context mContext, String message, int time) {
View layout = LayoutInflater.from(mContext).inflate(R.layout.toast_layout, null);
TextView text = (TextView) layout.findViewById(R.id.toast_message_textview);
text.setText(message);
Toast toast = new Toast(mContext);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, DPUtils.dip2px(mContext, 80));
toast.setDuration(time);
toast.setView(layout);
toast.show();
}
public static void showToast(Context mContext, int messageRes, int time) {
showToast(mContext, mContext.getString(messageRes), time);
}
public static void showToast(Context mContext, int messageRes) {
showToast(mContext, mContext.getString(messageRes), Toast.LENGTH_SHORT);
}
public static void showToast(Context mContext, String message) {
showToast(mContext, message, Toast.LENGTH_SHORT);
}
}
package com.onewave.boost.clean.utils;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.onewave.boost.clean.R;
import com.onewave.boost.clean.adapter.ToastCoinAdapter;
import com.onewave.boost.clean.utils.TextStyleUtils;
/**
* author : jian11058
* time : 2022/1/10
* desc :
*/
public class ToastUtils {
public static void showMessage(Context context, String hint,boolean isFinish){
View view = LayoutInflater.from(context).inflate(R.layout.item_toast_big, null);
TextView tv_hint=view.findViewById(R.id.toast_tv);
if (!TextUtils.isEmpty(hint)){
tv_hint.setText(hint);
}
if (isFinish){
TextStyleUtils.setTextColorSpan(tv_hint,hint,context.getResources().getColor(R.color.red_yellow),12,19);
}else {
TextStyleUtils.setTextColorSpan(tv_hint,hint,context.getResources().getColor(R.color.red_yellow),11,19);
}
showView(view,context,true);
}
public static void showBothCoin(Context mContext,String redCoin,String goldCoin){
View view = LayoutInflater.from(mContext).inflate(R.layout.item_toast_both, null);
RecyclerView recyclerView =view.findViewById(R.id.toast_get_recycleview);
ToastCoinAdapter adapter=new ToastCoinAdapter(redCoin,goldCoin);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(mContext,adapter.getData().size()));
showView(view,mContext,true);
}
private static void showView(View view,Context context,boolean isShort){
Toast toast=new Toast(context); //上下文
toast.setGravity(Gravity.CENTER,0,0); //位置居中
if (isShort){
toast.setDuration(Toast.LENGTH_SHORT); //设置短暂提示
}else {
toast.setDuration(Toast.LENGTH_LONG); //设置短暂提示
}
toast.setView(view); //把定义好的View布局设置到Toast里面
toast.show();
}
}