自定义Toat及其弹出时间控制

效果就是实现Toast布局自定义,弹出的事件进行控制

一、先自定义布局

1
2
3
4
5
6
7
Toast toast = Toast.makeText(mContext, str, Toast.   LENGTH_SHORT);
View view = LayoutInflater.from(mContext).inflate(R.layout.item_toast, null);
TextView textView = view.findViewById(R.id.str_toast);
textView.setText(str);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
Utils.showTimeToast(toast,500);

二、自定义弹出时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Toast自定义时间
* Toast对象时间需要为Toast.LENGH_LONG
*/
public static void showTimeToast(final Toast toast, final int time) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0, 3000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, time);
}

就是那么简单,利用Timer和schedule实现事件监听,控制toast的弹出时间。