Kotlin直接使用控件id作为变量使用
作者:互联网
1,根目录下build.gradle:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
2.app下build.gradle
//写法一
plugins {
id 'kotlin-android'
id 'kotlin-android-extensions'
}
//写法二
applyplugin:'kotlin-android'
applyplugin:'kotlin-android-extensions'
3.页面中引入
import kotlinx.android.synthetic.main.xxx.*
直接获取按钮ID,mBtnSignIn点击事件
import kotlinx.android.synthetic.main.fragment_login.*
//@AndroidEntryPoint
class LoginFragment : BaseFragment() {
override val layoutId: Int = R.layout.fragment_login
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binds()
}
private fun binds() {
mBtnSignIn.setOnClickListener {
// mViewModel.login(tvUsername.text.toString(), tvPassword.text.toString())
}
}
}
<Button
android:id="@+id/mBtnSignIn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginTop="32dp"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:text="@string/login_sign_in"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="18sp"
android:textStyle="bold" />
标签:控件,kotlin,gradle,extensions,Kotlin,login,android,id 来源: https://blog.csdn.net/qq_30998053/article/details/122041286