android studio 3.0 preview 使用kotlin写android,抛弃findviewbyid
2017-05-20 12:24
716 查看
听所使用kotlin就可以不用findviewbyid来找控件了,小生迫不及待的尝试了一下,结果。。。没仔细看官方文档,经验不足,掉坑里了
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.miracle.kotlindemo.MainActivity">
<TextView
android:id="@+id/tv" //添加一个id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
直接使用tv.text赋值,提示找不到tv。。。网上查了一下,需要导入
网上找了半天,尝试了几种方案也没解决,最后在官方文档中找到解决方法
在build.gradle(Module: app)中添加
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.miracle.kotlindemo.MainActivity">
<TextView
android:id="@+id/tv" //添加一个id
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
直接使用tv.text赋值,提示找不到tv。。。网上查了一下,需要导入
import kotlinx.android.synthetic.main.activity_main.*;结果提示kotlinx找不到。。。
package com.example.miracle.kotlindemo import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.*; class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //val tv=findViewById(R.id.tv) as TextView; tv.text="Hello Kotlin"; } }
网上找了半天,尝试了几种方案也没解决,最后在官方文档中找到解决方法
在build.gradle(Module: app)中添加
apply plugin: 'kotlin-android-extensions'
就可以了,默认只有kotlin-android,不懂官方为什么不默认把这个加上。。。
相关文章推荐
- android studio 3.0 使用Kotlin抛弃findViewById()
- 使用Kotlin,抛弃findViewById
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- android 减少findViewById 的使用
- Android使用DataBinding代替findViewById
- Android Studio使用系列——butterknife插件代替findViewById
- 关于android Activity中注解的使用,省去无用的findviewbyid....setonclick.....
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- Android studio 使用findviewbyme 快速生产 findviewbyid 引用资源
- AndroidStudio中使用Kotlin--findViewById
- android开发经验-findViewById(参数)的使用
- Android开发学习之路-让注解帮你简化代码,彻底抛弃findViewById
- 求助 android开发中 如果两个控件的id相同 会怎样?如何使用findviewbyid ()寻找到?
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- Android 注解的简单使用之省去findViewById()
- Android开发框架xUtils简介(附有不使用findViewById的方法)
- Android解决使用findViewById时需要对返回值进行类型转换问题的辅助类
- Android--xml控件写法和findViewById的对应使用
- kotlin Android中 findViewById()不能推导类型的错误
- Kotlin的android扩展:对findViewById说再见(KAD 04)