自定义轻提示

This commit is contained in:
2026-05-08 17:57:06 +08:00
parent 5ee50d3785
commit d1a72da001
11 changed files with 90 additions and 0 deletions

View File

@@ -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<ImageView>(R.id.iv_icon)
val tvMessage = view.findViewById<TextView>(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()
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color='@color/ic_blake'/>
<corners android:radius="12dp"/>
</shape>

Binary file not shown.

After

Width:  |  Height:  |  Size: 610 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 585 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 B

View File

@@ -0,0 +1,24 @@
<?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/bg_toast"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@null"/>
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:textColor="@android:color/white"
android:textSize="14sp"/>
</LinearLayout>

View File

@@ -16,5 +16,6 @@
<color name="blue">#03A9F4</color> <color name="blue">#03A9F4</color>
<color name="bg_gray">#F7F7F7</color> <color name="bg_gray">#F7F7F7</color>
<color name="setting_gray">#F3F3F3</color> <color name="setting_gray">#F3F3F3</color>
<color name="ic_blake">#111111</color>
</resources> </resources>