kotlin for android 点击事件和开启activity

发布时间:2019-06-15 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了kotlin for android 点击事件和开启activity脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1.Kotlin点击事件

xML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout >"http://schemas.andROId.COM/apk/res/android"
    >"http://schemas.android.com/apk/res-auto"
    >"http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.boxunpark.www.kotlinapplication.MainActivITy">

    <Button
        android:id="@+id/hello_text"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="Hello World!" />

    <Button
        android:id="@+id/click"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="点击" />

</LinearLayout>
代码:
 //第一种点击方式
       hello_text.setOnClickListener {
          ...
        }
        //第二种点击方式
        click.setOnClickListener({ view ->
           ...
        })

2.Kotlin开启一个Activity:
在用java实现时我们是这样的:

startActivity(new Intent(this,SecondActivity.class));

使用Kotlin实现

 startActivity(Intent(this@MainActivity, SecondActivity::class.java))

3.Kotlin中给TextView赋值

1.使用java实现
//java代码
TextView hello_text=(TextView)findViewById(R.id.hello_text)
hello_text.settext("你好啊")
2.使用Kotlin实现:
//kotlin实现
 hello_text.text="你好啊"

脚本宝典总结

以上是脚本宝典为你收集整理的kotlin for android 点击事件和开启activity全部内容,希望文章能够帮你解决kotlin for android 点击事件和开启activity所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。