toastcustom自定义显示风格的toast

HaiyuKing HaiyuKing     2022-08-31     445

关键词:

版权声明:本文为HaiyuKing原创文章,转载请注明出处!

前言

基于系统Toast的自定义显示风格的Toast。

效果图

代码分析

  • ToastCustom类基于系统Toast,不是继承Toast,只是通过toast.setView(view)方法引用自定义的显示风格布局文件,达到自定义显示风格的目的。
  • 为了和Toast用法保持一致,ToastCustom类中也使用了makeText、show、setGravity、setText方法。方便在项目中直接替换Toast。
  • 下面分析下ToastCustom类中的setText()方法

  该方法用来修改显示的文本,刚开始的时候,我直接使用了toast.setText()方法进行修改文本:

  public void setText(CharSequence s){
     toast.setText(s);
  }

  但是程序崩溃了。分析原因得知toast的setText方法是找到系统Toast的系统布局文件mNextView中的ID值为message的TextView控件,然后修改这个TextView控件的文本实现的。(下面是源码)

    /**
     * Update the text in a Toast that was previously created using one of the makeText() methods.
     * @param s The new text for the Toast.
     */
    public void setText(CharSequence s) {
        if (mNextView == null) {
            throw new RuntimeException("This Toast was not created with Toast.makeText()");
        }
        TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);
        if (tv == null) {
            throw new RuntimeException("This Toast was not created with Toast.makeText()");
        }
        tv.setText(s);
    }

但是在ToastCustom类中我们已经修改了toast的布局文件引用,所以直接使用toast.setText()方法的时候,肯定找不到ID值为message的TextView控件。正确的代码如下:

    public void setText(CharSequence s){
        TextView tv = (TextView) toast.getView().findViewById(R.id.tv_toast);
        tv.setText(s);
    }

使用步骤

一、项目组织结构图

 

注意事项:

1、 导入类文件后需要change包名以及重新import R文件路径

2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖

二、导入步骤

将ToastCustom复制到项目中,并重新import R文件

package com.why.project.toastcustom.views;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.why.project.toastcustom.R;


/**
 * Create By HaiyuKing
 * Used 自定义Toast显示风格,基于系统Toast【可以控制显示样式、位置,不可以控制显示时间、动画,不可触发】
 * 注意 Toast布局在源码中的布局是采用LinearLayout
 */
public class ToastCustom {

    private static ToastCustom toastCustom;
    private Toast toast;

    public static ToastCustom makeText(Context context, CharSequence text, int duration){
        LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflate.inflate(R.layout.toast_custom_view, null);
        TextView tv = (TextView)view.findViewById(R.id.tv_toast);
        tv.setText(text);
        if (toastCustom == null) {
            toastCustom = new ToastCustom();
        }
        toastCustom.toast = new Toast(context);
        toastCustom.toast.setView(view);
        toastCustom.toast.setDuration(duration);

        return toastCustom;
    }

    public static ToastCustom makeText(Context context, int resId, int duration){
        return ToastCustom.makeText(context,context.getResources().getString(resId),duration);
    }

    public void show(){
        toast.show();
    }

    /**
     * 1、gravity是输入Toast需要显示的位置,例如CENTER_VERTICAL(垂直居中)、CENTER_HORIZONTAL(水平居中)、TOP(顶部)等等。
     * 2、xOffset则是决定Toast在水平方向(x轴)的偏移量,偏移量单位为,大于0向右偏移,小于0向左偏移
     * 3、yOffset决定Toast在垂直方向(y轴)的偏移量,大于0向下偏移,小于0向上偏移,想设大值也没关系,反正Toast不会跑出屏幕。*/
    public void setGravity(int gravity, int xOffset, int yOffset) {
        toast.setGravity(gravity, xOffset, yOffset);
    }

    public void setText(CharSequence s){
        TextView tv = (TextView) toast.getView().findViewById(R.id.tv_toast);
        tv.setText(s);
    }

    public void setText(int resId){
        TextView tv = (TextView) toast.getView().findViewById(R.id.tv_toast);
        tv.setText(resId);
    }
}
ToastCustom.java

将toast_custom_view.xml文件复制到项目中

