标签: kotlin-extension

Android扩展中的实验功能适用于Production版本

我正在使用@ParcelizeKotlin语言在Android开发中使用该功能.

要使用它们,我在build.gradle文件中进行了以下修改.

apply plugin: 'kotlin-android-extensions'
Run Code Online (Sandbox Code Playgroud)

然后

androidExtensions {
    experimental = true
}
Run Code Online (Sandbox Code Playgroud)

我成功使用了上述功能.但是,是否建议将它们用于生产发布或仅用于开发目的?

android kotlin kotlin-extension kotlin-android-extensions gradle-experimental

3
推荐指数
1
解决办法
2392
查看次数

找不到方法androidExtensions()

我想使用kotlin @Parcelize的功能,apply the plugin: 'kotlin-android-extensions'在gradle上,我添加了

androidExtensions { 
    experimental = true 
}
Run Code Online (Sandbox Code Playgroud)

但是错误继续出现。此错误消息:

Error:(28, 0) Could not find method androidExtensions() for arguments [build_8di01fmxa4d18k9q0yy3fdd20$_run_closure2@27f46852] on project ':app' of type org.gradle.api.Project.
<a href="openFile:F:\BELAJAR\ANDROID\AndroidStudioProjects\KADE\app\build.gradle">Open File</a>
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-extension kotlin-android-extensions gradle-experimental

3
推荐指数
1
解决办法
967
查看次数

错误:Kotlin:浮点文字不符合预期的类型 Float

我正在 kotlin 中制作一个简单的数学计算器,当我尝试初始化用作浮点整数的 0.00 的变量之一的值时,屏幕上出现错误。

var x:Float= readLine()!!.toFloat()
var y:Float= readLine()!!.toFloat()
var sum:Float=0.00// the error message is showcased in this line
sum=x+y
println("Addition " + sum)
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-extension

3
推荐指数
1
解决办法
5268
查看次数

Kotlin “toString()” 在 Android 数据绑定中不可用

刚刚了解到DataBinding并发现toString()Kotlin的强大内置功能不可用:

<layout 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="student"
            type="com.example.databindingtest2.Student" />

    </data>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{student.name}"
        android:textColor="@android:color/black"
        android:textSize="30sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@{student.age.toString()}"    //doesn't work, age is integer
        android:textColor="@android:color/black"
        android:textSize="30sp" />

</layout>
Run Code Online (Sandbox Code Playgroud)

我知道String.valueOf()会起作用,但这不是 Kotlin 的方式。任何帮助,将不胜感激。

android kotlin kotlin-extension android-databinding android-jetpack

3
推荐指数
1
解决办法
667
查看次数

将 Integer 转换为 Unit 编译成功

为什么该程序在运行时应该因 ClassCastException 失败而打印 kotlin.Unit?

class Animal<T> {
  
}

fun <T> Animal<T>.extension(block: ()-> T){
    print(block())
}

fun main(){
    //(4 as Unit) //Runtime ClassCastException, OK
    //Animal<String>().extension { 2+2 } //Compilation error, ok
    Animal<Unit>().extension { 2+2 } // Why no ClassCastException but prints kotlin.Unit?
}
Run Code Online (Sandbox Code Playgroud)

如果这不是错误,是否可以强制执行约束?

generics kotlin kotlin-extension

3
推荐指数
2
解决办法
70
查看次数

我可以通过功能参数发送扩展功能吗?

我在下面有一些扩展功能.

fun EditText.setEmailValidationListener(): TextWatcher {
    val textWatcher = object : TextWatcher {
        override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, after: Int) { }
        override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) { }
        override fun afterTextChanged(text: Editable?) { validateEmail() }

        private fun validateEmail(): Boolean {
            if (validateEmailFormat(showError = false)) {
                getParentInputLayout()?.isErrorEnabled = false
                return true
            }
            return false
        }
    }
    addTextChangedListener(textWatcher)

    return textWatcher

}

fun EditText.setPasswordValidationListener(): TextWatcher {
    val textWatcher = object : TextWatcher {
        override fun …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-extension

2
推荐指数
1
解决办法
257
查看次数

如何在Kotlin中创建化类型的对象

我正在尝试创建扩展功能以为回收站视图适配器创建视图持有者对象

inline fun <reified T: RecyclerView.ViewHolder> ViewGroup.createViewHolder(@LayoutRes res: Int): T {
    val inflater = LayoutInflater.from(context)
    val itemView = inflater.inflate(res, this, false)
    // return ViewHolder Object
}
Run Code Online (Sandbox Code Playgroud)

如何创建扩展RecyclerView.ViewHolder的T类型的对象,以便可以从函数返回。

android kotlin recycler-adapter kotlin-extension kotlin-reified-type-parameters

2
推荐指数
2
解决办法
882
查看次数

使用super在kotlin中扩展功能

如何使用super关键字在派生类中调用基类的扩展函数?

我尝试使用super打电话,但是没有用。

 open class abc {
     open fun aa() {
         println("function in abc")
     }
 }
 fun abc.sum() {
     println("extension function")
 }
 class ab: abc() {

     override fun aa() {
         super.aa()
         println("functon in ab")
     }
     fun sum() {
         super.sum()
         println("sum function")
     }
 }
 fun main(args: Array < String > ) {
     var aa: ab = ab()
     aa.aa()
     aa.aa()
     aa.sum()
 }
Run Code Online (Sandbox Code Playgroud)

这是16号数字错误,我无法调用扩展功能。

kotlin kotlin-extension

2
推荐指数
2
解决办法
555
查看次数

未解决的参考:1.3中的Kotlin中的异步

我在这里的 github中有多模块kotlin gradle项目。

我的子项目中的一个介绍协程的构建文件build.gradle.kts文件在这里

build.gradle.kts的内容是-

    import org.jetbrains.kotlin.gradle.dsl.Coroutines
    import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

    plugins {
        java
        kotlin("jvm") version "1.3.11"
    }

    group = "chapter2"
    version = "1.0-SNAPSHOT"

    repositories {
        mavenCentral()
    }

    dependencies {
        compile(kotlin("stdlib-jdk8"))
        compile("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
        testCompile("junit", "junit", "4.12")
    }

    configure<JavaPluginConvention> {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "1.8"
    }

    kotlin {
        experimental {
            coroutines   = Coroutines.ENABLE
        }
    }
Run Code Online (Sandbox Code Playgroud)

我正在尝试从此链接创建我的第一个协程程序。

import kotlinx.coroutines.*
import kotlinx.coroutines.async
import kotlin.system.*
import kotlin.system.measureTimeMillis

suspend  fun computecr(array: IntArray, low: Int, high: Int): Long …
Run Code Online (Sandbox Code Playgroud)

coroutine kotlin kotlin-extension kotlinx.coroutines

2
推荐指数
2
解决办法
2476
查看次数

是否可以在Kotlin中添加运算符重载扩展功能?

我的意思是这样的:

fun operator Table.get(column_name: String) = this.column(column_name)
// Currently gives an error: "Expecting a top level declaration"
Run Code Online (Sandbox Code Playgroud)

Table 实例当前的工作方式如下: table.column("column_name")

我想使它像这样工作: table["column_name"]

kotlin kotlin-extension

2
推荐指数
1
解决办法
45
查看次数