未解决的参考:Kotlin中的findViewById [英] Unresolved Reference: findViewById in Kotlin
问题描述
fun Tryouts() {
var CheckBox1 : CheckBox = findViewById(R.id.ForwardBox) as CheckBox
CheckBox1.setChecked(false)
}
我仍然是Kotlin的初学者,只学习过Kotlin的基本工作原理,无论是TextView还是CheckBox或RadioBox,我都无法引用任何android小部件或更改其在Android Studio中的状态.
在所有情况下,findViewById都存在相同的未解决的参考错误...
我不知道我做错了什么,即使java转换也会输出相同的错误.
I'm still a beginner in Kotlin having learnt only the basic working of kotlin, I am unable refer to any android widget or change it's state in Android Studio whether it's TextView or CheckBox or RadioBox.
Same Unresolved Reference errors for findViewById in all cases...
I don't know what is it that I am doing wrong, even java conversion outputs the same errors.
推荐答案
看来这是摆脱findViewById ()
转到您的Build.Gradle(模块:应用程序)
Go to your Build.Gradle (Module: app)
添加以下行
apply plugin: 'kotlin-android-extensions'
- 然后它将要求您同步
- 然后按同步
之后进入您的活动文件说很多话MainActivity.kt
After that come to your Activity file Say it many be MainActivity.kt
有进口 导入单个视图
import kotlinx.android.synthetic.main.<layout_name>.<view_name>;
或
要导入所有视图
import kotlinx.android.synthetic.main.<layout_name>.*;
示例: 在您的布局
<Checkbox id="@+/forwardBox" . . . />
它在activity_main
布局中
然后导入为
it is in activity_main
layout
then import as
import kotlinx.android.synthetic.main.activity_main.forwardBox;
所以在您的函数或类中直接使用它
so either in your function or class use it directly
forwardBox.isChecked = false
这篇关于未解决的参考:Kotlin中的findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!