自定义的Toast类

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/button01_id"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示自定义toast" /> <Button
android:id="@+id/button02_id"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示可以控制显示时间的toast" /> <Button
android:id="@+id/button03_id"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示由代码创建的toast" /> <Button
android:id="@+id/button04_id"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在其他线程中的toast" />
</LinearLayout>

自定义toast的布局界面

toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_id"
android:orientation="vertical"
android:padding="10dp"
android:layout_width="200dp"
android:layout_height="150dp"
android:background="#000000"> <ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/kale"/> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="toast"
android:textColor="#FFF"/> </LinearLayout>

myToast.java

package com.kale.toast;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Toast; /*
* Toast自定义显示时间
* 使用方法
* 1.先初始化类 MyToast myToast = new MyToast(this);
* 2.显示消息 myToast.setText("要显示的内容");//设置要显示的内容
* myToast.show(8000); //传入消息显示时间,单位毫秒,最少2000毫秒,并且只能是2000的倍数。
* 传入0时会一直显示,只有调用myToast.cancel();时才会取消。
* 3.取消消息显示 myToast.cancel();
* */ public class MyToast { private Context mContext = null;
private Toast mToast = null;
private Handler mHandler = null;
private int duration = 0;
private int currDuration = 0;
private final int DEFAULT=2000;
private Runnable mToastThread = new Runnable() { public void run() {
mToast.show();
mHandler.postDelayed(mToastThread, DEFAULT);// 每隔2秒显示一次
if (duration != 0) {
if (currDuration <= duration) {
currDuration += DEFAULT;
}
else {
cancel();
}
} }
}; public MyToast(Context context) {
mContext = context;
currDuration=DEFAULT;
mHandler = new Handler(mContext.getMainLooper());
mToast = Toast.makeText(mContext, "", Toast.LENGTH_LONG);
} public void setText(String text) {
mToast.setText(text);
} public void show(int duration) {
this.duration = duration;
mHandler.post(mToastThread);
} public void setGravity(int gravity, int xOffset, int yOffset){
mToast.setGravity(gravity, xOffset, yOffset);
} public void setDuration(int duration){
mToast.setDuration(duration);
} public void setView(View view){
mToast.setView(view);
} public void cancel( ) {
mHandler.removeCallbacks(mToastThread);// 先把显示线程删除
mToast.cancel();// 把最后一个线程的显示效果cancel掉,就一了百了了
currDuration = DEFAULT;
}
}

MainActivity.java

package com.kale.toast;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { Button btn01,btn02,btn03,btn04; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); viewInit();
ButtonListener listener = new ButtonListener();
btn01.setOnClickListener(listener);
btn02.setOnClickListener(listener);
btn03.setOnClickListener(listener);
btn04.setOnClickListener(listener); } public class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {
case R.id.button01_id:
firstToast();
break;
case R.id.button02_id:
secondToast();
break;
case R.id.button03_id:
thirdToast();
break;
case R.id.button04_id:
otherToast();
default:
break;
}
} } public void firstToast() {
//获取LayoutInflater对象,该对象能把XML文件转换为与之一直的View对象
LayoutInflater inflater = getLayoutInflater();
//根据指定的布局文件创建一个具有层级关系的View对象
//第二个参数为View对象的根节点,即LinearLayout的ID
View layout = inflater.inflate(R.layout.toast,
(ViewGroup) findViewById(R.id.toast_layout_id)); //查找ImageView控件
//注意是在layout中查找
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.kale);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("自定义Toast"); Toast toast = new Toast(getApplicationContext());
//设置Toast的显示位置,后两个参数是偏移量
toast.setGravity(Gravity.CENTER, 0, 100);
toast.setView(layout);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
} public void secondToast() {
View view = getLayoutInflater().inflate(R.layout.toast, null);
MyToast myToast = new MyToast(MainActivity.this);
myToast.setText("显示时间为8000毫秒");//设置要显示的内容
myToast.setView(view);
myToast.show(8000); //传入消息显示时间,单位毫秒,最少2000毫秒,并且只能是2000的倍数。
} public void thirdToast() {
ImageView image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher); LinearLayout ll = new LinearLayout(getApplicationContext());
ll.addView(image); Toast toast = new Toast(getApplicationContext());
toast.setView(ll);
toast.setDuration(0);
toast.show();
} public void otherToast() {
System.out.println("other");
new Thread(){
@Override
public void run() {
Looper.prepare();//先移除
Toast.makeText(getApplicationContext(),"在其他线程中的toast",Toast.LENGTH_SHORT).show();
Looper.loop();// 进入loop中的循环,查看消息队列
}
}.start(); } private void viewInit() {
btn01 = (Button)findViewById(R.id.button01_id);
btn02 = (Button)findViewById(R.id.button02_id);
btn03 = (Button)findViewById(R.id.button03_id);
btn04 = (Button)findViewById(R.id.button04_id);
}
}

源码下载:http://download.csdn.net/detail/shark0017/7654053

