Kotlin在Android上的运用(二)
来源:互联网 发布:软件存储权限 编辑:程序博客网 时间:2024/06/08 03:45
这次主要写一下kotlin上的函数运用和bean类运用
一、控件的动态使用
先看一下xml布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.hao.myrxjava.MainActivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /></RelativeLayout>
我们在代码动态设置textview的内容,我们只需要这样
package com.hao.myrxjava;import android.os.Bundleimport android.support.v7.app.AppCompatActivityimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textview.text = "123" }}
我滴哥啊,是不是很神奇,只需要一行代码,textview是TextView的id,这样写之前必须导入
import kotlinx.android.synthetic.main.activity_main.*
如果像我们之前的Java代码需要俩步
TextView textview = (TextView) findViewById(R.id.textview);textview.setText("123");
不要小看只少了一句代码,仔细想想看,如果在一个复杂的布局里面有很多控件,是不是代码会少了很多啊
二、对控件加监听
对上面提到的textview添加点击监听事件,我们只需要
textview.setOnClickListener { //do something}
真的是很简单啊,代码少了匿名内部类的好多代码,如果是Java代码我们需要
textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //do something } });
是不是感觉到爽了,Kotlin就是如此
三、bean类的运用
在Java中bean类写法
package com.hao.myrxjava;/** * java bean类 * * @author zhanghaohao089 * @date 2017/5/19 */public class PeopleBean { private String name; private String sex; public PeopleBean(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; }}
bean类定义了俩个属性 name sex。为什么bean类的属性都定义为private,是为了属性权限定于为私有的不能随意改变,改变只能通过get、set函数,然而有时候需要做一些额外的操作,只需要在get、set函数里写代码就可以了。然而也可以用AOP实现
再看一下Kotlin上bean类的实现
data class PeopleBean(var name: String, var sex: String)
我的乖乖,只需要一句代码就搞定了
var peopleBean= PeopleBean("name", "sex");peopleBean.name = "name1";peopleBean.sex = "sex1";
语法介绍
阅读全文
0 0
- Kotlin在Android上的运用(二)
- Kotlin在Android上的运用(三)
- Kotlin在Android上的运用(四)
- Kotlin在Android studio上的环境配置和运用(一)
- Git在Android Studio上的运用
- Android开发,Kotlin的了解与学习(九)-----在Android上的具体应用
- kotlin在Android studio3.0上的配置问题
- Kotlin在Android上令人惊叹的技巧
- Kotlin在Android上令人惊叹的技巧
- kotlin第一次运行在Android上
- Kotlin Android 扩展(二)
- Kotlin在AndroidStudio上的配置
- Android开发,Kotlin的了解与学习(二)-----Kotlin数据类型与Extensions的学习
- 运用Kotlin开发Android应用的一些技巧
- 我的Kotlin For Android之路开篇(二)
- Kotlin for Android(二)Kotlin基础语法
- 【Android】使用Kotlin在Android Studio上开发App
- 【Android】使用Kotlin在Android Studio上开发App
- Android 螺旋水平进度条 progressbar
- python scrapy 之 随机选择user-agent
- 提升Android比于iOS吸引力,谷歌欲借力人工智能
- Oracle 12.2新特性掌上手册
- WebService
- Kotlin在Android上的运用(二)
- POJ 1195 Mobile phones 题解
- 20150519-jQuery ajax()
- SDN学习日记—基于RYU的hub开发1
- Windows/Android/IOS WebRTC音视频总结(二)
- mapreduce编程练习(一)简单的练习 WordCount
- Lowest Common Ancestor of a Binary Tree
- 如何看待 Google 最新的系统 Fuchsia?
- 对LOWORD, HIWORD, LOBYTE, HIBYTE的理解