前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >XYLibrary笔记三:XToast使用笔记

XYLibrary笔记三:XToast使用笔记

作者头像
项勇
发布2023-03-24 13:25:31
3310
发布2023-03-24 13:25:31
举报
文章被收录于专栏:项勇项勇

源码传送门(点击末尾阅读原文)

用于替换原生Toast,防止高版本出现的显示问题,高版本加显示权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

示例:

代码语言:javascript
复制
ToastUtils.make(this)
        .Text("AAAAAA")
        .TextColor(R.color.colorPrimary) //字体颜色
        .TextSize(50) //字体大小
        .BackGroundColor(R.color.colorAccent) //背景颜色
        .LayoutParamsX(0) //离X左边距离
        .LayoutParamsY(150) //离Y坐标距离
        .Gravity(Gravity.BOTTOM) //Toast显示位置的重心设置
        .RoundedCorners(3) //背景圆角大小
        .ShowTime(ToastUtils.LENGTH_SHORTSHORT) //显示时长
        .show();

代码

代码语言:javascript
复制
package cn.xy.library.util.toast;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import cn.xy.library.App;
import cn.xy.library.util.convert.ConvertUtils;
import cn.xy.library.util.image.ImageUtils;

/**
 * xiangy add by 12:48 2021/1/5
 */
public class ToastUtils {
  private WindowManager mWindowManager;
  private LayoutParams mLayoutParams;
  private static TextView mView;
  private static final int HIDE = 1;
  private static ToastUtils mToastView;
  private static String Text = "";
  private static int TextColor = 0xFF000000;
  private static int BackGroundColor = 0xDCDCDCDC;
  private static float TextSize = 20.0F;
  private static int Radius = 15;
  private static int LayoutParamsX = 0;
  private static int LayoutParamsY = 35;
  private static int LayoutParamsGravity = Gravity.BOTTOM;
  int DelayMillis_sh = 2000;
  public static final int LENGTH_SHORTSHORT = 0;
  public static final int LENGTH_SHORT = 1;
  public static final int LENGTH_LONG = 2;
  public static final int LENGTH_LONGLONG = 3;

  private ToastUtils() {
    throw new UnsupportedOperationException("u can't instantiate me...");
  }

  public static ToastUtils make() {
    if (mToastView == null) {
      mToastView = new ToastUtils(App.getApp());
      return mToastView;
    } else {
      return mToastView;
    }
  }

  public ToastUtils Text(String toastString) {
    Text = toastString;
    return mToastView;
  }

  public ToastUtils TextSize(int size){
    TextSize = size;
    return mToastView;
  }
  public ToastUtils TextColor(int color){
    TextColor = color;
    return mToastView;
  }
  public ToastUtils TextColor(String color){
    TextColor = ConvertUtils.string2color(color);
    return mToastView;
  }

  public ToastUtils RoundedCorners(int radius){
    Radius = radius;
    return mToastView;
  }
  public ToastUtils BackGroundColor(int backgroundcolor){
    BackGroundColor = backgroundcolor;
    return mToastView;
  }
  public ToastUtils BackGroundColor(String backgroundcolor){
    BackGroundColor = ConvertUtils.string2color(backgroundcolor);
    return mToastView;
  }

  public ToastUtils LayoutParamsX(int x){
    LayoutParamsX = x;
    return mToastView;
  }

  public ToastUtils LayoutParamsY(int y){
    LayoutParamsY = y;
    return mToastView;
  }

  public ToastUtils Gravity(int gravity){
    LayoutParamsGravity = gravity;
    return mToastView;
  }

  public ToastUtils ShowTime(int i){
    switch (i){
      case LENGTH_SHORTSHORT:
        DelayMillis_sh = 1500;
        break;
      case LENGTH_SHORT:
        DelayMillis_sh = 2500;
        break;
      case LENGTH_LONG:
        DelayMillis_sh = 3500;
        break;
      case LENGTH_LONGLONG:
        DelayMillis_sh = 4500;
        break;
    }
    return mToastView;
  }

  private Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
      if (msg.what == HIDE) {
        hideView();
      }
    }
  };

  private ToastUtils(Context context) {
    mView = new TextView(context);
    // 设置窗体显示类型
    mWindowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
    mLayoutParams = new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0,
        LayoutParams.TYPE_PHONE,
        LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE,
        PixelFormat.RGBA_8888);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
      mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    }
  }

  public void show(){
    if (mView != null) {
      mView.setText(Text);
      mView.setTextColor(TextColor);
      mView.setPadding(10, 10, 10, 10);
      mView.setBackgroundColor(BackGroundColor);
      ImageUtils.setRoundedCorners(mView,Radius);
      mView.setTextSize(TextSize);
    }
    mLayoutParams.gravity = LayoutParamsGravity;
    mLayoutParams.x = LayoutParamsX;
    mLayoutParams.y = LayoutParamsY;
    showView();
  }
  private void showView() {
    if (mView.getParent() == null) {
      mWindowManager.addView(mView, mLayoutParams);
    }
    mHandler.removeMessages(HIDE);
    mHandler.sendEmptyMessageDelayed(HIDE, DelayMillis_sh);
  }

  private void hideView() {
    if (mView.getParent() != null) {
      mWindowManager.removeView(mView);
    }
  }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档