自定义轻提示
@@ -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()
|
||||
}
|
||||
}
|
||||
6
app/src/main/res/drawable/bg_toast.xml
Normal 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>
|
||||
BIN
app/src/main/res/drawable/check_ellipse.png
Normal file
|
After Width: | Height: | Size: 610 B |
BIN
app/src/main/res/drawable/close.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
app/src/main/res/drawable/ellipse.png
Normal file
|
After Width: | Height: | Size: 585 B |
BIN
app/src/main/res/drawable/error_circle.png
Normal file
|
After Width: | Height: | Size: 1.3 KiB |
BIN
app/src/main/res/drawable/fill.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
app/src/main/res/drawable/open.png
Normal file
|
After Width: | Height: | Size: 3.5 KiB |
BIN
app/src/main/res/drawable/search.png
Normal file
|
After Width: | Height: | Size: 449 B |
24
app/src/main/res/layout/layout_custom_toast.xml
Normal 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>
|
||||
@@ -16,5 +16,6 @@
|
||||
<color name="blue">#03A9F4</color>
|
||||
<color name="bg_gray">#F7F7F7</color>
|
||||
<color name="setting_gray">#F3F3F3</color>
|
||||
<color name="ic_blake">#111111</color>
|
||||
|
||||
</resources>
|
||||