Tag: kotlin android extensions

将MainActivity与操作栏/工具栏和浮动按钮转换为Anko

我正在学习如何使用Kotlin / Anko。 我已经通过这里的例子,也克隆了模板项目,可以理解如何做一些基本的东西,但作为一个练习,我想转换这个简单的活动(从Android Studio中的空白活动生成并转换为Kotlin)使用安科也是如此。 Anko没有太多的例子,大多数只是上面引用的github页面的副本。 有人可以演示如何去将以下内容转换为Anko DSL? MainActivity.kt import android.os.Bundle import android.support.design.widget.FloatingActionButton import android.support.design.widget.Snackbar import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.Menu import android.view.MenuItem class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val toolBar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolBar) val fab = findViewById(R.id.fab) as FloatingActionButton fab.setOnClickListener { view -> Snackbar.make(view, "Replace this […]

什么id名称约定是好的Kotlin机器人扩展

使用Kotin的Android扩展我可以避免使用findViewById ,但林不知道如何命名ID来使用它的属性。 我发现两个选项是: 使用简单的ID名称, 但如果我使用碎片,可能会遇到意式咖啡的麻烦 : android.support.test.espresso.AmbiguousViewMatcherException:'与id:… / mainLayout'匹配层次结构中的多个视图。 这是因为我有一个TabLayout里面有两个相同ID的片段: <LinearLayout android:id="@+id/mainLayout" 与所有者姓名: "@+id/loginMainLayout"和"@+id/signUpMainLayout" 但是,我将不得不使用像signUpMainLayout.doSomething()这样的变量。 注意:在这种情况下,我不喜欢使用_ ,因为这不是一个好的代码风格 。 还有什么其他的选择?

如何使用ViewStub正确使用Kotlin Android扩展?

这些扩展是否有一个魔术来调用膨胀视图? 据我所知,我应该打破代码的和谐,并调用findViewById 意图是在某个时候膨胀layout_ongoingView布局,并根据场景再次隐藏和可见 <ViewStub android:id="@+id/viewStubOngoing" android:layout_width="match_parent" android:layout_height="wrap_content" android:inflatedId="@+id/ongoingView" android:layout="@layout/layout_ongoingView" /> 和代码 override fun hideOngoingPanel() { if (viewStubOngoing !is ViewStub) { findViewById(R.id.ongoingView).visibility = View.GONE } } override fun showOngoingPanel() { if (viewStubOngoing !is ViewStub) { findViewById(R.id.ongoingView).visibility = View.VISIBLE } else { viewStubOngoing.inflate() } }

Android gradle插件抱怨Uncaught翻译错误

我正在更新我的kotlin版本到1.0.0-beta-2423,我所做的就是更新相关依赖关系的版本号。 但是,当我运行assembleDebug任务,它抱怨这个错误。 :app:transformClassesWithDexForDebug Uncaught translation error: com.android.dx.cf.code.SimException: expected type java.lang.Object but found int Uncaught translation error: com.android.dx.cf.code.SimException: expected type java.lang.Object but found int 2 errors; aborting Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 我列出的所有依赖: apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { compileSdkVersion 23 […]

arrayListOf和mutableListOf有什么区别,哪一个更好?

我正在尝试在Kotlin中使用集合,并在arrayListOf和mutableListOf之间感到困惑,应该使用哪一个?为什么?

将textView与Kotlin中的字符串进行比较

我试图改变我的Android应用程序的文本视图每次我按下按钮 问题在于“if”操作不适用,就好像它总是返回false 这是为什么 ? override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val lol: String = "Hello World!" button.setOnClickListener{ if ( textview.equals(lol)){ textview.setText("yeah")} else textview.setText("Hello World!") }}}

接口中的属性不能有后台字段

我正在学习Kotlin 。 我的代码如下: interface BaseLogicDecoupler<A : BaseViewNotifier, B : BaseScreenRouter> { var notifier: A? var router: B? fun attachNotifier(notifier: A?) { this.notifier = notifier } fun detachNotifier() { notifier = null; } fun attachRouter(router: B?) { this.router = router } fun detachRouter() { router = null; } } 但是,当我改变它,并尝试提供像以下属性的访问器: var notifier: A? get() = notifier […]

字符串资源提取kotlin在android studio?

所以我在Android Studio(3.0 Beta 6)中看到的唯一一件事情是,当我输入时,这是一个非常无用的“调整代码样式设置”选项。 这非常不方便。 那里应该是那个地方的特色,因为这个问题在这里已经被标记为“固定”了,而且如果我“帮助 – >找到行动”,那么我真的找到了一个意图 但是这没有任何作用。 我如何得到这个工作?

Android Studio / Kotlin – 无效错误“覆盖方法应该调用超级”

我有一个由一个ContainerActivity类扩展的BaseActivity类。 因为我想在其他几个项目中使用BaseActivity ,所以我将它移动到一个Android库中,并作为Gradle依赖项添加到这些其他项目中。 除了移动Base类之外,在扩展它的类中没有任何代码改变。 然而,他们现在都有覆盖方法的错误,超级方法用@CallSuper注释。 这个子类仍然在调用超级方法。 我也可以编译和运行没有问题的项目。 这是错误消息的图像。 这里是一个图像,你可以看到我实际上是调用超级方法。 (从onCreate中删除了额外的代码) 这是BaseActivity 有没有人找到一个解决方案,让IDE认识到这不是一个错误? 更新: 我添加了一个应用程序模块到我的图书馆项目。 他们也有同样的错误。 看起来像包含一个库的任何东西,如果从@CallSuper注释的库中扩展一个方法,就会显示错误

Int和Int类型参数的标识符相等已被弃用

只是fyi,这是我在StackOverflow上的第一个问题,在Kotlin我真的很新。 在完全完成Kotlin(ver 1.1.3-2)的项目时,我在下面的代码中看到了一个警告(对于你好奇的小伙子的评论): // Code below is to handle presses of Volume up or Volume down. // Without this, after pressing volume buttons, the navigation bar will // show up and won't hide val decorView = window.decorView decorView .setOnSystemUiVisibilityChangeListener { visibility -> if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN === 0) { decorView.systemUiVisibility = flags } } 警告是针对可见性和View.SYSTEM_UI_FLAG_FULLSCREEN […]