一步步学习kotlin for android(三) kotlin省略findviewById
来源:互联网 发布:邮箱正则表达式php 编辑:程序博客网 时间:2024/05/17 02:44
findViewById 今天的内容涉及到findViewByID,android语言原来这个特别繁琐,现在好了,kotlin语言,直接拿来布局里面的id用,省去好多重复工作量啊
在使用kotlin的id之前,需要先在builde.gradle里引入这个
apply plugin: 'com.android.application'apply plugin: 'kotlin-android'apply plugin: 'kotlin-android-extensions'android { compileSdkVersion 23 buildToolsVersion '25.0.0' defaultConfig { applicationId "" minSdkVersion 15 targetSdkVersion 21 versionCode 45 versionName "4.2.0" }加入黄色字体部分
然后再在activity里面引入:
import kotlinx.android.synthetic.main.activity_start.*
其中,activity_start就是你的布局文件的名称,*代表布局里所有的id接着就可以直接使用布局里面的id了,例如:
<ImageView android:id="@+id/img" android:layout_width="fill_parent" android:layout_height="fill_parent" android:contentDescription="@null" android:scaleType="fitXY" android:src="@mipmap/bg_home" /><TextView android:id="@+id/timecount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_margin="@dimen/spacing_small" android:text="3" android:textColor="@color/normal" android:textSize="@dimen/font_large" android:visibility="gone"/>
在activity里不需要做任何其他查找,直接使用
img.setBackgroundColor(resources.getColor(R.color.background));
getResources().getColor(R.color.background));成了timecount.text="我是kotlin";resources.getColor(R.color.background));
阅读全文
0 0
- 一步步学习kotlin for android(三) kotlin省略findviewById
- 一步步学习kotlin for android(一)配置kotlin
- 一步步学习kotlin for android(二)创建kotlin类
- Kotlin for Android(三)Kotlin数据类型
- Kotlin与Android的奇妙之旅-FindViewbyid(三)
- android studio 3.0 使用Kotlin抛弃findViewById()
- Kotlin-Android-Extensions:不仅仅是替代findViewById
- kotlin语言学习文档 for Android
- Kotlin for Android
- Kotlin for Android
- Kotlin for Android
- Kotlin for Android
- Kotlin for Android
- Kotlin for Android
- kotlin-for-android-developers
- Kotlin学习教程(三)
- Kotlin for Android(九)Kotlin集合
- Kotlin学习系列----Android Studio配置Kotlin
- Basic threading 建立线程(Thread)
- mysql查询表里的重复数据方法和删除重复数据
- MBTiles离线包生成和使用
- 记录一次项目引用导致的StackOverflowError
- linux-svn命令
- 一步步学习kotlin for android(三) kotlin省略findviewById
- php json_decode输出null, json_last_error() 页面显示4,也就是语法错误。
- Android实现系统关机和重启
- Android之利用正则表达式校验邮箱、手机号、密码、身份证号码等
- mina两种协议获取方式(TCP/UDP)
- hive 表修改字段类型
- html5移动端input file可进行拍照和选择图片
- [学习linux]Ubuntu kylin 16.04下安装mysql 问题及总结
- 内存操作