<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="12dp" /> <solid android:color="#CC000000"/> </shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_back"
android:gravity="center_vertical"
android:padding="13dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/toast_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/toast_y"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#FFFFFF"
android:gravity="center"
android:textSize="17sp" />
</LinearLayout>
public class BToast extends Toast {
/**
* Toast单例
*/
private static BToast toast;
/**
* 构造
*
* @param context
*/
public BToast(Context context) {
super(context);
}
}
/**
* 隐藏当前Toast
*/
public static void cancelToast() {
if (toast != null) {
toast.cancel();
}
}
public void cancel() {
try {
super.cancel();
} catch (Exception e) {
}
}
@Override
public void show() {
try {
super.show();
} catch (Exception e) {
}
}
/**
* 初始化Toast
*
* @param context 上下文
* @param text 显示的文本
*/
private static void initToast(Context context, CharSequence text) {
try {
cancelToast();
toast = new BToast(context);
// 获取LayoutInflater对象
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 由layout文件创建一个View对象
View layout = inflater.inflate(R.layout.toast_layout, null);
// 吐司上的图片
toast_img = (ImageView) layout.findViewById(R.id.toast_img);
// 吐司上的文字
TextView toast_text = (TextView) layout.findViewById(R.id.toast_text);
toast_text.setText(text);
toast.setView(layout);
toast.setGravity(Gravity.CENTER, 0, 70);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 图标状态 不显示图标
*/
private static final int TYPE_HIDE = -1;
/**
* 图标状态 显示√
*/
private static final int TYPE_TRUE = 0;
/**
* 图标状态 显示×
*/
private static final int TYPE_FALSE = 1;
/**
* 显示Toast
*
* @param context 上下文
* @param text 显示的文本
* @param time 显示时长
* @param imgType 图标状态
*/
private static void showToast(Context context, CharSequence text, int time, int imgType) {
// 初始化一个新的Toast对象
initToast(context, text);
// 设置显示时长
if (time == Toast.LENGTH_LONG) {
toast.setDuration(Toast.LENGTH_LONG);
} else {
toast.setDuration(Toast.LENGTH_SHORT);
}
// 判断图标是否该显示,显示√还是×
if (imgType == TYPE_HIDE) {
toast_img.setVisibility(View.GONE);
} else {
if (imgType == TYPE_TRUE) {
toast_img.setBackgroundResource(R.drawable.toast_y);
} else {
toast_img.setBackgroundResource(R.drawable.toast_n);
}
toast_img.setVisibility(View.VISIBLE);
// 动画
ObjectAnimator.ofFloat(toast_img, "rotationY", 0, 360).setDuration(1700).start();
}
// 显示Toast
toast.show();
}
/**
* 显示一个纯文本吐司
*
* @param context 上下文
* @param text 显示的文本
*/
public static void showText(Context context, CharSequence text) {
showToast(context, text, Toast.LENGTH_SHORT, TYPE_HIDE);
}
/**
* 显示一个带图标的吐司
*
* @param context 上下文
* @param text 显示的文本
* @param isSucceed 显示【对号图标】还是【叉号图标】
*/
public static void showText(Context context, CharSequence text, boolean isSucceed) {
showToast(context, text, Toast.LENGTH_SHORT, isSucceed ? TYPE_TRUE : TYPE_FALSE);
}
/**
* 显示一个纯文本吐司
*
* @param context 上下文
* @param text 显示的文本
* @param time 持续的时间
*/
public static void showText(Context context, CharSequence text, int time) {
showToast(context, text, time, TYPE_HIDE);
}
/**
* 显示一个带图标的吐司
*
* @param context 上下文
* @param text 显示的文本
* @param time 持续的时间
* @param isSucceed 显示【对号图标】还是【叉号图标】
*/
public static void showText(Context context, CharSequence text, int time, boolean isSucceed) {
showToast(context, text, time, isSucceed ? TYPE_TRUE : TYPE_FALSE);
}
public void click(View view) {
switch (view.getId()) {
case R.id.btn_text:
BToast.showText(this, "简单提示");
break;
case R.id.btn_text_true:
BToast.showText(this, "简单提示 正确图标", true);
break;
case R.id.btn_text_false:
BToast.showText(this, "简单提示 错误图标", false);
break;
case R.id.btn_text_long:
BToast.showText(this, "简单提示 长~ ", Toast.LENGTH_LONG);
break;
case R.id.btn_text_true_long:
BToast.showText(this, "简单提示 正确图标 长~ ", Toast.LENGTH_LONG, true);
break;
case R.id.btn_text_false_long:
BToast.showText(this, "简单提示 错误图标 长~ ", Toast.LENGTH_LONG, false);
break;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有