Tag: kotlin

在Kotlin的匿名类与lambdas

我正在尝试将我的闲暇时间项目从Java重新编写到Kotlin(了解它),并遇到了一些问题。 研究把我带到{ function() }但它没有帮助我 在Java中我有这个接口: public interface Shuffling<T> { List<T> shuffle(List<T> list, ShuffleCallback callback); interface ShuffleCallback { void onShuffle(int addedTo, int randomIndex); } } 我试图在Kotlin中添加测试对象到洗牌算法列表中: val algoList = ArrayList<Shuffling<Int>>() algoList.add(Shuffling { list, callback -> { Timber.d("Test!") ArrayList<Int>() // how to return this value? }}) 如何添加多行到lambda函数? 另外我还有一个麻烦的例子: Kotlin界面: interface Drawable { fun draw() } 和Kotlin实现: private […]

Kotlin弹簧安全配置

从1.0.0-beta-242升级到Kotlin 1.0.0-beta-3595后,以下代码不能编译: @Throws(Exception::class) override fun configure(http: HttpSecurity) { http.addFilterBefore(AuthenticationFilter(authenticationManager()), BasicAuthenticationFilter::class.java) http.csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().authorizeRequests() .antMatchers("/authorization/**", "/public/**").permitAll() .antMatchers("/**").authenticated() } 返回的错误是: SecurityAssembly.kt: (48, 65): Unresolved reference: permitAll 编辑: 来自流行的Spring Security框架的permitAll方法的签名是: public ExpressionInterceptUrlRegistry permitAll() { return access(permitAll); } 我错过了什么或者这是一个错误?

Android Kotlin .visibility

我有这样的代码,应该使图像可见,但我不知道它应该如何写入Kotlin。 我正在尝试在Kotlin中使用.visibility ,我不知道该怎么给它一个价值。 它基于setVisibility() 。 码: fun hacerVisibleLaFoto(v: View) { imageView.visibility = 1; } 我把1放在值域中,因为在那里需要一个整数值,这是我的占位符值,直到我找到真正的值。 =号后面应该显示的值是什么?

Kotlin KDoc:文档?

我在 这里看到, 在这里和那里引用了KDoc,相当于Kotlin的JavaDoc实用程序。 但是,我找不到有关如何使用它的任何文档,更不用说如何定制它或将其集成到Maven或Gradle中。 我知道Kotlin API文档是使用KDoc生成的,因为页面源代码有以下的HTML注释: <!– Generated by kdoc on Sun Jul 06 20:27:33 UTC 2014 –> 那么,有没有关于如何使用KDoc的文档呢?

混合Kotlin + Java与Maven,未解决的参考

我有一个Maven项目与Kotlin代码hello.kt调用Java代码JavaFoo.java调用Kotlin代码KotlinFoo.kt 。 hello.kt也直接调用KotlinFoo.kt 。 我试图用mvn clean install使用kotlinlang的Maven文档中描述的Maven设置来构建它。 如果hello.kt不调用JavaFoo (但是我把JavaFoo留在了项目中),那么这样做就好了。 文档中说,应该在Java编译器之前调用Kotlin编译器,这意味着所有的Kotlin代码都需要在任何Java代码之前编译,即使用这种设置,您可以从Java调用Kotlin,反之则不然。 但是,文档将这种设置描述为“混合代码应用程序”,而不是“从Java调用Kotlin”。 换句话说,这种失败似乎与文件似乎暗示的一致,但与他们直接说的不一致 – 或者我只是误解了一些东西。 我想从另一个语言中调用每种语言。 有没有一个Maven的配置,将这样做,请? (我查看了混合代码设置的各种StackExchange问​​题,而且这些解决方案都不适用于我。) 按要求添加代码:pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.kotlindemo</groupId> <artifactId>kotlin-demo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>kotlin-demo</name> <properties> <kotlin.version>1.1.2-2</kotlin.version> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> <main.class>com.example.kotlindemo.HelloKt</main.class> </properties> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <executions> <execution> <id>compile</id> <phase>process-sources</phase> <goals> <goal>compile</goal> </goals> <configuration> […]

在Kotlin嵌套lambda调用

在Kotlin嵌套lambda调用时,我怎样才能明确地引用孩子和父母的元素? 例如: data class Course(var weekday: Int, var time: Int, var duration: Int) var list1 = mutableListOf<Course>() var list2 = mutableListOf<Course>() // populate list1 and list2 // exclude any element from list1 if it has the same time and weekday as any element from list2 list1.filter { list2.none{it.weekday == it.weekday && it.time == it.time} }

查询Android的联系人在Kotlin收购

我在尝试在Kotlin编写的Android应用程序中查询ContactsContract时遇到了一些麻烦。 Android工作室从未解析的引用(例如ContactsContract.Contacts._ID)中提供错误。 有谁知道正确的方式来查询这些Kotlin?

Kotlin null来自Java

当我将Java代码从一个库转换为Kotlin时,我会看到Java代码中是否存在空值检查,并想知道如何最好地将其转换。 我仍然需要在Kotlin的空检查,因为我不能控制,如果Java将使对象为空? 比方说,我有一个CameraDevice可以从Java返回null 我应该如何定义这个 private lateinit var mCameraDevice: CameraDevice 然后不需要空检查或像这样.. private var mCameraDevice: CameraDevice? = null 然后保持空的检查 if (mCameraDevice != null) { // do something } Kotlin的可玩性让我感到困惑,因为我觉得我不应该处理null,并且应该使用选项1,但是基本上我使用的每个库都是Java,所以我必须处理空值,并使用选项2

Kotlin相当于三元运算符

所以在java中,我们有三元运算符(?),它有时很容易通过if-else inlines来计算一些值。 例如: myAdapter.setAdapterItems( textToSearch.length == 0 ? noteList : noteList.sublist(0, length-5) ) 我知道kotlin的等价物是: myAdapter.setAdapterItems( if(textToSearch.length == 0) noteList else noteList.sublist(0, length-5) ) 但是我只是喜欢Java中的三元运算符,用于简短的表达条件以及将值传递给方法。 有没有Kotlin等价物?

Kotlin自定义属性数据绑定

我想在我的Kotlin项目中使用Android DataBinding库设置自定义属性,如下所示: 布局 <ImageView android:id="@+id/imgView” android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center" android:adjustViewBounds="true" app:imageUrl="@{segment.url}"/> 码 class Utils { companion object { @BindingAdapter("bind:imageUrl") @JvmStatic fun loadImage(view: ImageView, url:String) {Picasso.with(view.context).load(url).error(R.drawable.error).into(view)} } 我得到的运行时错误是: in中的BindingAdapter不是静态的,需要使用从DataBindingComponent中检索的对象。 如果您不使用采用DataBindingComponent的通胀方法,请使用DataBindingUtil.setDefaultComponent或使所有BindingAdapter方法都是静态的。 任何指针来解决它? 这只发生在自定义属性上。 其余的数据绑定工作正常