Tag: 安卓

什么是在Anko协程kotlin推迟?

在kotlin的Anko coroutines库中,有一个特性bg()用于在后台线程上轻松地执行你的代码。 在那个返回类型是Deferred 。 那么什么是延期 ? Refrence链接 (1) https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Deferred.kt (2) https://github.com/Kotlin/anko/wiki/Anko-Coroutines#bg fun getData(): Data { … } fun showData(data: Data) { … } async(UI) { val data: Deferred<Data> = bg { // Runs in background getData() } // This code is executed on the UI thread showData(data.await()) }

等同于在kotlin上实现java接口

我是Kotlin新手,我想知道如何在kotlin上实现java接口,我在android上使用它, public interface OnClickedItemListener { void onClick(boolean state); } OnClickedItemListener是我的自定义接口,我想在kotlin中实现,我有这个类: class MyProgressView : RelativeLayout { constructor(context: Context?) : super(context) { init() } constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { init() } constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init() } private fun init() { LayoutInflater.from(context).inflate(R.layout.download_progress_layout, this) cusotmView.setOnClickListener { } } […]

应该把Kotlin文件放在Android的一个单独的源码目录中吗?

除了Java之外,我将开始使用Kotlin进行Android开发,因为它的好处。 我已经安装了Android Studio插件,并在我的gradle文件中包含了相关的依赖关系。 所以我从Kotlin文档和Stack Overflow中读到,可以为Kotlin文件包含一个单独的源目录,如下所示: 应用: -表现 -java -kotlin -res 我知道我可以通过添加以下内容到我的build.gradle文件来创建此目录: sourceSets { main.java.srcDirs += 'src/main/kotlin' } 我的问题是: 如果Kotlin文件与相同目录中的Java文件“存在”或不存在? 除了意见之外,我想知道是否有一个特定的惯例,如果是这样,为什么它是这样的。 如果不是,那么每个选项的优缺点是什么?

带参数的单身人士在Kotlin

我正在尝试将Android应用程序从Java转换为Kotlin。 应用程序中有几个单身人士。 我没有构造函数参数的单身人士使用伴侣对象。 还有另外一个单例需要一个构造函数参数。 Java代码: public class TasksLocalDataSource implements TasksDataSource { private static TasksLocalDataSource INSTANCE; private TasksDbHelper mDbHelper; // Prevent direct instantiation. private TasksLocalDataSource(@NonNull Context context) { checkNotNull(context); mDbHelper = new TasksDbHelper(context); } public static TasksLocalDataSource getInstance(@NonNull Context context) { if (INSTANCE == null) { INSTANCE = new TasksLocalDataSource(context); } return INSTANCE; } } 我在kotlin的解决方案: […]

在kotlin中尝试makeSceneTransitionAnimation

而学习kotlin我尝试创建makeSceneTransitionAnimation图像转换,但它不像在java中,这是我的代码从适配器类 itemView.imageContent.setOnClickListener { val i = Intent(context, ImagePreviewActivity::class.java) i.putExtra("IMAGE", item.content) val trans = context.getString(R.string.transition) val options = ActivityOptionsCompat.makeSceneTransitionAnimation( Activity(), itemView.imageContent, trans ) context.startActivity(i, options.toBundle()) } 这是我的logcat java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.view.Window.hasFeature(int)' on a null object reference at android.app.ActivityOptions.makeSceneTransitionAnimation(ActivityOptions.java:709) at android.app.ActivityOptions.makeSceneTransitionAnimation(ActivityOptions.java:648) at android.app.ActivityOptions.makeSceneTransitionAnimation(ActivityOptions.java:622) at android.support.v4.app.ActivityOptionsCompat.makeSceneTransitionAnimation(ActivityOptionsCompat.java:172) at com.docotel.binmaspublic.module.report.ReportAdapter$ViewHolder$bind$2.onClick(ReportAdapter.kt:130) 对不起,如果你发现这是一个重复的问题

Kotlin扩展bug

我有个问题。 我有一个旧的项目,写在kotlin.It是1.0.2版本,但我有一个编译器扩展。 java.lang.ClassNotFoundException: com.intellij.util.containers.EmptyIterator 它是build.gradle应用程序文件: apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'io.fabric' android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "eu.***.**.**" minSdkVersion 16 targetSdkVersion 23 versionCode 7 versionName "1.0.13" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main.java.srcDirs += 'src/main/kotlin' } lintOptions { […]

Kotlin中活动与片段之间的交流

当谷歌宣布Kotlin为Android开发的官方语言时,我正在尝试使用Kotlin来构建一个应用程序。 问题:如何使用Kotlin在活动和片段之间进行交流? 谢谢!。

Kotlin android parcelable

我已经使用Android插件的android工作室的自动生成的功能,它生成了我下面的代码,但我不明白为什么需要为CREATOR字段的final val CREATOR ? 我在kotlin第一次看到final关键字。 data class Person( val name: String, val surname: String ) : Parcelable { constructor(source: Parcel): this(source.readString(), source.readString()) override fun describeContents(): Int { return 0 } override fun writeToParcel(dest: Parcel?, flags: Int) { dest?.writeString(name) dest?.writeString(surname) } companion object { @JvmField final val CREATOR: Parcelable.Creator<Person> = object : Parcelable.Creator<Person> { override fun […]

Kotlin:我们可以在Kotlin中使用@订阅EventBus(GreenRobot)吗?

我的onEvent在一个片段中,如下所示,在我的Kotlin函数中捕获活动的认证。 但是,我无法得到onEvent触发。 @Subscribe fun onEvent(event: AuthenticationEvent) { if (event.isAuthenticated) { startFragment(signInFragment, SignInFragment.TAG) } else { startFragment(signOutFragment, SignOutFragment.TAG) } } 在我的build.gradle文件中,我添加了这个 compile 'org.greenrobot:eventbus:3.0.0' 有什么我需要做得到这个触发器?

Kotlin的Proguard混淆关键字

我正在使用这个关键字与我的android-java项目中的proguard 混淆 : do if for int new try byte case char else goto long this void break catch class const final float short super throw while double import native public return static switch throws boolean default extends finally package private abstract continue strictfp volatile interface protected transient implements instanceof synchronized 但是我正在切换到Kotlin ,现在的问题是我应该使用哪些关键字作为Kotlin? 我已经找到了,但我应该使用所有这些?