android 自定义Toast以及Toast覆盖显示

1.自定义Toast布局文件 toast_show.xmll

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000">
<LinearLayout
    android:id="@+id/ll_toast"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:gravity="center"
    >
    <TextView
        android:id="@+id/tv_toast"
        android:textSize="18sp"
        android:text="hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

</RelativeLayout>

1.ToastUtils工具类

public class ToastUtils {

    protected static Toast toast   = null;
    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;

    public static void showToast(Context context, String flag){

        View layout = View.inflate(context, R.layout.toast_show, null);//加载一个自定义的toast布局文件
        //获取自定义布局的控件
        final LinearLayout ll_toast = layout.findViewById(R.id.ll_toast);
        final TextView tv_toast = layout.findViewById(R.id.tv_toast);
        if(toast==null){
            toast = new Toast(context);//创建一个Toast示例
            toast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
            if (flag.equals("Y")){
                ll_toast.setBackgroundResource(R.drawable.toast_bg);
                tv_toast.setText("hello world"+flag);
            }else {
                ll_toast.setBackgroundResource(R.drawable.toast_bg2);
            }

            toast.setView(layout);//给toast设置布局
            toast.setGravity(Gravity.FILL, 0, 0);//设置toast在屏幕中的显示位置
            toast.show();
            oneTime=System.currentTimeMillis();
        }else{
            twoTime=System.currentTimeMillis();
            if(flag.equals(oldMsg)){
                if(twoTime-oneTime>Toast.LENGTH_SHORT){
                    toast.show();
                }
            }else{
                oldMsg = flag;
                if (flag.equals("Y")){
                    ll_toast.setBackgroundResource(R.drawable.toast_bg);
                    tv_toast.setText("hello world"+flag);
                }else {
                    ll_toast.setBackgroundResource(R.drawable.toast_bg2);
                    tv_toast.setText("============"+flag);
                }
                toast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
                toast.setView(layout);//给toast设置布局
                toast.setGravity(Gravity.FILL, 0, 0);//设置toast在屏幕中的显示位
                toast.show();
            }
        }
        oneTime=twoTime;
    }
}

3.ActivityMain.java中点击事件引用

 private  static long  lastTime=0;
    public void onClickToast(View v){
       final long nowTime= SystemClock.uptimeMillis();
        if (nowTime-lastTime>10000){
            ToastUtils.showToast(context, "Y");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    ToastUtils.showToast(context, "N");
                }
            },1000); 
        }else {
            lastTime=nowTime;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值