簡化Android SDK中常用方法調用:KAndroid
Download
Download latest version with Gradle:
repositories {
jcenter()
}
dependencies {
compile 'com.pawegio.kandroid:kandroid:0.1.7@aar'</span> }</pre></div>
Usage
Binding views // instead of findViewById(R.id.textView) as TextView
val textView = findView<TextView>(R.id.textView)
Accessing Activity methods from Fragment // instead of (getActivity() as SampleActivity).foo()
getActivity<SampleActivity>().foo()
Using system services // instead of getSystemService(Context.WINDOW_SERVICE) as WindowManager
getWindowManager()
// instead of getSystemService(Context.POWER_SERVICE) as PowerManager
getPowerManager()
// instead of getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
getNotificationManager()
// instead of getSystemService(Context.USER_SERVICE) as UserManager
getUserManager()
// etc.
Toast messages longToast("I'm long toast message!")
shortToast("Hi, I'm short one!")
Layout inflater // instead of LayoutInflater.from(context).inflate(R.layout.some_layout, null, false)
inflateLayout(R.layout.some_layout)
// or
inflateLayout(R.layout.some_layout, attachToRoot = true)
Using Intents // instead of Intent(this, javaClass<SampleActivity>())
val intent = IntentFor<SampleActivity>(this)
Logging
// using javaClass.getName() as a TAG
v("Verbose log message")
d("Debug log message")
i("Info log message")
w("Warn log message")
e("Error log message")
// or with custom TAG
v("CustomTag", "Verbose log message with custom tag")
Threading // instead of Thread(Runnable { /* long execution */ }).start()
runAsync {
// long execution
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!