Kotli N — 如何创建自定义 Toast

27 篇文章 0 订阅
20 篇文章 1 订阅

在这个例子中,我将解释如何用两种不同的方式 toast。第一种方法非常简单,而第二种方法则不那么简单,因为我们将使用lambda表达式。但首先你必须创建一个项目。创建一个新项目,完成后比较activity_main。xml,并确保您没有遗漏任何内容。您的activity_main。xml应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/id_btn_show_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_toast"
        android:layout_gravity="center"/>
</FrameLayout>

如果您的android:text=” @string /show_toast”显示错误,只需将其更改为 android:text=”Show Toast” 即可修复它。您还应该将您的 MainActivity.kt 与我的进行比较,并确保您没有遗漏任何内容。您的 MainActivity.kt 应该看起来像这样

class MainActivity : AppCompatActivity() {
    private lateinit var id_btn_show_toast: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        id_btn_show_toast = findViewById(R.id.id_btn_show_toast)
        clickButtonListener()
    }

    private fun clickButtonListener() {
        id_btn_show_toast.setOnClickListener{
            Tool.showToast(this, "This is a toast message", Gravity.TOP)
        }
    }
}

如果您的“工具”显示错误,请不要担心,我们会修复它。在我们这样做之前,我们将创建一个名为 toast_popup 的布局。创建一个名为 toast_popup.xml 的布局,它应该看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:id="@+id/id_ll_toast_layout">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/id_tv_toast_message"
        android:text="@string/app_name"
        android:gravity="center_vertical"
        app:drawableStartCompat="@drawable/ic_toast" />
</LinearLayout>

我添加了一个 ic_toast 可绘制对象,它是 TextView 的图像。这只有在您的 TextView 位于 LinearLayout 内时才有效。LinearLayout 和 TextView 有一个 id,所以我们可以在 toast 中使用它们。

现在修复我们之前遇到的工具错误。创建一个名为 Tool 的对象类。为此,请创建一个 kotlin 类并设置为像这样的对象类

object Tool {

    fun showToast(activity: Activity, message: String, gravity: Int = Gravity.BOTTOM){
        // Get the toast layout
        val layout = LayoutInflater.from(activity)
            .inflate(R.layout.toast_popup, activity.findViewById(R.id.id_ll_toast_layout))
        // Initialize the textview to show message
        val id_tv_toast_message: TextView = layout.findViewById(R.id.id_tv_toast_message)
        // Add the message to the textview
        id_tv_toast_message.text = message
        // Configure the toast to show the custom toast
        Toast(activity).apply {
            this.view = layout
            this.duration = Toast.LENGTH_SHORT
            this.setGravity(gravity,0,40)
        }.show()
    }

    fun advanceShowToast(activity: Activity, message: String, gravity: Int = Gravity.BOTTOM, function: ((TextView) -> Unit)?){
        // Get the toast layout
        val layout = LayoutInflater.from(activity).inflate(R.layout.toast_popup, activity.findViewById(R.id.id_ll_toast_layout))
        // Initialize the text view to show message
        val id_tv_toast_message: TextView = layout.findViewById(R.id.id_tv_toast_message)
        // Add the message to the textview
        id_tv_toast_message.text = message
        // Lambda function to manipulate the textview
        function!!(id_tv_toast_message)
        // Configure the toast to show the custom toast
        Toast(activity).apply {
            this.view = layout
            this.duration = Toast.LENGTH_SHORT
            this.setGravity(gravity,0,40)
        }.show()
    }
}

第一种方法 Tool 对象类中的第一个函数 showToast() 有一个名为“activity”的括号,它将保存我们的 toast 将使用的当前活动。message 保存了将要显示的消息,而重力使 toast 显示在我们希望它显示的位置。默认设置为底部。

然后我们创建了一个 LayoutInflater 来显示 toast。我们使用 Activity、我们创建的 toast_popup.xml 布局文件和 toast_popup 内的 LinearLayout 视图创建 LayoutInflater。您也可以在不使用这样的 LinearLayout 的情况下膨胀布局,‘inflate(R.layout.toast_popup, null)’ 但 android studio 会抱怨。

然后我们初始化我们在 toast_popup.xml 里面创建的 TextView 并将括号中的消息设置为它,最后我们创建了一个 toast 实例。你可以这样做

Toast(activity).apply {
    this.view = layout
    this.duration = Toast.LENGTH_SHORT
    this.setGravity(gravity,0,40)
}.show()

或者像这样

val toast = Toast(activity)
toast.view = layout
toast.duration = Toast.LENGTH_SHORT
toast.setGravity(gravity,0,40)
toast.show()

任何一种方式都有效… 第二种方法 第二种方法就像第一种方法一样,但是由于它的 lambda 函数,它给了我们更多的定制能力。第二个名为 AdvanceShowToast() 的函数有一个 lambda 作为其括号的一部分 我们创建了一个期望 null 作为值的 lambda 函数,这就是它看起来很有趣的原因,这让我们可以选择使用或不使用 lambda 函数,并且我们在 AdvanceShowToast() 中使用 lambda 的方式是将其断言为 null,您可以这样做

function!!(id_tv_toast_message)

或者像这样

if (function != null) {
    function(id_tv_toast_message)
}

或者像这样

function?.let { it(id_tv_toast_message) }

使用 showToast 方法你可以像这样在 MainActivity 中使用 showToast 函数

Tool.showToast(this, "This is a toast message", Gravity.BOTTOM)

或者

Tool.showToast(this, "This is a toast message")

我们没有添加重力,因为重力有一个默认值,所以可以忽略使用 AdvanceShowToast 方法您可以像这样在 MainActivity 中使用 AdvanceShowToast 函数

Tool.advanceShowToast(this, "This is a toast message", Gravity.BOTTOM, null)

或者

Tool.advanceShowToast(this, "This is a toast message", function = null)
Tool.advanceShowToast(this, "This is a toast message", Gravity.CENTER){ textview ->
    textview.setBackgroundColor(Color.YELLOW)
    textview.setTextColor(Color.BLACK)
}

最后像这样

Tool.advanceShowToast(this, "This is a toast message"){ textview ->
    textview.setBackgroundColor(Color.YELLOW)
    textview.setTextColor(Color.BLACK)
}

我们名为 function 的 lambda 函数设置为 null,因为我们不想使用它。我们的重力已经有一个默认值,所以我们可以忽略它的声明

最后如果你在一个片段中,你可以使用这样的功能

Tool.showToast(requireActivity(), "This is a toast message", Gravity.TOP)
Tool.showToast(requireActivity(), "This is a toast message")

或者

Tool.advanceShowToast(requireActivity(), "This is a toast message", Gravity.TOP, null)

或者

Tool.advanceShowToast(requireActivity(), "This is a toast message", function = null)
Tool.advanceShowToast(requireActivity(), "This is a toast message", Gravity.CENTER){ textview ->
    textview.setBackgroundColor(Color.YELLOW)
    textview.setTextColor(Color.BLACK)
}

或者

Tool.advanceShowToast(requireActivity(), "This is a toast message"){ textview ->
    textview.setBackgroundColor(Color.YELLOW)
    textview.setTextColor(Color.BLACK)
}

最后

本文在本人文档中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

目前已经更新的部分资料,需要的点击文末卡片即可免费领取:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值