天道酬勤,学无止境

anko

Is it possible to reuse a layout in Kotlin Anko

I read that the most benefit of using Anko is its reusability. But i could't find its exact example. Currently in the new Android layout system, the boiler plate is like below: DrawerLayout (with some setup) CoordinatorLayout (with some setup) AppBarLayout (with some setup) ToolBar <The Main Content> NavigationView (with header inflated) From the layout structure above, only <The Main Content> is varry. And in many cases those ceremonial setup duplicated almost in every activity. So here with Anko im thinking if there is a reusable solution about that issue. Im not expecting it will be

2021-05-31 13:17:20    分类:问答    android   android-layout   kotlin   anko

'receiver type mismatch' with Fragment and Anko toast

I am trying to use Jetbrains' Anko library to easily display an Android toast message in my app. Here is the relevant code snippet: val message : CharSequence = "Recycled: ${holder.taskEditText.text}" (tasksFragment as Fragment).toast(text = message) and the error: Error:(80, 45) Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public inline fun Fragment.toast(text: CharSequence): Unit defined in org.jetbrains.anko public inline fun Fragment.toast(textResource: Int): Unit defined in org.jetbrains.anko public fun Context.toast(text

2021-05-09 09:08:04    分类:问答    android   kotlin   anko

How to create Kotlin DSL - DSL syntax Kotlin

As with anko you can write callback functions like this: alert { title = "" message = "" yesButton { toast("Yes") } noButton { toast("No") } } How can I create a nested functions like that? I tried creating it like below but doesn't seem to be working. class Test { fun f1(function: () -> Unit) {} fun f2(function: () -> Unit) {} } Now, if I use this with extension function, fun Context.temp(function: Test.() -> Unit) { function.onSuccess() // doesn't work } Calling this from Activity: temp { onSuccess { toast("Hello") } } Doesn't work. I am still lacking some basic concepts here. Can anyone

2021-04-07 00:05:34    分类:问答    android   kotlin   kotlin-extension   anko