美文网首页
说说在 Android 中如何不使用 findViewById(

说说在 Android 中如何不使用 findViewById(

作者: deniro | 来源:发表于2021-12-05 20:14 被阅读0次

1 安装

在 build.grade 中加入以下配置[1]

apply plugin: 'kotlin-android-extensions'

点击 “Sync Now” 进行安装:

在这里插入图片描述

2 使用

在 Activity 类中导入 import kotlinx.android.synthetic.main.first_layout.*

然后就可以直接使用资源 ID 来引用对象了:

在这里插入图片描述

资源 ID 是定义在 layout.xml 类型的文件中:

   <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮" />

通过 kotlin-android-extensions 插件,简化了 Android 代码开发,推荐使用。

参考资料

  1. https://www.jianshu.com/p/8fa6854566e9

相关文章

网友评论

      本文标题:说说在 Android 中如何不使用 findViewById(

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