Android Studio 下的Kotlin HelloWorld
来源:互联网 发布:catia软件好学吗 编辑:程序博客网 时间:2024/05/17 04:48
一、Android Studio下Kotlin环境配置
Kotlin的一轮浪潮迅猛扑过来,借助这个风口,先多学习学习。
【某人说,在风口,猪也能飞起来。笨鸟先飞罗 ~_~】
1,安装Kotlin的相关插件
执行 Settings -> plugins -> BrowseRepositories中搜索“Kotlin”
安装kotlin插件。【关于是否安装Kotlin Extensions不同博客有不同说明,实际测试中没有安装也可行】
2,新建Kotlin Activity
3,配置Kotlin Configure
配置成功后,在Module下的Build.gradle中有添加内容:
apply plugin: 'com.android.application'apply plugin: 'kotlin-android'android { compileSdkVersion 25 buildToolsVersion "25.0.1" defaultConfig { applicationId "com.future.customcontrolsfirstdemo" minSdkVersion 11 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"}repositories { mavenCentral()}
项目路径下的Build.gradle也有添加内容:buildscript { ext.kotlin_version = '1.1.51' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
经过编译后,项目应该能够正常运行。xml文件大体和原先项目一致:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.future.customcontrolsfirstdemo.Main2Activity"> <TextView android:id="@+id/kotlin_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginTop="8dp" android:text="Hello Kotlin !!!" android:textColor="#ff0000" android:textSize="32sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.027" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.047" /></RelativeLayout>
Activity中的实现:
class Main2Activity : AppCompatActivity(), View.OnClickListener { private var textView: TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) textView = findViewById(R.id.kotlin_tv) as TextView textView!!.text = "hello kotlin!" }}
是的,如此,就算是踏入Kotlin的大门了吧,或许早了点,看见门了。。。。
二、Kotlin HelloWorld的扩展实现
如上边实例所表述,XML没有什么变化,控件的实例化没有什么变化,控件的使用也没有什么变化。那Kotlin为何能够迅速推广开来呢?
我们做点小小的改动,初窥门径。
Module下Build.gradle顶部添加:
apply plugin: 'kotlin-android-extensions'
在类的实现中添加:
import kotlinx.android.synthetic.main.activity_main.*
其中activity_main是指XML的名称。
有那么一点神奇的事情发生了,我们不需要findViewByID();
class Main2Activity : AppCompatActivity(), View.OnClickListener { private var textView: TextView? = null var t: Toast? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) textView = findViewById(R.id.kotlin_tv) as TextView textView!!.text = "hello kotlin!" btn1.setOnClickListener(this) btn2.setOnClickListener(this) } override fun onClick(v: View?) { when (v?.id) { R.id.btn1 -> Toast.makeText(this@Main2Activity, "${btn1.text}", Toast.LENGTH_SHORT).show() R.id.btn2 -> if (t == null) { Toast.makeText(this@Main2Activity, "${btn2.text}", Toast.LENGTH_SHORT).show() } else { t?.setText("${btn2.text}") t?.show() } } }}
三、细节扩展
在Android Studio编译工具下,安装插件以后,能够将之前的Java文件进行转换成为Kotlin代码。
面对一个新事物,我们应该学习和接纳他,同时也需要省视他。推荐一篇文章,也是给自己做一个标记:
http://www.jianshu.com/p/f364e3f9cc36 Kotlin --这次入门就不用放弃了
一个迷茫阶段,没有把自己直接提升一个境界的好方法,也不确信在未来能够有更好的处理办法。甚至于觉得自己并不是那种聪明的人,在这一片沼泽地里,迷失方向的小孩只有:适当的坚持锻炼,维持一个还不错的身体;继续在自己所选择的路上慢慢积累,尽管那么的不明显,收效相对的微小。
- Android Studio 下的Kotlin HelloWorld
- Kotlin在android studio下的初次使用
- Android Studio Kotlin插件的简单使用
- Android studio 配置Kotlin的开发环境
- Android Studio 集成Kotlin遇到的错误
- android studio kotlin
- android studio配置kotlin
- android studio 3.0 kotlin
- Android Studio 配置Kotlin
- Android Studio+Kotlin初探
- Android Studio配置Kotlin
- Android Studio 集成 kotlin
- Android-studio&Kotlin
- Android Studio配置kotlin
- 【Kotlin】Android Studio 配置
- android studio入门Kotlin
- Kotlin + android Studio
- Android Studio的helloworld遇到的坑
- otsu
- 安卓头像设置
- Principles of training multi-layer neural network using backpropagation
- git忽略跟踪文件修改
- 一列数的规则如下:1、1、2、3、5、8、13、21、34…… 求第30位数是多少,用递归算法实现。
- Android Studio 下的Kotlin HelloWorld
- 关于ajax跨域访问,Access-Control-Allow-Origin 404的简单解决办法
- TextView跑马灯效果
- 综合练习:路由+过滤器+服务+指令
- ElasticSearch5.X 增删改查
- 记账本
- ionic下拉刷新
- c++ map使用(增删查改遍历)
- MySQL主从复制的常用拓扑结构