Android显示Toast,点击的多了,会很难看,所以,做一个有新的Toast要显示的话,就把旧的撤销掉。
可自定义Toast样式。
Toast工具类:
public class ToastUtil {
private static List<Toast> toastList = new ArrayList();
//显示文本的Toast
public static void showTextToast(Context context, String message){
cancelAll();
View toastView= LayoutInflater.from(context).inflate(R.layout.toast_text_layout,null);
TextView text = (TextView) toastView.findViewById(R.id.tv_message);
text.setText(message);
Toast toast=new Toast(context);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastView);
toastList.add(toast);
toast.show();
}
public static void cancelAll() {
if (!toastList.isEmpty()){
for (Toast toast : toastList) {
toast.cancel();
}
toastList.clear();
}
}
}
思想就是:把老的加入到一个list里边,每有新的要显示,就把旧的先撤销,在显示新的。
toast_text_layout如下:
<?xml version="1.0" encoding="utf-8"?>
<Layout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/toast_background"
android:gravity="center">
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="Toast"
android:paddingTop="30px"
android:paddingLeft="30px"
android:paddingRight="30px"
android:paddingBottom="30px"
android:textSize="40px"
/>
</LinearLayout>
背景toast_background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#80000000" />
<corners
android:topLeftRadius="8dip"
android:topRightRadius="8dip"
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"
>
</corners>
</shape>
调用的话:
ToastUtil.showTextToast(MainActivity.this,"弹出了一个吐司");
现在又有一个新的需求,就是设置吐司的时间,自定义吐司的时间,想法就是加一个定时器来控制显示和消失:
所以将自定义的Toast类改成了:
public class ToastUtil {
private static List<Toast> toastList = new ArrayList();
//显示文本的Toast
public static void showTextToast(Context context, String message,int num){
cancelAll();
View toastView= LayoutInflater.from(context).inflate(R.layout.toast_text_layout,null);
TextView text = (TextView) toastView.findViewById(R.id.tv_message);
text.setText(message);
Toast toast=new Toast(context);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastView);
toastList.add(toast);
//toast.show();
showMyToast(toast,num);
}
public static void cancelAll() {
if (!toastList.isEmpty()){
for (Toast toast : toastList) {
toast.cancel();
}
toastList.clear();
}
}
public static void showMyToast(final Toast toast, final int countNum) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, countNum);
}
}
这样就加了一个时间的限定来控制每一个吐司显示的时间,布局什么的也都没有变化,调用的时候,多传一个表示时间的参数就行了,例:
ToastUtil.showTextToast(context,bean.getMessage(),2500);
这条吐司的显示时间为2.5秒。