资源简介
Android自定义Toast样式和时间,在屏幕中间显示,
是使用方式:
ToastUtil.getInstance().show(str);

代码片段和文件信息
package com.petecat.jfree.utils;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;
import com.petecat.jfree.R;
import com.petecat.jfree.context.JfreeApplication;
public class ToastUtil {
private Toast mToast;
private TextView mTextView;
private TimeCount timeCount;
private String message;
private Handler mHandler = new Handler();
private boolean canceled = true;
public static ToastUtil getInstance() {
return ToastUtil.ToastUtils.sInstance;
}
// 静态内部类
private static class ToastUtils {
private static final ToastUtil sInstance = new ToastUtil();
}
public ToastUtil() {
}
public void init(int type) {
LayoutInflater inflater = (LayoutInflater) JfreeApplication.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//自定义布局
View view = null;
if (type == 0) {
view = inflater.inflate(R.layout.toast_center null);
} else {
view = inflater.inflate(R.layout.toast_center1 null);
}
//自定义toast文本
mTextView = (TextView) view.findViewById(R.id.toast_msg);
if (mToast == null) {
mToast = new Toast(JfreeApplication.getAppContext());
}
mToast.setGravity(Gravity.CENTER 0 0);
mToast.setDuration(Toast.LENGTH_LONG);
mToast.setView(view);
}
/**
* 自定义居中显示toast
*/
public void show(String text) {
hide();
if (mToast == null) {
init(0);
}
message = text;
mTextView.setText(text);
mToast.show();
}
/**
* 自定义时长、居中显示toast
*
* @param duration
*/
public void show(String text int duration) {
hide();
if (mToast == null) {
init(1);
}
message = text;
if (timeCount == null) {
timeCount = new TimeCount(duration 1000);
}
if (canceled) {
timeCount.start();
canceled = false;
showUntilCancel();
}
}
/**
* 隐藏toast
*/
public void hide() {
if (mToast != null) {
mToast.cancel();
mToast = null;
mTextView = null;
}
canceled = true;
if (timeCount != null) {
timeCount.cancel();
}
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
}
private void showUntilCancel() {
if (canceled) { //如果已经取消显示,就直接return
return;
}
mToast.show();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
showUntilCancel();
}
} 2000);
}
/**
* 自定义计时器
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3598 2018-12-23 23:35 ToastUtil.java
文件 1040 2018-12-18 18:57 toast_center.xm
相关资源
- Android非常漂亮的登录界面
- pc与android通过usb socket实现手机通信
- android毕业设计
- 百度地图自定义Markerandroid
- Android分区工具包
- android-support-v4.jar已打包进去源代码
- u-blox_Android_GNSS_Driver_v3.10驱动源码+中
- 个人根据Android移动开发案例详解手写
- android 视频播放器 项目和原码
- Android【动画】【特效】 17种动画特效
- 基于Android智能家居详细设计(经典)
- android通过JDBC连接Mysql数据库
- Android通讯录的源代码
- android 瀑布流Demo
- 指纹传感器FPC1080在android下的驱动
- delphi xe5 android 调用照相机摄像头拍照
- Android手机连连看游戏源码
- android-sdk-windows v2.3离线完整版
- android 底部弹出菜单(带透明背景)
- Android工程模式简介.rar
- Android蓝牙和Cors网络开发源码
- Android powermanger wakelock
- Android v7的一些jar包
- 最新android supportV7包
- android图片压缩工具类分享
- 单机搭建Android(解决Network is unreach
- Android上监听收到的短信(SMS)
- android电商app源码
- Android代码-多功能拨号盘源码.zip
- printershare直接破解版--11.5(适配andr
评论
共有 条评论