一、Toast介绍

平时我们在Android开发中会经常用到一个叫Toast的东西,官方解释如下

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

Toast最基本的用法很简单,不用说大家都会(这里切记要调用show()去显示)

public static void showToast(Context context){
Toast.makeText(context, "欢迎关注水寒的CSDN博客", Toast.LENGTH_LONG).show();
}

二、自定义Toast

上面的Toast一般显示在手机偏下的一个位置上,有的时候我们需要将这个Toast的位置或者内容进行修改,比如让显示在屏幕中间,让内容里面有图片,这样的修改也比较容易,Toast有一个和ActionBar类似的方法setView(),这个方法就是提供给我们自定义Toast的。

public static void showToast(Context context){
LayoutInflater inflater = LayoutInflater.from(context);
View toastView = inflater.inflate(R.layout.toast_test_custome, null);
Toast toast = new Toast(context);
toast.setDuration(3000);
toast.setView(toastView); //设置自定义view
toast.setGravity(Gravity.CENTER, 0, 0); //控制显示到屏幕中间
toast.show(); //注意:一定要调用才能显示
}

布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/custom_toast_bg"
android:padding="15dip"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:scaleType="centerCrop"
android:src="@drawable/shuihan"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dip"
android:textSize="16sp"
android:text="欢迎访问水寒的CSDN博客"
android:textColor="#ffffff"/>
</LinearLayout>

三、为什么要改变Toast的显示时长

上面的自定义非常简单,但是我们要改变Toast的显示时间就比较麻烦了,因为toast的setDuration方法只能填写两个值2000ms或者3000ms



不信你可以给setDuration设置一个10000它最长只显示3s,网上有各种延长Toast的方法,基本上都是通过定时器来延长显示的。

那么我们接下来就要思考一个问题了,我们为什么要延迟Toast的时间?为什么不用Dialog或者PopupWindow来替代?

其实原因很简单,Toast显示是不获取焦点的,所以Toast一般是用来提示用户的,而不影响用户的操作。我们可以用一个很简单的例子证明一下:

findViewById(R.id.test_toast_button).setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//showToast();
showDialog();
Log.d("shuihan", "----down----");
break;
case MotionEvent.ACTION_MOVE:
Log.d("shuihan", "----move----");
break;
case MotionEvent.ACTION_UP:
Log.d("shuihan", "----up----");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("shuihan", "----cancel----");
break;
default:
break;
}
return true;
}
});

我们分别去在ACTION_DOWN的时候去显示一下toast和dialog你再对照输出日志看一下,就会明白。

我们在微信里面录音的时候会有一个Toast大家应该都见过,如下:



这个Toast的一个很重要的特点就是只要你按着不放它就不消失,也就是说它的显示时间可以长于3s

四、实现显示时间长于3s的Toast

(本文出自水寒的CSDN博客:http://blog.csdn.net/dawanganban

我们采用的方式也是使用定时器来实现重复显示一个toast从而延长它的时间,我们先来看一个androd里面的CountDownTimer的用法

 new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
} public void onFinish() {
mTextField.setText("done!");
}
}.start();

这是官方文档上的一个例子,一个30s的倒计时,每隔1s调用一次onTick方法,该方法返回一个倒计时剩余时间,ok,接下来我们来看看toast怎么让他显示30s

    /**
* 显示Toast
* @param toast
* @param duration
*/
public static void showToast(final Toast toast, long duration, final OnToastStatus toastStatu) {
final long tickTime = 200;
mCountDownTimer = new CountDownTimer(duration, tickTime) {
@Override
public void onTick(long millisUntilFinished) {
if(millisUntilFinished <= 200){
showToast("最长可录制一分钟");
}else{
toast.show();
}
} @Override
public void onFinish() {
toast.cancel();
if(toastStatu != null){
toastStatu.toastStop();
}
}
}.start();
} public interface OnToastStatus{
public void toastStop();
} /**
* 停止显示Toast
*/
public static void stopToast(Toast toast){
if(toast != null){
toast.cancel();
}
if(mCountDownTimer != null){
mCountDownTimer.cancel();
}
}