<?xml version="1.0" encoding="utf-8"?>
<!-- 自定义显示风格的Toast的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_custom_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_custom_view_bg"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/toast_custom_text_paddingTB"
        android:paddingBottom="@dimen/toast_custom_text_paddingTB"
        android:paddingLeft="@dimen/toast_custom_text_paddingLR"
        android:paddingRight="@dimen/toast_custom_text_paddingLR"
        android:text=""
        android:textColor="@android:color/white"
        android:textSize="@dimen/toast_custom_text_size"/>

</LinearLayout>
toast_custom_view.xml

将toast_custom_view_bg.xml文件复制到项目中

<?xml version="1.0" encoding="utf-8"?>
<!-- 自定义显示风格的Toast的布局文件的背景 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <!-- 圆角 -->
    <corners android:radius="@dimen/toast_custom_view_bg_corners" />
    <!-- 描边 -->
    <stroke
        android:width="1dp"
        android:color="#666666" />
    <!-- 填充 -->
    <solid android:color="#666666" />

</shape>
toast_custom_view_bg.xml

在dimens.xml中添加以下颜色标记的代码

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <!-- **************自定义显示风格的Toast布局文件********************* -->
    <!-- 提示文字的大小 -->
    <dimen name="toast_custom_text_size">18sp</dimen>
    <!-- 提示文字的内边距(上下) -->
    <dimen name="toast_custom_text_paddingTB">10dp</dimen>
    <!-- 提示文字的内边距(左右) -->
    <dimen name="toast_custom_text_paddingLR">20dp</dimen>
    <!-- 背景的圆角 -->
    <dimen name="toast_custom_view_bg_corners">30dp</dimen>
</resources>

三、使用方法

ToastCustom toastCustom = ToastCustom.makeText(this,"自定义Toast显示风格",Toast.LENGTH_LONG);
toastCustom.show();

如果想要修改文本或者显示位置,参考下面的代码:

ToastCustom toastCustom = ToastCustom.makeText(this,"自定义Toast显示风格",Toast.LENGTH_LONG);
toastCustom.setText(R.string.app_name);
toastCustom.setGravity(Gravity.CENTER,0,0);
toastCustom.show();

混淆配置

参考资料

android 自定义Toast显示风格

http://blog.csdn.net/yongxinzhenxi/article/details/25069415

Android:谈一谈安卓应用中的Toast情节(基础)

http://www.cnblogs.com/net168/p/4041763.html

Android:剖析源码,随心所欲控制Toast显示

http://www.cnblogs.com/net168/p/4058193.html

项目demo下载地址

https://github.com/haiyuKing/ToastCustomDemo

android基础:自定义带图片的toast(代码片段)

...ff0c;所以,为了满足项目的需求,我们需要用到自定义的Toast。一、Toast布局文件自定义Toast首先我们需要给Toast指定一 查看详情

android的toast怎么自定义显示时间长度?

android新手求解Android中Toast的显示时间为特定时间且不可更改,但是有时候我们开发设计需要让Toast显示更长时间,或者自己完全控制Toast的显示和关闭。通过查看Toast类的源码,可以看出,这有点难为它了,Toast类本身并没有提供... 查看详情

uniapp原生toast弹窗提示(可穿透所有界面)ba-toast(代码片段)

...#xff0c;接入简单,功能强大。支持穿透所有界面支持自定义显示位置支持显示图标,可自定义(默认有“success”、“error”、“loading”,参照uniapp)支持自定义背景、字体、图标颜色支持自定义字体大小支持自... 查看详情

uniapp原生toast弹窗提示(可穿透所有界面)ba-toast(代码片段)

...#xff0c;接入简单,功能强大。支持穿透所有界面支持自定义显示位置支持显示图标,可自定义(默认有“success”、“error”、“loading”,参照uniapp)支持自定义背景、字体、图标颜色支持自定义字体大小支持自... 查看详情

android中的toast源码分析和自定义toast(代码片段)

我的主页Demo下载地址一、系统自带Toast的源码分析1.Toast的调用显示学过Android的人都知道,弹出一个系统API吐司只需要一行代码,调用的是Toast对象的makeText()方法,方法里给一个上下文,显示的文字,和显示的... 查看详情

