美文网首页
kotlin安卓教程一之控件直接通过id调用

kotlin安卓教程一之控件直接通过id调用

作者: 程序猿的小生活 | 来源:发表于2022-04-15 09:49 被阅读0次

1.在app->build.gradle->plugins 中添加以下插件

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'//此插件引用后可以直接通过控件id 调用控件
}

2.将鼠标放到setContentView(activity_main)中的activity_main上,按下alt+enter 导入以下包

import kotlinx.android.synthetic.main.activity_main.*

3.接下来可以直接通过id调用控件

  <TextView
        android:id="@+id/tv111"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
tv111.setOnClickListener {
             tv111.text="你好啊"
         }

相关文章

网友评论

      本文标题:kotlin安卓教程一之控件直接通过id调用

      本文链接:https://www.haomeiwen.com/subject/otpfertx.html