Android版Kotlin。 吐司
在Android的不同Kotlin示例中,我看到了toast(“ Some message ...”)或toastLong(“ Some long message”)。 例如:
view.setOnClickListener { toast("Click") }
据我了解,它是Activity的扩展功能。
如何定义此toast()函数以及在项目中的何处(在什么地方)可以使用它?
Andrey asked 2019-12-27T23:23:11Z
13个解决方案
86 votes
它可以是Context的扩展功能:
fun Context.toast(message: CharSequence) =
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
您可以将其放置在项目中的任何位置,完全由您决定。 例如,您可以定义一个文件Context并将其放在顶层作为函数。
只要有访问Context实例的权限,就可以导入并使用此函数:
import mypackage.util.ContextExtensions.toast
fun myFun(context: Context) {
context.toast("Hello world!")
}
nhaarman answered 2019-12-27T23:23:31Z
41 votes
这是Kotlin中的一线解决方案:
Toast.makeText(this@MainActivity, "Its toast!", Toast.LENGTH_SHORT).show()
Zeero0 answered 2019-12-27T23:23:51Z
41 votes
它实际上是Koko的扩展名Anko的一部分。 语法如下:
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such a duration")
在您的应用程序级build.gradle中,添加implementation "org.jetbrains.anko:anko-common:0.8.3"
将import org.jetbrains.anko.toast添加到您的活动中。
Muz answered 2019-12-27T23:24:20Z
3 votes
与Kotlin一起使用Anko时,内部片段使用以下任一方法:
activity.toast("string message")
要么
context.toast("string message")
or
view.holder.context.toast("string message")
vishnu benny answered 2019-12-27T23:25:44Z
3 votes
如果您不想使用anko,而是想创建自己的Toast扩展名。 您可以创建在kotlin文件中定义的内联(或不具有内联)扩展名(不带类),并且可以在任何类中访问此函数。
inline fun Context.toast(message:String){
Toast.makeText(this, message , duration).show()
}
用法:
在活动中,
toast("Toast Message")
在片段中
context?.toast("Toast Message")
Aziz answered 2019-12-27T23:26:17Z
3 votes
尝试这个
活动中
Toast.makeText(applicationContext, "Test", Toast.LENGTH_LONG).show()
要么
Toast.makeText(this@MainActiivty, "Test", Toast.LENGTH_LONG).show()
在片段中
Toast.makeText(activity, "Test", Toast.LENGTH_LONG).show()
要么
Toast.makeText(activity?.applicationContext, "Test", Toast.LENGTH_LONG).show()
bala answered 2019-12-27T23:26:54Z
2 votes
我发现从给定的链接吐司很简单的方法[https://gist.github.com/felipearimateia/ee651e2694c21de2c812063980b89ca3。]在此链接中,使用“活动”代替“上下文”。 尝试一下。
Khyati Fatania answered 2019-12-27T23:27:14Z
1 votes
它只是Context的扩展功能(就像已经指出的其他功能一样)。
您可以在Anko中找到许多预定义的android扩展功能,这可能也是许多教程使用的功能。
Lovis answered 2019-12-27T23:27:39Z
1 votes
只需添加@nhaarman的答案->您可能还想添加resourceId136支持
fun Context.toast(resourceId: Int) = toast(getString(resourceId))
fun Context.toast(message: CharSequence) =
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
Algar answered 2019-12-27T23:27:59Z
1 votes
使用此Toasts扩展功能,您可以在“活动”和“片段”中调用它们,可以将其作为“ Activities”传递为Context,对于“ Fragments”则传递为getApplication(),它也是默认值为Toast.LENGTH_SHORT生成的,因此您无需担心传递它 作为参数,但是如果需要,您也可以覆盖它。
科特林
fun Context.toast(context: Context = applicationContext, message: String, duration: Int = Toast.LENGTH_SHORT){
Toast.makeText(context, message , duration).show()
}
用法
toast(this, "John Doe")
如果要覆盖持续时间。
toast(this, "John Doe", Toast.LENGTH_LONG)
Gastón Saillén answered 2019-12-27T23:28:32Z
0 votes
从此处下载源代码(在Android Kotlin中自定义Toast)
fun Toast.createToast(context: Context, message: String, gravity: Int, duration: Int, backgroucolor: String, imagebackgroud: Int) {
val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
/*first parameter is the layout you made
second parameter is the root view in that xml
*/
val layout = inflater.inflate(R.layout.custom_toast, (context as Activity).findViewById(R.id.custom_toast_container))
layout.findViewById(R.id.tv_message).text = message
layout.setBackgroundColor(Color.parseColor(backgroucolor))
layout.findViewById(R.id.iv_image).iv_image.setImageResource(imagebackgroud)
setGravity(gravity, 0, 100)
setDuration(Toast.LENGTH_LONG);
setView(layout);
show()
}
谢谢!
Deepshikha Puri answered 2019-12-27T23:28:56Z
0 votes
我使用它的方式只是创建Object/Class
object UtilKotlin {
fun showMessage(context: Context, message: String) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
}
}
并调用方法
UtilKotlin.showMessage(activity, "Sets 0 !") // in activity trying this
Sam answered 2019-12-27T23:29:20Z
0 votes
具有背景色的自定义吐司文本大小,并且不会夸大XML文件在不设置背景色的情况下尝试代码
fun theTOAST(){
val toast = Toast.makeText(this@MainActivity, "Use View Person List",Toast.LENGTH_LONG)
val view = toast.view
view.setBackgroundColor(Color.TRANSPARENT)
val text = view.findViewById(android.R.id.message) as TextView
text.setTextColor(Color.BLUE)
text.textSize = (18F)
toast.show()
}
Grendel answered 2019-12-27T23:29:40Z