Toast的用法(可以设置显示时间,自定义布局的,线程中的Toast)的更多相关文章

  1. Android开发之在子线程中使用Toast

    在子线程中使用Toast的时候,出现Force close. 错误提示:Can't create handler inside thread that has not called Looper.pr ...

  2. 如何在子线程中使用Toast和更新UI

    因为没一个Looper处理消息循环,所以子线程中无法使用Toast 方法: Looper.prepare(); Toast.makeText(getActivity(),"刷到底啦" ...

  3. Android线程中使用Toast、dialog、loading

    代码改变世界 Android线程中使用Toast.dialog.loading Loading: Thread t1 = new Thread(new Runnable() { @Override p ...

  4. Notification的基本用法以及使用RemoteView实现自定义布局

    Notification的作用 Notification是一种全局效果的通知,在系统的通知栏中显示.既然作为通知,其基本作用有: 显示接收到短消息.即时信息等 显示客户端的推送(广告.优惠.新闻等) ...

  5. Android Studio 之 在活动中使用 Toast

    •简介 Toast 是 Android 系统提供的一种非常好的提醒方式: 在程序中可以使用它将一些短小的信息通知给用户: 这些信息会在一段时间内自动消失,并且不会占用任何屏幕空间 •Toast.mak ...

  6. 【转】通知 Toast详细用法(显示view)

    原文网址:http://www.pocketdigi.com/20100904/87.html 今天学习Android通知 Toast的用法,Toast在手机屏幕上向用户显示一条信息,一段时间后信息会 ...

  7. 2018.6.2 AndroidStudio项目中的问题:===== oast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应多长时间

    oast.LENGTH_LONG和Toast.LENGTH_SHORT分别对应多长时间 在Android源码中的NotificationManagerService.java这个类中定义了两个静态变量 ...

  8. java操作Redis缓存设置过期时间

    关于Redis的概念和应用本文就不再详解了,说一下怎么在java应用中设置过期时间. 在应用中我们会需要使用redis设置过期时间,比如单点登录中我们需要随机生成一个token作为key,将用户的信息 ...

  9. Ubuntu设置显示桌面快捷键

    Ubuntu设置显示桌面快捷键 直接在系统设置中没有效果, 学习了:http://www.cnblogs.com/pluse/p/5286585.html 需要进行安装compizconfig,然后在 ...

随机推荐

  1. CentOS6安装Jenkins

    1.安装最新版JDK(作为JENKINS运行环境)# mount -t cifs //192.168.8.1/share /mnt -o username=share,password=share,n ...

  2. Codeforces Round #440 (Div. 1, based on Technocup 2018 Elimination Round 2) C - Points, Lines and Ready-made Titles

    C - Points, Lines and Ready-made Titles 把行列看成是图上的点, 一个点(x, y)就相当于x行 向 y列建立一条边, 我们能得出如果一个联通块是一棵树方案数是2 ...

  3. zabbix agent配置文件记录

    一.无论主动还是被动模式都要配置server和linstenPort 二.若要设置主动模式那么要添加ServerActive,若不添加则默认为被动模式

  4. 034 Spark Sql的入门介绍

    一:进程介绍 1.use sql 2.shark 3.spark sql 4.终止shark 5.进程线 二:spark sql细节介绍 1.hive 与sparkSql比较(以后具体学习) 2.使用 ...

  5. win7下再装Ubuntu双系统

    一.UltraISO制作U盘启动盘 1.1打开 UltraISO,单机“文件”,选择“打开”. 1.2然后单击“启动”,选择“写入硬盘映像”. 二.装Ubuntu 前面省略,直接到安装类型(我的安装好 ...

  6. [代码审计]eyoucms前台未授权任意文件上传

    0x00 背景 来公司差不多一年了,然而我却依旧没有转正.约莫着转正也要到九月了,去年九月来的,实习,转正用了一年.2333 废话不多说了,最近有其他的事要忙,很久没有代码审计了.难的挖不了,浅的没意 ...

  7. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem B. Travelling Camera Problem set贪心

    Problem B. Travelling Camera Problem 题目连接: http://www.codeforces.com/gym/100253 Description Programm ...

  8. [C# 基础知识系列]专题八: 深入理解泛型(二)

    引言: 本专题主要是承接上一个专题要继续介绍泛型的其他内容,这里就不多说了,就直接进入本专题的内容的. 一.类型推断 在我们写泛型代码的时候经常有大量的"<"和"& ...

  9. kernel logo到开机动画之间闪现黑屏(android 5.X)

    在BootAnimation開始画图之前,会先做一次clear screen的动作,避免出现前面的图干扰到BootAnimation的显示. 通过check main_log先确认播放开机动画是哪个f ...

  10. Cocos2d-x3.0下实现循环列表

    本文的实现是參照我之前在做iOS时实现的一个能够循环的列表这里用C++重写一遍. 效果: 原文地址:http://blog.csdn.net/qqmcy/article/details/2739301 ...