自定义toast,显示号码归属地的customtoast

publicclassCustomToastimplementsOnTouchListener{privateContextmContext;privateViewview;privateWindowManagerwindowManager;privateintstartX;privateintstartY;privateWindowManager.LayoutParamsparams;publi 查看详情

android基础:自定义带图片的toast(代码片段)

...ff0c;所以,为了满足项目的需求,我们需要用到自定义的Toast。一、Toast布局文件自定义Toast首先我们需要给Toast指定一个布局文件toast_email.xml,代码如下:<?xmlversion="1.0"encoding="utf-8"?><LinearLa... 查看详情

vue2.0自定义提示框(toast)组件

1.自定义提示框组件src/components/Toast/index.js/***自定义提示框(Toast)组件*/varToast={};varshowToast=false,//存储toast显示状态showLoad=false,//存储loading显示状态toastVM=null,//存储toastvmloadNode=null;//存储loading节点元素Toast.install 查看详情

自定义toast解决快速点击时重复弹出,排队无止尽

解决办法:自定义MyToast类:publicclassMyToast{/**之前显示的内容*/privatestaticStringoldMsg;/**Toast对象*/privatestaticToasttoast=null;/**第一次时间*/privatestaticlongoneTime=0;/**第二次时间*/privatestaticlongtwoTime=0;/***显示Toast 查看详情

uniapp原生toast弹窗提示(可穿透所有界面)ba-toast(代码片段)

...#xff0c;接入简单,功能强大。支持穿透所有界面支持自定义显示位置支持显示图标,可自定义(默 查看详情

android之toast通知的几种自定义用法

Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。1.默认用法[html] viewplain copy print?Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();  2.Fragment中的用法[html]& 查看详情

小程序如何封装自定义组件(toast)(代码片段)

1、创建和pages同级的component目录新建一个myToast目录例如:2、myToast.wxml文件内容:<!--自定义toast组件--><!--name模块名称--><templatename="toast"><!--catchtouchmove=‘xxx’遮罩层的滚动穿透--><!--isHide显示消失- 查看详情

angular2自定义弹出组件toast(使用路由)

参考技术A原理:使用Angular2的命名路由插座,一个用来显示app正常的使用,一个用来显示弹出框浏览器的导航栏中则这样显示路由配置toast内容创建用来跳转至popup路由的服务,例如popup.service使用:一、在app.module.ts中将服务导进... 查看详情

toast的高级自定义方式-循序渐进带你了解toast(代码片段)

...户进行交互的一个提示框。接下来,让我们一步步自定义Toast,全方位的玩转Toast,实现它的不同显示需求。从此再也不怕提示的各种变态需求。~先来看看效果图,苦逼的华为手机,4.4版本,没root,只能连上电脑, 查看详情

android怎么缩短toast的显示时间

...)Toast.LENGTH_SHORT(2秒)如果你需要更短的时间就只能自定义Toast了,不过也可以做一个计时器Timer,到点关闭取消Toast。finalToasttoast=Toast.makeText(this,"自定义Toast的时间",Toast.LENGTH_LONG);finalTimertimer=newTimer();timer.schedule(newTimerTa... 查看详情

android开发自定义toast(可以带图标)(代码片段)

一、自定义Toast测试如下1.不带图标的Toast:2.带图标的Toast:二、自定义Toast的布局1.Toast的布局代码:<?xmlversion="1.0"encoding="utf-8"?> 查看详情

如何从自定义列表视图中获取选定项目并在 toast 消息中打印?

】如何从自定义列表视图中获取选定项目并在toast消息中打印?【英文标题】:Howtogetselecteditemsfromcustomlistviewandprintthatintoastmessage?【发布时间】:2019-07-1920:39:24【问题描述】:在这里,我试图从自定义列表视图中获取选定的项目... 查看详情

如何在android开发中熟练使用五种toast的特效

...ontext(),"默认Toast样式",Toast.LENGTH_SHORT).show();  自定义显示位置效果,代码为:  toast=Toast.makeText(getApplicationContext(),"自定义位置Toast",Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER,0,0);toast.show();  带图片效果,代码为:... 查看详情