Tag: android

Kotlin属性访问语法如何为Java类(即EditText setText)工作?

我试图将我的Android项目切换到Kotlin。 我有一个EditText ( TextView的子TextView ),我想以编程方式设置提示和文本。 提示按预期工作。 但是,对于文本,如果我尝试使用Kotlin setter语法执行操作,则会出现类型不匹配异常: val test = EditText(context) test.setHint("hint") // Lint message: "Use property access syntax" test.hint = "hint" // ok test.setText("text") // ok (no lint message) test.text = "text" // Type mismatch: inferred type is kotlin.String but android.text.Editable! was expected 如果我们看看这个声明,我们会发现从TextView继承的相同的签名: public final void setHint(CharSequence hint) public final void setText(CharSequence […]

如何在Kotlin for Android上使用“setTextColor(hexaValue)”,

背景 在Java中,我可以使用它的标准十六进制值直接更改TextView的文本颜色: textView.setTextColor(0xffffffff); //white textView.setTextColor(0x00000000); //transparent textView.setTextColor(0xff000000); //black textView.setTextColor(0xff0000ff); //blue //etc… 好简单… 问题 在Kotlin上,如果我尝试写这样的东西,我得到一个奇怪的构建错误: 错误:(15,18)以下函数都不能用所提供的参数调用:public open fun setTextColor(p0:ColorStateList!):在android.widget.TextView中定义的单位public open fun setTextColor(p0:Int):Unit在android.widget.TextView中定义 我试过了 我试图通过互联网搜索这个,我看不出有什么特别的十六进制值。 看起来像在Java上一样: https://kotlinlang.org/docs/reference/basic-types.html 然后我决定用Java编写,转换成Kotlin。 结果在颜色值方面是非常不可读的: textView.setTextColor(-0x1) //white textView.setTextColor(0x00000000) //transparent textView.setTextColor(-0x1000000) //black textView.setTextColor(-0xffff01) //blue 对我来说,用于Kotlin的Integer的十六进制值似乎是有符号的,而在Java上它会自动转换为带符号的值,所以这会导致值的翻转以及需要时设置一个负号。 我能想到的唯一的东西,仍然允许阅读,就像这样: textView.setTextColor(Integer.parseUnsignedInt("ffff0000",16)); 但是,这有多个缺点: 这是更长的时间。 它转换一个字符串,所以效率要低得多 最重要的是:它仅适用于API 26(Android O),它目前在全球约1%的Android设备上处于活动状态。 问题 为什么会发生? 我能做些什么才能使其具有最好的可读性,无需字符串转换,并且可以在所有Android版本上运行(在我的情况下,minSdkVersion 14)?

在创建gradle插件时,把生成的java或kotlin源放到哪里?

我不太确定 – 通常在/build/generated下有一个目录,但是我想知道是否有一个标准的位置可以安全地编写源文件,这些文件也将包含在编译中? 环境变量? 我找不到文档中的任何内容。 如果重要的话,这个用例就是一个单独的kotlin文件,我把它作为一个基于配置的独立任务生成,并且希望包含在java / kotlin编译中。

如何将此代码从iOS Swift转换为Android Kotlin

我试图将此Swift代码转换为Kotlin Android,并有问题。 Swift code: public let kCORNER_RADIUS_BUTTON: CGFloat = 4.0 我已经转换了我在下面找到的CGFloat java类 /** * CGFloat * @author [Harald Kuhr](mailto:harald.kuhr@gmail.com) * * * @author duncan */ class CGFloat : Number { private val value: Double constructor() { value = 0.0 } constructor(d: Double) { value = d } override fun toByte(): Byte { return value.toByte() } […]

Android – Kotlin – 对象必须声明为抽象或实现抽象成员

我为我的RecyclerView设置了一个ItemClickLister,像这样: ItemClickSupport.addTo(recyclerView!!).setOnItemClickListener( object : ItemClickSupport.OnItemClickListener { override fun onItemClicked(recyclerView: RecyclerView?, position: Int, v: View?) { val row = recyclerView!!.getChildAt(position) val el = row.findViewById(R.id.active_expandablelayout) as ExpandableLayout if (el.isExpanded) { el.collapse() } else { el.expand() } } } ) 使用我翻译成Kotlin的ItemClickSupport库。 我得到一个object (第2行)的错误,说: 对象必须声明为抽象或实现抽象成员。 我对Kotlin来说真的很陌生,在SO上我也找不到任何解决方案。 任何帮助是极大的赞赏。 编辑: 这是我的ItemClickSupport.kt : class ItemClickSupport private constructor(private val mRecyclerView: RecyclerView) { private […]

如何传递和从片段和活动android kotlin获得价值

我是新来的kotlin。 任何人都可以指导我如何通过,并从片段和活动中获得价值

类型干扰失败。 期望的类型不匹配:必需的字符串在Kotlin和Anko中找到pair <String,String>

我用kotlin和anko库创建了一个数据库。 我正在关注这篇文章https://antonioleiva.com/databases-anko-kotlin/我想在下面的数据库块中插入数据,但我得到一个错误 类型干扰失败。 期望的类型不匹配:必需的字符串找到对 fun insertPerson() { database.use { insert(PersonTable.Name, PersonTable.PersonName to "XX", PersonTable.Domain to "Technology", PersonTable.MobileNumber to "XXXXX") } } object PersonTable { val Name = "Person" val ID = "id" val PersonName = "person_name" val Domain = "domain" val MobileNumber = "mobile_number" } 的build.gradle apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' […]

与Proguard和Kotlin“找不到引用类”

我遇到了proguard和kotlin的一个奇怪的问题。 我逐渐将一个项目编程转换为kotlin – 目前为止,这个项目已经非常好了,但是在转换某些类时,我却遇到了proguard错误。 我还不能确定这些类的特殊属性是如何破坏的 – 它们似乎与其他类没有区别。 作为一个例子InputStreamWithSource只是: package org.ligi.passandroid.model import java.io.InputStream class InputStreamWithSource(val source: String, val inputStream: InputStream) 它完全在IDE中工作 – 我可以部署到一个设备 – 也所有的UI测试运行良好。 只是当试图组装发布项目我得到proguard错误我不明白: Warning: org.ligi.passandroid.ui.FileUnzipControllerSpec: can't find referenced class org.ligi.passandroid.model.InputStreamWithSource Warning: org.ligi.passandroid.ui.FileUnzipControllerSpec: can't find referenced class org.ligi.passandroid.model.InputStreamWithSource Warning: org.ligi.passandroid.ui.InputStreamProvider: can't find referenced class org.ligi.passandroid.model.InputStreamWithSource Warning: org.ligi.passandroid.ui.InputStreamProvider: can't find referenced class org.ligi.passandroid.model.InputStreamWithSource Warning: org.ligi.passandroid.ui.InputStreamProvider: […]

Kotlin注释处理器+ AutoService

我有以下问题: – 创建了一些模块,实现了一个Component类,它用@AutoService(Component::class)注释 – 我的Android应用程序使用ServiceLoader来检索这些类。 但由于某种原因, kapt不会生成META-INF/services/…的文件META-INF/services/… 我的模块gradle.file: apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' android { compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } […]

在Kotlin(Android)中以字符串形式读取文本资产(来自assets文件夹的文本文件)

我需要读取存储在src/main/assets/ ie中的文本文件; 在资产文件夹中并将其作为字符串获取。 有一个简单的方法来做到这一点。 Java复制,粘贴,转换功能是麻烦,所以我宁愿使用kotlin的方式。 我需要一个kotlin的方式来做到这一点