Android自定义组件系列【17】——教你如何高仿微信录音Toast的更多相关文章

  1. Android自定义组件系列【7】——进阶实践(4)

    上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...

  2. Android自定义组件系列【6】——进阶实践(3)

    上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...

  3. Android自定义组件系列【5】——进阶实践(2)

    上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...

  4. Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动

    在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...

  5. Android自定义组件系列【3】——自定义ViewGroup实现侧滑

    有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...

  6. Android自定义组件系列【8】——遮罩文字动画

    遮罩文字的动画我们在Flash中非常常见,作为Android的应用开发者你是否也想将这种动画做到你的应用中去呢?这一篇文章我们来看看如何自定义一个ImageView来实现让一张文字图片实现文字的遮罩闪 ...

  7. Android自定义组件系列【12】——非UI线程绘图SurfaceView

    一.SurfaceView的介绍 在前面我们已经会自定义View,使用canvas绘图,但是View的绘图机制存在一些缺陷. 1.View缺乏双缓冲机制. 2.程序必须重绘整个View上显示的图片,比 ...

  8. Android自定义组件系列【1】——自定义View及ViewGroup

    View类是ViewGroup的父类,ViewGroup具有View的所有特性,ViewGroup主要用来充当View的容器,将其中的View作为自己孩子,并对其进行管理,当然孩子也可以是ViewGr ...

  9. Android自定义组件系列【15】——四个方向滑动的菜单实现

    今天无意中实现了一个四个方向滑动的菜单,感觉挺好玩,滑动起来很顺手,既然已经做出来了就贴出来让大家也玩弄一下. 一.效果演示 (说明:目前没有安装Android模拟器,制作的动态图片太卡了,就贴一下静 ...

随机推荐

  1. LTE协议

    开启通信不归路的第一炮!----LTE整体框架和协议架构概述 (2015-03-09 09:07:04) 转载▼   分类: 通信那些事儿 听说“态度.决心.毅力和细心”一定可以成就一个人!而我们也总 ...

  2. jquery 插件 国外

    http://www.jqueryrain.com/demo/jquery-portfolio-gallery-plugin/

  3. WCF IIS部署

    创建WCFHost应用程序 Iservice.cs using System; using System.Collections.Generic; using System.Linq; using S ...

  4. 2. nmap扫描神器总结

    -----------------nmap(选项)(参数)------------------O:激活操作探测: -P0:值进行扫描,不ping主机: -PT:是同TCP的ping: -sV:探测服务 ...

  5. js学习笔记3:with语句的使用

    with语句 with是ECMAScript规定的内容,主要用于设置代码在特定对象中的作用域. var sMessage = "hello"; with(sMessage) { c ...

  6. CodeForces - 633B A Trivial Problem 数论-阶乘后缀0

    A Trivial Problem Mr. Santa asks all the great programmers of the world to solve a trivial problem. ...

  7. 权限验证MVC

    http://www.jb51.net/article/20147.htm  引用 <authentication mode="Forms"><!--权限受到阻碍 ...

  8. Unity T4M 中文讲解

    http://blog.csdn.net/tianmao111/article/details/46482963 现在在u3d圈里流行了一种地形转换器(或者叫编辑器吧),但是经查阅之后,似乎还没有中文 ...

  9. jzoj6004. 【PKUWC2019模拟2019.1.17】集合 (组合数学)

    题面 题解 这种题目就是要好好推倒 我们枚举最小的数是哪一个,那么答案就是\[Ans=\sum_{i=1}^nT^i{n-i\choose k-1}\] 因为有\[\sum_{i=p}^n{n-i\c ...

  10. Glassfish 4 修改server.log 等配置

    如果所示: