kotlin写android,Kotlin安卓开发

1.配置Android Studio

a.在Project对应的build.gradle文件中添加如下代码:

buildscript {

ext.kotlin_version ='1.2.30' //kotlin版本

ext.anko_version ='0.8.2' //anko是方便kotlin开发android的库,不需要findById()...

repositories {

jcenter()

google()

}

dependencies {

classpath'com.android.tools.build:gradle:3.1.3'

classpath"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

classpath"org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

}

}

b.在module对应的build.gradle文件 中添加代码:

添加插件

apply plugin: 'kotlin-android'

applyplugin:'kotlin-android-extensions'

添加依赖库

implementation"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

2.创建工程

使用android3.1.3创建kotlin

39c18348e136

3365026-bb212eff10c5d9b1.png

勾选红色部分,不需要自己配置了上面信息了

引用了anko,简化了好多工作,例如:

按钮的点击事件:

buttonID.onClick { ... }

intent跳转:

startActivity("key" to value, "key1" to "value1")

打开浏览器:

browse("https://www.baidu.com")

分享:

share("分享", "subject")

发邮件:

email("邮箱地址", "subject", "text")

Toast:

toast("Hello world")

toast(R.string.xx)

longToast("Hello world")

对话框:

alert("内容", "标题") {

positiveButton("确定") {... }

negativeButton("取消") { ... }

}.show()

kotlin实践

如图:

39c18348e136

TIM图片20190522171033.png

使用kotlin语言实现这个设置界面,首先 创建一个实现 AnkoComponent接口的类

class SettingUI :AnkoComponent{

override fun createView(ui: AnkoContext): View = with(ui) {

verticalLayout {

backgroundColor = Color.parseColor("#1c1e21")

relativeLayout {

backgroundColor = Color.BLACK

textView("设置"){

textColor = Color.WHITE

textSize = 20f

}.lparams(wrapContent, wrapContent){

centerInParent()

centerVertically()

}

imageView(R.mipmap.ic_back).lparams(wrapContent, wrapContent){

alignParentLeft()

centerVertically()

}

}.lparams(matchParent, dip(58)) {

setMargins(0,0,0,10)

}

relativeLayout {

backgroundColor = Color.parseColor("#1c1e21")

textView("单位设置"){

textColor = Color.WHITE

textSize = 16f

}.lparams(wrapContent, wrapContent){

centerVertically()

setMargins(dip(10),0,0,0)

}

imageView(R.mipmap.arrow_gray).lparams(wrapContent, wrapContent){

alignParentRight()

centerVertically()

}

onClick {

toast("点击了单位设置")

}

view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){

setMargins(dip(10),0,0,0)

alignParentBottom()

}

}.lparams(matchParent, dip(48))

relativeLayout {

backgroundColor = Color.parseColor("#1c1e21")

var weeklyTv = textView("星期开始日"){

textColor = Color.WHITE

textSize = 16f

}.lparams(wrapContent, wrapContent){

centerVertically()

setMargins(dip(10),0,0,0)

}

textView("周日"){

textColor = Color.WHITE

textSize = 13f

val rightDrawable = ctx.resources.getDrawable(R.mipmap.arrow_gray)

rightDrawable.setBounds(0,0,rightDrawable.minimumWidth,rightDrawable.minimumHeight);

setCompoundDrawables(null,null,rightDrawable,null)

compoundDrawablePadding = dip(3)

}.lparams(wrapContent, wrapContent){

centerVertically()

alignParentRight()

}

onClick {

toast(weeklyTv.text)

}

view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){

setMargins(dip(10),0,0,0)

alignParentBottom()

}

}.lparams(matchParent, dip(48))

relativeLayout {

backgroundColor = Color.parseColor("#1c1e21")

textView("账号管理"){

textColor = Color.WHITE

textSize = 16f

}.lparams(wrapContent, wrapContent){

centerVertically()

setMargins(dip(10),0,0,0)

}

imageView(R.mipmap.arrow_gray).lparams(wrapContent, wrapContent){

alignParentRight()

centerVertically()

}

onClick {

toast("账号管理")

}

view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){

setMargins(dip(10),0,0,0)

alignParentBottom()

}

}.lparams(matchParent, dip(48))

relativeLayout {

backgroundColor = Color.parseColor("#1c1e21")

var weeklyTv = textView("APP版本"){

textColor = Color.WHITE

textSize = 16f

}.lparams(wrapContent, wrapContent){

centerVertically()

setMargins(dip(10),0,0,0)

}

textView("v.${BuildConfig.VERSION_NAME}"){

textColor = Color.WHITE

textSize = 13f

val rightDrawable = ctx.resources.getDrawable(R.mipmap.arrow_gray)

rightDrawable.setBounds(0,0,rightDrawable.minimumWidth,rightDrawable.minimumHeight);

setCompoundDrawables(null,null,rightDrawable,null)

compoundDrawablePadding = dip(3)

}.lparams(wrapContent, wrapContent){

centerVertically()

alignParentRight()

}

onClick {

toast(weeklyTv.text)

}

view { backgroundColor = Color.parseColor("#141517") }.lparams(matchParent,dip(1)){

setMargins(dip(10),0,0,0)

alignParentBottom()

}

}.lparams(matchParent, dip(48))

button("退出登录"){

textColor = Color.WHITE

textSize = 20f

backgroundColor = Color.parseColor("#ff1353")

}.lparams(dip(300),dip(50)){

setMargins(0,dip(10),0,0)

setGravity(Gravity.CENTER_HORIZONTAL)

}

}

}

}

在SettingAct中调用:

class WidgetsAct : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

SettingUI().setContentView(this)

}

}

大功告成,ok!,

说实话还是习惯了xml写UI,哈哈哈哈!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值