如何在我的Kotlin类中创建一个匿名接口实现并使用它?
我怎么能有这样的Kotlin的Java代码? 甚至IDE也不会完全将其转换为Kotlin!
/** Defines callbacks for service binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { // We've bound to LocalService, cast the IBinder and get LocalService instance LocalBinder binder = (LocalBinder) service; mService = binder.getService(); mBound = true; } @Override public void onServiceDisconnected(ComponentName arg0) { mBound = false; } };
我尝试使用inner class
但是我不能像这样使用它:
@Override protected void onStart() { super.onStart(); // Bind to LocalService Intent intent = new Intent(this, LocalService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); }
你在这里创建一个匿名类。 在Kotlin中,这些是对象expression式 :
val connection = object: ServiceConnection() { override fun onServiceConnected(className: ComponentName, service: IBinder) { ... } override fun onServiceDisconnected(arg0: ComponentName) { ... } }
- Android网络配置
- Kotlin REPL与Android类unStub!编辑
- Android工作室插件ID:'kotlin-android-extensions'
- Android Studio上的Kotlin(非Android)项目的ClassNotFoundException错误
- TextView在设计预览中显示,但不在实际的Android应用程序中显示
- Android gradle构建在':app:mergeDebugResources'上突然失败,没有错误消息
- 有没有办法自动填充参数名称?
- Android Studio gradle花费太长的时间来构建
- Kotlin在Android Studio 3.0-alpha8中应用()扩展lint消息