简化Android SDK中常用方法调用:KAndroid
jopen
10年前
Kotlin这个Android库提供一些实用的扩展来简化Android SDK中常用方法调用,专注于提高开发效率。
Download
Download latest version with Gradle:
repositories { jcenter() } dependencies { compile 'com.pawegio.kandroid:kandroid:0.1.7@aar' }
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")