如何去自定义一个Toast?
来源:互联网 发布:java工程师是干什么的 编辑:程序博客网 时间:2024/05/30 02:24
考虑如下:
一、视图,
二、需要调用的方法
package com.bw.aday05toast;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
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 ToastUtil(Context context, int layoutId, String msg) {
message = msg;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//自定义布局
View view = inflater.inflate(layoutId, null);
//自定义toast文本
mTextView = (TextView)view.findViewById(R.id.toast_msg);
mTextView.setText(msg);
Log.i("ToastUtil", "Toast start...");
if (mToast == null) {
mToast = new Toast(context);
Log.i("ToastUtil", "Toast create...");
}
//设置toast居中显示
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.setDuration(Toast.LENGTH_LONG);
mToast.setView(view);
}
/**
* 自定义居中显示toast
*/
public void show() {
mToast.show();
Log.i("ToastUtil", "Toast show...");
}
/**
* 自定义时长、居中显示toast
* @param duration
*/
public void show(int duration) {
timeCount = new TimeCount(duration, 1000);
Log.i("ToastUtil", "Toast show...");
if (canceled) {
timeCount.start();
canceled = false;
showUntilCancel();
}
}
/**
* 隐藏toast
*/
private void hide() {
if (mToast != null) {
mToast.cancel();
}
canceled = true;
Log.i("ToastUtil", "Toast that customed duration hide...");
}
private void showUntilCancel() {
if (canceled) { //如果已经取消显示,就直接return
return;
}
mToast.show();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.i("ToastUtil", "Toast showUntilCancel...");
showUntilCancel();
}
}, Toast.LENGTH_LONG);
}
/**
* 自定义计时器
*/
private class TimeCount extends CountDownTimer {
public TimeCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval); //millisInFuture总计时长,countDownInterval时间间隔(一般为1000ms)
}
@Override
public void onTick(long millisUntilFinished) {
mTextView.setText(message + ": " + millisUntilFinished / 1000 + "s后消失");
}
@Override
public void onFinish() {
hide();
}
}
}
- 如何去自定义一个Toast?
- 自定义一个Toast(技巧)
- 自定义一个Toast(技巧)
- 自定义一个Toast(技巧)
- 如何在Android自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- 自定义toast
- 自定义toast
- 自定义Toast
- 自定义toast
- 自定义Toast
- 自定义Toast
- 自定义Toast
- Toast自定义
- 自定义Toast
- 筛选冗余的图片资源
- C语言函数调用约定
- 怎么让Chrome支持小于12px 的文字?
- SpringBoot在自定义类中调用service层等Spring其他层
- 无缝滚动小练习—animation
- 如何去自定义一个Toast?
- angularjs 图片上传 回显
- Android 使用Scheme实现从网页启动APP
- [leetcode]617. Merge Two Binary Trees
- Groovy 特殊语法
- 两个栈实现一个队列功能
- Android程序真退出与假退出后台运行详解
- 获取屏幕宽高的四种方法
- Groovy 属性访问