dependencies {
// Anko Commons
implementation "org.jetbrains.anko:anko-commons:$anko_version"
// Anko Layouts
implementation "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
// Coroutine listeners for Anko Layouts
implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
// Anko SQLite
implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
}
复制代码
val listItems = arrayOf("Russia", "USA", "Japan", "Australia") //传数组
val listDialog: AlertDialog.Builder = AlertDialog.Builder(this)
listDialog.setItems(listItems) { p0, p1 ->
toast(p1)
}
val dialog: AlertDialog = listDialog.create()
dialog.show()
val window: Window = dialog.window
val params: WindowManager.LayoutParams = window.attributes
params.y = 45 * ScreenUtils.getScreenDensity().toInt()
params.gravity = Gravity.TOP or Gravity.RIGHT
params.width = ScreenUtils.getScreenWidth() / 2
params.height = ViewGroup.LayoutParams.WRAP_CONTENT
window.attributes = params
复制代码
但是我们用Anko是这样的:
val countries = listOf("Russia", "USA", "Japan", "Australia") //传list
selector("Where are you from?", countries, { dialogInterface, i ->
toast("So you're living in ${countries[i]}, right?")
})
复制代码
看起来只是简化了dialog的创建过程。
2.2.5 Progress dialogs
不显示进度的 Loading Dialg
pressDialog("Please wait a minute.", "Downloading…")
indeterminateProgressDialog("Fetching the data…")
复制代码
2.3 Logging
打印log辅助工具。
Android SDK 提供 android.util.Log 类来提供一些 logging 方法,,这些方法都很实用,但是我们每次必须传递一个 Tag 参数,同时这个 Tag 信息必须是 String 类型的,这就略显麻烦。不过现在我们可以通过 AnkoLogger 类摆脱这些恼人的问题:
class SomeActivity : Activity(), AnkoLogger {
fun someMethod() {
info("Info message")
debug(42) // .toString() method will be called automatically
}
}
复制代码
默认的 Tag 名是当前的类名( 本例中的是SomeActivity),但是通过重写 AnkoLogger 的 loggerTag 属性我们是可以来更改的,而且每个方法有两个版本: plain and lazy (inlined)
class SomeActivity : Activity() {
private val log = AnkoLogger(this.javaClass)
private val logWithASpecificTag = AnkoLogger("my_tag")
private fun someMethod() {
log.warning("Big brother is watching you!")
}
}
复制代码
上面两种方法分别是不同Tag的实现方式。
AnkoLogger中loggerTag 属性具体对照如下:
2.4 Resources and dimensions
你可以在你的项目中使用Anko Resources and dimensions来简化你的代码,例如Color、Dimen等,颜色透明度直接色值.opaque就可以,尺寸的话直接使用dip(dipValue)、sp(spValue)就可以。在这里面还有一个就是applyRecursively()用来控制子View的操作,如:
class DatabaseHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "LibraryDatabase", null, 1) {
companion object {
private var instance: DatabaseHelper? = null
@Synchronized
fun Instance(context: Context): DatabaseHelper {
if (instance == null) {
instance = DatabaseHelper(context.applicationContext)
}
return instance!!
}
}
override fun onCreate(database: SQLiteDatabase) {
createTable(Book.TABLE_NAME, true, Book.COLUMN_ID to INTEGER + PRIMARY_KEY, Book.COLUMN_TITLE to TEXT, Book.COLUMN_AUTHOR to TEXT)
}
override fun onUpgrade(database: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
dropTable(Book.TABLE_NAME, true)
}
}
复制代码
访问数据库的推荐做法是通过依赖注入,或者为 Context 添加一个 extension:
val Context.database: DatabaseHelper
get() = DatabaseHelper.Instance(applicationContext)
复制代码
下面这是一个简单的 model 类:
data class Book(val id: Int, val title: String, val author: String) {
companion object {
val Book.COLUMN_ID = "id"
val TABLE_NAME = "books"
val COLUMN_TITLE = "title"
val COLUMN_AUTHOR = "author"
}
}
复制代码
当数据库准备好后,就可以通过 use 方法来进行操作了。比如:
database.use {
insert(Book.TABLE_NAME, Book.COLUMN_ID to 1, Book.COLUMN_TITLE to "2666", Book.COLUMN_AUTHOR to "Roberto Bolano")
}
复制代码
doAsync(UI) {
val data: Deferred<Data> = bg {
// Runs in background
getData()
}
// This code is executed on the UI thread
showData(data.await())
}
复制代码
Martin Kleppmann / O'Reilly Media / 2017-4-2 / USD 44.99
Data is at the center of many challenges in system design today. Difficult issues need to be figured out, such as scalability, consistency, reliability, efficiency, and maintainability. In addition, w......一起来看看 《Designing Data-Intensive Applications》 这本书的介绍吧!