About onclick listener


#1

button’ onclick in anko coroutine is deprecated now,What insteads it?


#2

I try, but not deprecated in latest anko v0.10.8


#3

This is my code:
va Btn=button(“Login”){
id R.id.button_login
onClick{
val imageUri:Uri
val outputImage= File(ctx?.getExternalCacheDir(), “output_image.jpg”)
if (outputImage.exists()) outputImage.delete()
outputImage.createNewFile()
if (Build.VERSION.SDK_INT >= 24)
imageUri = FileProvider.getUriForFile(ctx,“com.gyq.cameraalbumtest.fileprovider”, outputImage)
else imageUri = Uri.fromFile(outputImage)
val ExtData = Pair<String,Uri>(MediaStore.EXTRA_OUTPUT,imageUri)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
(ctx as Activity).startActivityForResult(intent,1)
}
onClick is prompted deprecated.


#4

Im my Project, button#onClick is from ‘anko-sdk25-coroutines-0.10.8’ : ListenersWithCoroutines.kt

maybe you can check your build.gradle, whether it contains

implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"

this is source code:

fun android.view.View.onClick(
        context: CoroutineContext = Dispatchers.Main,
        handler: suspend CoroutineScope.(v: android.view.View?) -> Unit
) {
    setOnClickListener { v ->
        GlobalScope.launch(context, CoroutineStart.DEFAULT) {
            handler(v)
        }
    }
}

#5

The anko version of mine is 0.10.7-2. Has v0.10.8 released?


#6

Yes , you can see
https://github.com/Kotlin/anko/releases/tag/v0.10.8


#7

There’s only source code,no anko-ide-plugin.


#8

My gradle contains " implementation “org.jetbrains.anko:anko:$anko_version”".This should include your that statement.I also add that statement,but it’s useless.


#9

this is My all dependencies:

    implementation "org.jetbrains.anko:anko-sdk25:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
    implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"

#10

I changed anko-version to 0.10.8 in my gradle,then ok.But my anko support version is still 0.10.7-2,not updade to 0.10.8,why?


#11

paste all your dependencies in build.gralde


#12

dependencies {
implementation fileTree(include: [’*.jar’], dir: ‘libs’)
implementation “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”
implementation “org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version”
//noinspection GradleCompatible
implementation ‘com.android.support:appcompat-v7:27.1.1’
implementation ‘com.android.support.constraint:constraint-layout:1.1.3’
implementation “org.jetbrains.anko:anko:$anko_version”
//implementation “org.jetbrains.anko:anko-commons:$anko_version”
//implementation “org.jetbrains.anko:commons-base:$anko_version”
implementation “org.jetbrains.anko:anko-constraint-layout:$anko_version”
//implementation “org.jetbrains.anko:anko-sdk27:$anko_version”
// sdk15, sdk19, sdk21, sdk23 are also available
//implementation “org.jetbrains.anko:anko-appcompat-v7:$anko_version”
//implementation “org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version”
// Coroutine listeners for Anko Layouts
//implementation “org.jetbrains.anko:anko-coroutines:$anko_version”
//implementation “org.jetbrains.anko:anko-sdk28-coroutines:$anko_version”
//implementation “org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version”
// Design
//implementation “org.jetbrains.anko:anko-design:$anko_version”
//implementation “org.jetbrains.anko:anko-design-coroutines:$anko_version”
//implementation “org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1”
//implementation “org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1”
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘com.android.support.test:runner:1.0.2’
androidTestImplementation ‘com.android.support.test.espresso:espresso-core:3.0.2’
}


#13

It seems no problem,
Maybe you can try one of the opts belows :
run Build -> Clean Project from toolsbar
or execute ./gradlew clean at bootom termail tab
or restart -> invalidate an restart


#14

I mean the Anko Layout Preview is still 0.10.7-2


#15

However, it works for me

You can find diff in build.gradle from yours.

build.gradle for project

buildscript {
    ext.kotlin_version = '1.3.0'
    ext.coroutines_version = '1.0.1'
    ext.anko_version = "0.10.8"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

build.gradle for app

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
    implementation "org.jetbrains.anko:anko:$anko_version"
    implementation "org.jetbrains.anko:anko-coroutines:$anko_version"

    implementation "org.jetbrains.anko:anko-sdk25:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
    implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
    implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}


#16

OK,mine’s work ,too.I only want to understand why anko layout preview not to update to 0.10.8 synchronously.