使用kotlin开发android
16lz
2022-04-09
说明
本篇简单介绍使用kotlin开放android的基本操作,有关kotlin的配置,文件创建,基础语法等。
创建kotlin开发环境
通过gradle创建基本的kotlin开发环境,需要使用kotlin支持插件,用于支持android开放,其基本配置build.gradle文件如下:
buildscript { ext.kotlin_version = '1.1.3-2' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.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 }}repositories { repositories { jcenter() } }//使用android 插件apply plugin: 'com.android.application'//使用kotlin支持android插件apply plugin: 'kotlin-android'android { compileSdkVersion 25 buildToolsVersion "25.0.3" defaultConfig { applicationId "com.haolianluo.myapplication" minSdkVersion 21 targetSdkVersion 19 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile 'com.android.support:appcompat-v7:25.3.1' }
如上,为基本的kotlin支持配置,kotlin文件的后缀为*.kt,创建对应kt文件实现基本逻辑(和java类似)。如下,为一个基本的kotlin文件:
package cn.enjoytoday.bmobimport android.app.Activityimport android.os.Bundleimport android.view.LayoutInflaterimport android.view.Viewimport android.widget.FrameLayoutimport android.widget.Toastimport cn.bmob.v3.Bmobimport cn.enjoytoday.R/** * bmob test activity. */class BmobActivity constructor(name: String):Activity() { constructor(name:String,type:String):this(name){ } var BMOB_APP_ID:String="291b15675a92224a9170e6410fca8ff2" var fragment: FrameLayout?=null var main_layout: View?=null var register_layout: View?=null var sign_in_layout:View?=null var sign_out_layout:View?=null var upload_layout:View?=null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_bmob) Bmob.initialize(this,BMOB_APP_ID) initView() } /** * init view. */ fun initView(){ fragment= this.findViewById(R.id.content) as FrameLayout? main_layout=LayoutInflater.from(this).inflate(R.layout.bmob_main,null) register_layout=LayoutInflater.from(this).inflate(R.layout.bmob_register_layout,null) sign_in_layout=LayoutInflater.from(this).inflate(R.layout.bmob_sign_in_layout,null) sign_out_layout=LayoutInflater.from(this).inflate(R.layout.bmob_sign_out_layout,null) upload_layout=LayoutInflater.from(this).inflate(R.layout.bmob_upload_layout,null) /** * 当fragment为空时抛出异常 * fragment!!.removeAllViews() * * 当fragment为空时返回为空 */ fragment?.removeAllViews() fragment?.addView(main_layout) } fun onClick(view:View){ when(view.id){ R.id.register ->{ fragment?.removeAllViews() fragment?.addView(register_layout) } R.id.sing_in -> { fragment?.removeAllViews() fragment?.addView(sign_in_layout) } R.id.sign_out -> { fragment?.removeAllViews() fragment?.addView(sign_out_layout) } R.id.upload -> { fragment?.removeAllViews() fragment?.addView(upload_layout) } else -> { toast("No found this id.") } } } /** * toast add. */ fun Activity.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, message, duration).show() }}
总结,如上kotlin使用的几个基础语法格式:
- 变量声明
变量通过关键字 var 修饰,基本格式如下:
var param:Type(变量类型,基本类型可以不写) = value
默认遍历使用需要进行初始化操作且不可为null,可以通过如下方式初始化:
var parma:String? =null //?可以为空
- 方法声明
方法使用格式如下:
[override] fun name(param1:String?,param2:Int):[Type]{ //实现 return p}
如上,override 为重写父类方法关键字,fun为方法声明关键字,param1:String?,param2:Int 为传递的参数名和参数类型,Type为返回值类型,默认为Void,无返回值。
- 类声明创建
kotlin 中的类的创建格式如下:
class BmobActivity constructor(name: String):Activity() { ... constructor(name:String,type:String):this(name){ //init }}
如上,class 为类声明符号,constructor为 BmobActivity 的主构造器,Activity为其继承的父类,constructor(name:String,type:String):this(name) 为其第二构造器.
- 参考
参考示例:https://github.com/fishly/AndroidDemo/tree/master/ActionBarDemo
Enjoytoday,EnjoyCoding
更多相关文章
- maven 学习笔记(二)-创建简单的eclipse+android+maven工程
- Android(安卓)获取设备唯一号 unknown
- Android中使用log4j
- xUtils3注解方式编程
- Android(安卓)开发中涉及到的设计模式
- 《Android学习指南》目录
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用