Tag: 安卓

Android Kotlin打开资产文件

我想打开资产文件。 在java代码工作之前,但当我更改代码kotlin它不起作用。 Java代码的工作 InputStream streamIN = new BufferedInputStream(context.getAssets().open(Database.ASSET)); OutputStream streamOU = new BufferedOutputStream(new FileOutputStream(LOCATION)); byte[] buffer = new byte[1024]; int length; while ((length = streamIN.read(buffer)) > 0) { streamOU.write(buffer, 0, length); } streamIN.close(); streamOU.flush(); streamOU.close(); 我将代码更改为Kotlin,但它不起作用 var length: Int val buffer = ByteArray(1024) BufferedOutputStream(FileOutputStream(LOCATION)).use { out -> { BufferedInputStream(context.assets.open(Database.ASSET)).use { length = it.read(buffer) if (length […]

以编程方式在Kotlin中定制TypeFace

如何设置任何TextView字体。 使用java我们做这样的事情 TextView tv = (TextView) findViewById(R.id.custom); tv.setTypeface(null, Typeface.BOLD); 如何在使用kotlin时执行此类操作。 这将是在kotlin中做到这一点的有效方法? 有没有更好的方式提供kotlin? holder.itemView.title.typeface = ?

Android kotlin扩展错误

java.lang.VerifyError:… / utils / KotlinViewExtKt $ animateFadeOut $ 1 在仿真器上运行应用程序时出现错误PRE Lolipop(<21 api) 引起麻烦的function: fun View.animateFadeOut(animDuration: Long = 250) { this.animate() .alpha(0F) .setDuration(animDuration) .setListener(object : Animator.AnimatorListener { override fun onAnimationRepeat(p0: Animator?) {} override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) { super.onAnimationEnd(animation, isReverse) show(false) } override fun onAnimationEnd(p0: Animator?) { show(false) } override fun onAnimationCancel(p0: Animator?) { } […]

For循环在kotlin

我是Kotlin的新人,请帮助我实现这一点。 int number[] = {5,4,1,3,15} for(int i = number.length; i > 0; i–) { Log.e(“number”, number[i]) }

什么是Kotlin中的generateStubs配置?

什么是Kotlin的generateStubs ? 这是我在build.gradle配置。 我在这里的公共文档中找不到它: http : //kotlinlang.org/docs/reference/kapt.html kapt { generateStubs = true } 如果我在我的项目中使用Java和Kotlin(1.2) ,仍然需要添加?

在kotlin中设置UserDefaultsKeys(swift)

如何在kotlin中设置UserDefaultsKeys(swift)。 将喜欢设置用户数据和登录状态。 一个快速的例子 //swift example; how is this possible with kotlin? set(value, forKey: UserDefaultsKeys.userid.rawValue) synchronize()

在Kotlin中按多个字段排序收集

比方说,我有一个人名列表,我需要首先按年龄,然后按名称排序。 来自C#的背景,我可以通过使用LINQ轻松实现这种语言: var list=new List(); list.Add(new Person(25, “Tom”)); list.Add(new Person(25, “Dave”)); list.Add(new Person(20, “Kate”)); list.Add(new Person(20, “Alice”)); //will produce: Alice, Kate, Dave, Tom var sortedList=list.OrderBy(person => person.Age).ThenBy(person => person.Name).ToList(); 如何使用Kotlin完成这个工作? 这是我试过的(这显然是错误的,因为第一个“sortedBy”子句的输出被第二个输出覆盖,导致列表按Name排序) val sortedList = ArrayList(list.sortedBy { it.age }.sortedBy { it.name })) //wrong

kotlin中的“::”是什么意思?

我是Kotlin的新手 我用这个代码打开另一个活动: startActivity(Intent(this,IntroAndLang::class.java)) 目前的活动和目标活动写在Kotlin 我不明白为什么没有单一的:而不是::在IntroAndLang::class.java

Android / Kotlin:未解决的参考:木材

我正在尝试为Android编写一个kotlin库,不能包含木材。 我总是得到以下错误: Error:error: unresolved reference: timber 我有这个在我的build.gradle: apply plugin: ‘java-library’ apply plugin: ‘kotlin’ dependencies { implementation fileTree(dir: ‘libs’, include: [‘*.jar’]) compile “org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version” } sourceCompatibility = “1.8” targetCompatibility = “1.8” buildscript { ext.kotlin_version = ‘1.1.2-4’ repositories { maven {url “https://maven.google.com”} mavenCentral() } dependencies { classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” } } repositories { mavenCentral() } dependencies { compile “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” […]

Kotlintypes推断faild

当我在项目中使用Kotlin和Java时在Java BaseActivity.class中: public abstract Class bindViewModel(); 当我在Kotlin中扩展BaseActivity时: override fun <T : BaseViewModel<*, out IBaseView>?> bindViewModel(): Class { return ArchViewModel::class.java } Kotlin提醒我返回是types推断expression式 Type inference failed. Expected type mismatch: required:Class found:Class 如何解决这个问题? PS的ArchViewModel.class扩展BaseViewModel