android 在子线程中使用Toast.makeText()及 自定义show Toast

//在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();
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值