diff --git a/app/src/main/java/com/example/defenseapplication/view/ToastUtils.kt b/app/src/main/java/com/example/defenseapplication/view/ToastUtils.kt new file mode 100644 index 0000000..0155d7e --- /dev/null +++ b/app/src/main/java/com/example/defenseapplication/view/ToastUtils.kt @@ -0,0 +1,59 @@ +import android.content.Context +import android.view.Gravity +import android.view.LayoutInflater +import android.widget.ImageView +import android.widget.TextView +import android.widget.Toast +import com.example.defenseapplication.R + +object ToastUtils { + + private var currentToast: Toast? = null + + /** + * 显示成功提示 + * @param message 提示文字 + * @param duration 时长 默认Toast.LENGTH_SHORT + */ + fun showSuccess(context: Context, message: String, duration: Int = Toast.LENGTH_SHORT) { + showCustomToast(context, message, true, duration) + } + + /** + * 显示失败提示 + * @param message 提示文字 + * @param duration 时长 默认Toast.LENGTH_SHORT + */ + fun showError(context: Context, message: String, duration: Int = Toast.LENGTH_SHORT) { + showCustomToast(context, message, false, duration) + } + + private fun showCustomToast(context: Context, message: String, isSuccess: Boolean, duration: Int) { + // 先取消上一个Toast,避免重复弹出 + currentToast?.cancel() + + // 加载自定义布局 + val view = LayoutInflater.from(context).inflate(R.layout.layout_custom_toast, null) + val ivIcon = view.findViewById(R.id.iv_icon) + val tvMessage = view.findViewById(R.id.tv_message) + + // 设置图标和文字 + if (isSuccess) { + ivIcon.setImageResource(R.drawable.fill) // 成功图标 + } else { + ivIcon.setImageResource(R.drawable.error_circle) // 失败图标 + } + tvMessage.text = message + + // 创建Toast + val toast = Toast(context.applicationContext).apply { + setGravity(Gravity.CENTER, 0, 0) // 居中显示,和你图里一致 + this.duration = duration + this.view = view + } + + // 保存当前Toast并显示 + currentToast = toast + toast.show() + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_toast.xml b/app/src/main/res/drawable/bg_toast.xml new file mode 100644 index 0000000..62c51d3 --- /dev/null +++ b/app/src/main/res/drawable/bg_toast.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/check_ellipse.png b/app/src/main/res/drawable/check_ellipse.png new file mode 100644 index 0000000..7558b7d Binary files /dev/null and b/app/src/main/res/drawable/check_ellipse.png differ diff --git a/app/src/main/res/drawable/close.png b/app/src/main/res/drawable/close.png new file mode 100644 index 0000000..a69b823 Binary files /dev/null and b/app/src/main/res/drawable/close.png differ diff --git a/app/src/main/res/drawable/ellipse.png b/app/src/main/res/drawable/ellipse.png new file mode 100644 index 0000000..14bb866 Binary files /dev/null and b/app/src/main/res/drawable/ellipse.png differ diff --git a/app/src/main/res/drawable/error_circle.png b/app/src/main/res/drawable/error_circle.png new file mode 100644 index 0000000..ebbe1e1 Binary files /dev/null and b/app/src/main/res/drawable/error_circle.png differ diff --git a/app/src/main/res/drawable/fill.png b/app/src/main/res/drawable/fill.png new file mode 100644 index 0000000..fe88a0d Binary files /dev/null and b/app/src/main/res/drawable/fill.png differ diff --git a/app/src/main/res/drawable/open.png b/app/src/main/res/drawable/open.png new file mode 100644 index 0000000..04bc36b Binary files /dev/null and b/app/src/main/res/drawable/open.png differ diff --git a/app/src/main/res/drawable/search.png b/app/src/main/res/drawable/search.png new file mode 100644 index 0000000..611cada Binary files /dev/null and b/app/src/main/res/drawable/search.png differ diff --git a/app/src/main/res/layout/layout_custom_toast.xml b/app/src/main/res/layout/layout_custom_toast.xml new file mode 100644 index 0000000..5ee0d52 --- /dev/null +++ b/app/src/main/res/layout/layout_custom_toast.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index f569136..f403e13 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -16,5 +16,6 @@ #03A9F4 #F7F7F7 #F3F3F3 + #111111 \ No newline at end of file